fix unsoundness in `Segment::build_index`

Fixes #185
This commit is contained in:
Scott Lamb 2023-01-29 14:58:43 -08:00
parent 284a59b05e
commit 159e426943
No known key found for this signature in database
1 changed files with 14 additions and 5 deletions

View File

@ -443,11 +443,20 @@ impl Segment {
let s = &self.s;
let lens = self.lens();
let len = lens.stts + lens.stsz + lens.stss;
let mut buf = {
let mut v = Vec::with_capacity(len);
unsafe { v.set_len(len) };
v.into_boxed_slice()
};
// This was a few percent faster when we didn't pre-initialize the
// slice (as in the commented-out code below), but it was unsound. See
// <https://github.com/scottlamb/moonfire-nvr/issues/185>. It might be
// nice to use `MaybeUninit::write_slice` here when it stabilizes,
// more ergonomic than dealing with raw pointers. In the meantime, a few
// percent difference in speed here probably isn't the biggest unsolved
// problem with Moonfire...
let mut buf = vec![0; len].into_boxed_slice();
// let mut buf = {
// let mut v = Vec::with_capacity(len);
// unsafe { v.set_len(len) };
// v.into_boxed_slice()
// };
{
let (stts, rest) = buf.split_at_mut(lens.stts);