uninit_vec: special case set_len(0)

set_len(0) does not create uninitialized elements. Fixes a false positive with
the following pattern:

    fn copy_slice_into_vec(dst: &mut Vec<u8>, src: &[u8]) {
        dst.reserve(src.len().saturating_sub(dst.len()));
        unsafe {
            dst.set_len(0);
            std::ptr::copy_nonoverlapping(src.as_ptr(), dst.as_mut_ptr(), src.len());
            dst.set_len(src.len());
        }
    }
This commit is contained in:
Alessandro Decina 2022-09-23 00:06:13 +01:00
parent 61fd2a8c6f
commit 49319b4206
2 changed files with 22 additions and 2 deletions

View file

@ -91,4 +91,10 @@ fn main() {
vec1.set_len(200);
vec2.set_len(200);
}
// set_len(0) should not be detected
let mut vec: Vec<u8> = Vec::with_capacity(1000);
unsafe {
vec.set_len(0);
}
}