Skip to content

Commit e2f271b

Browse files
committed
fix off-by-one error in a bounds check
1 parent 3bac806 commit e2f271b

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

libbz2-rs-sys/src/decompress.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,7 +597,7 @@ pub(crate) fn decompress(
597597
uc = GET_BYTE!(strm, s);
598598

599599
s.origPtr = (s.origPtr << 8) | i32::from(uc);
600-
if !(0..10 + 100000 * i32::from(s.blockSize100k)).contains(&s.origPtr) {
600+
if !(0..=10 + 100000 * i32::from(s.blockSize100k)).contains(&s.origPtr) {
601601
error!(BZ_DATA_ERROR);
602602
}
603603

test-libbz2-rs-sys/src/lib.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1454,3 +1454,26 @@ mod high_level_interface {
14541454
drop(path_as_cstring);
14551455
}
14561456
}
1457+
1458+
#[test]
1459+
fn orig_ptr_bounds_check_off_by_1() {
1460+
// From https://git.radicallyopensecurity.com/ngi/ngicore-zip-linting-and-bzip2-in-rust/-/issues/6
1461+
//
1462+
// A bounds check in `decompress.rs` was off-by-one in the rust version.
1463+
let source: &[u8] = &[
1464+
0x42, 0x5a, 0x68, 0x32, 0x31, 0x41, 0x59, 0x26, 0x53, 0x59, 0x03, 0x4f, 0x7e, 0x01, 0x01,
1465+
0x86, 0xa5, 0x00, 0x00,
1466+
];
1467+
1468+
let (err_c, dest_c) =
1469+
unsafe { crate::decompress_c_with_capacity(1 << 16, source.as_ptr(), source.len() as _) };
1470+
1471+
let (err_rs, dest_rs) =
1472+
unsafe { crate::decompress_rs_with_capacity(1 << 16, source.as_ptr(), source.len() as _) };
1473+
1474+
assert_eq!(err_c, err_rs);
1475+
1476+
if err_c == libbz2_rs_sys::BZ_OK {
1477+
assert_eq!(dest_c, dest_rs);
1478+
}
1479+
}

0 commit comments

Comments
 (0)