Skip to content

Commit e1b0d3e

Browse files
committed
Fix integer overflow in BitBufferMut::collect_bool, truncate, and append_n
Signed-off-by: Sungjin Kim <carp1230@gmail.com>
1 parent d257d09 commit e1b0d3e

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

vortex-buffer/src/bit/buf_mut.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,8 @@ impl BitBufferMut {
186186
/// Invokes `f` with indexes `0..len` collecting the boolean results into a new `BitBufferMut`
187187
#[inline]
188188
pub fn collect_bool<F: FnMut(usize) -> bool>(len: usize, f: F) -> Self {
189-
let num_words = len.div_ceil(64);
189+
let num_words = len.checked_add(63)
190+
.expect("collect_bool: len overflow") / 64;
190191
let mut buffer: BufferMut<u64> = BufferMut::with_capacity(num_words);
191192
// SAFETY: `collect_bool_words` writes every word in `0..num_words` below
192193
// before any read; `u64` has no invalid bit patterns and the assignments
@@ -376,7 +377,8 @@ impl BitBufferMut {
376377
return;
377378
}
378379

379-
let end_bit = self.offset + len;
380+
let end_bit = self.offset.checked_add(len)
381+
.expect("BitBufferMut::truncate: offset + len overflow");
380382
let new_len_bytes = end_bit.div_ceil(8);
381383
self.buffer.truncate(new_len_bytes);
382384
self.len = len;
@@ -442,7 +444,9 @@ impl BitBufferMut {
442444
return;
443445
}
444446

445-
let end_bit_pos = self.offset + self.len + n;
447+
let end_bit_pos = self.offset.checked_add(self.len)
448+
.and_then(|v| v.checked_add(n))
449+
.expect("BitBufferMut::append_n: offset + len + n overflow");
446450
let required_bytes = end_bit_pos.div_ceil(8);
447451

448452
// Ensure buffer has enough bytes

0 commit comments

Comments
 (0)