Skip to content

Commit 4a4af7e

Browse files
committed
fix logic error in blocksort
1 parent 71c89f6 commit 4a4af7e

3 files changed

Lines changed: 5979 additions & 1 deletion

File tree

libbz2-rs-sys/src/blocksort.rs

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,19 @@ fn mainSort(
864864
let bbStart = ftab[(ss as usize) << 8] & CLEARMASK;
865865
let bbSize = (ftab[(ss as usize + 1) << 8] & CLEARMASK) as i32 - bbStart as i32;
866866

867-
let shifts = (bbSize >> 16).count_ones();
867+
// FIXME: remove when our MSRV can use the stable method.
868+
fn highest_one(x: i32) -> Option<u32> {
869+
match x {
870+
0 => None,
871+
_ => Some(i32::BITS - 1 - x.leading_zeros()),
872+
}
873+
}
874+
875+
let shifts = if bbSize <= 65_534 {
876+
0
877+
} else {
878+
highest_one(bbSize).unwrap() - 15
879+
};
868880

869881
let ptr = &ptr[bbStart as usize..][..bbSize as usize];
870882
for j in (0..bbSize).rev() {

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,13 @@ fn compress_sample1() {
238238
assert_eq_compress!("../../tests/input/quick/sample1.bz2");
239239
}
240240

241+
#[test]
242+
fn issue_137() {
243+
// Exposed a logic error in blocksort.
244+
// See https://github.com/trifectatechfoundation/libbzip2-rs/issues/137.
245+
assert_eq_compress!("../../tests/input/issue_137.bin");
246+
}
247+
241248
#[test]
242249
fn compress_sample2() {
243250
assert_eq_compress!("../../tests/input/quick/sample2.bz2");

0 commit comments

Comments
 (0)