From 4b395523dd54046c9658f3c59f278927203e682e Mon Sep 17 00:00:00 2001 From: Andreas Longva Date: Tue, 19 Jan 2021 15:02:23 +0100 Subject: [PATCH] Fix issue with UninitVec and zero-sized types --- nalgebra-sparse/src/cs.rs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/nalgebra-sparse/src/cs.rs b/nalgebra-sparse/src/cs.rs index 30d73007..e79557b2 100644 --- a/nalgebra-sparse/src/cs.rs +++ b/nalgebra-sparse/src/cs.rs @@ -403,13 +403,17 @@ impl<'a, T> CsLaneMut<'a, T> { /// Helper struct for working with uninitialized data in vectors. /// TODO: This doesn't belong here. struct UninitVec { - vec: Vec + vec: Vec, + len: usize } impl UninitVec { pub fn from_len(len: usize) -> Self { Self { - vec: Vec::with_capacity(len) + vec: Vec::with_capacity(len), + // We need to store len separately, because for zero-sized types, + // Vec::with_capacity(len) does not give vec.capacity() == len + len } } @@ -425,7 +429,7 @@ impl UninitVec { /// It is undefined behavior to call this function unless *all* elements have been written to /// exactly once. pub unsafe fn assume_init(mut self) -> Vec { - self.vec.set_len(self.vec.capacity()); + self.vec.set_len(self.len); self.vec } }