Commit 371c984
Fix integer overflow in BitBufferMut::truncate and append_n (#8474)
## Summary
Several methods on `BitBufferMut` use unchecked arithmetic on
offset/length values, which can overflow in release mode and lead to
undefined behavior reachable entirely from safe code
(`#![forbid(unsafe_code)]`).
Affected methods:
- `truncate`: `self.offset + len` overflows, causing the buffer to be
truncated to an incorrect size.
- `append_n`: `self.offset + self.len + n` overflows, causing incorrect
capacity calculation.
Fix: add overflow assertions before the unchecked addition.
Reproduction (all use `#![forbid(unsafe_code)]`):
```rust
// 1. truncate
let buffer = ByteBufferMut::zeroed(10);
let mut bb = BitBufferMut::from_buffer(buffer, usize::MAX - 2, 80);
bb.truncate(3);
bb.set(2);
// 2. append_n
let buffer = ByteBufferMut::zeroed(2);
let mut bb = BitBufferMut::from_buffer(buffer, 1000, 0);
bb.append_n(true, usize::MAX);
bb.set(10000);
```
Related: #7979 (the `BufferMut::zeroed_aligned` overflow I reported to
maintainers in April 2025; this PR addresses additional overflow sites
found during the same audit)
## Testing
Existing `cargo test -p vortex-buffer` passes (789 tests + 6 doc-tests).
Both PoCs now panic with overflow message instead of UB.
Happy to adjust the approach if maintainers prefer a different fix
strategy.
---------
Signed-off-by: Sungjin Kim <carp1230@gmail.com>
Signed-off-by: Robert Kruszewski <github@robertk.io>
Co-authored-by: Robert Kruszewski <github@robertk.io>1 parent 0c7e0ff commit 371c984
1 file changed
Lines changed: 11 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
378 | 378 | | |
379 | 379 | | |
380 | 380 | | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
381 | 385 | | |
382 | 386 | | |
383 | 387 | | |
| |||
444 | 448 | | |
445 | 449 | | |
446 | 450 | | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
447 | 458 | | |
448 | 459 | | |
449 | 460 | | |
| |||
0 commit comments