Description
The "Guarantees" section in the documentation for Vec
has some unclear wording, especially in the second-to-last paragraph:
Vec will not specifically overwrite any data that is removed from it, but also won't specifically preserve it. Its uninitialized memory is scratch space that it may use however it wants. It will generally just do whatever is most efficient or otherwise easy to implement.
In my reading, that means we are explicitly not allowed to store anything (e.g. via FFI calls) in the Vec
's unused (but allocated) capacity, beyond its current valid length (size) -- but some others are disagreeing with my interpretation.
Specifically, the question is whether doing something like this is invalid:
let buf_size: usize = 666;
unsafe {
let buf: Vec<u8> = Vec::with_capacity(buf_size);
let data_size = libc::read(fd, buf.as_mut_ptr() as *mut c_void, buf_size as size_t) as usize;
assert(data_size >= 0);
buf.set_len(data_size);
}
As opposed to this variant, which should be valid even in my reading:
let buf_size: usize = 666;
unsafe {
let buf: Vec<u8> = Vec::with_capacity(buf_size);
buf.set_len(buf_size);
let data_size = libc::read(fd, buf.as_mut_ptr() as *mut c_void, buf_size as size_t) as usize;
assert(data_size >= 0);
buf.set_len(data_size);
}