From 3a4afbcaea2270c49f9377063d27de60e0f24713 Mon Sep 17 00:00:00 2001 From: Ben Aaron Goldberg Date: Wed, 3 Nov 2021 20:30:30 -0400 Subject: [PATCH] [web] confirm safety of advance After further testing and checks I'm fairy confident that the vectored write code is safe. Signed-off-by: Ben Aaron Goldberg --- web/src/write_vectored_all.rs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/web/src/write_vectored_all.rs b/web/src/write_vectored_all.rs index cc153f6..e1082c9 100644 --- a/web/src/write_vectored_all.rs +++ b/web/src/write_vectored_all.rs @@ -77,7 +77,9 @@ fn advance<'a>(buf: &mut IoSlice<'a>, n: usize) { if buf.len() < n { panic!("advancing IoSlice beyond its length"); } - // SAFTEY: hopefully + // This is just a hacky way of advancing the pointer inside the IoSlice + // SAFTEY: The newly constructed IoSlice has the same lifetime as the old and + // this is guaranteed not to overflow the buffer due to the previous check unsafe { let mut ptr = buf.as_ptr() as *mut u8; ptr = ptr.add(n); @@ -87,3 +89,17 @@ fn advance<'a>(buf: &mut IoSlice<'a>, n: usize) { } } +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_advance() { + let expected: Vec<_> = (10..100).collect(); + let buf: Vec<_> = (0..100).collect(); + let mut io_slice = IoSlice::new(&buf); + advance(&mut io_slice, 10); + assert_eq!(io_slice.len(), 90); + assert_eq!(&*io_slice, &expected); + } +}