Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove double bounds check #77

Merged
merged 2 commits into from
Jan 14, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
121 changes: 57 additions & 64 deletions libbz2-rs-sys/src/decompress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,25 +213,24 @@
($s:expr) => {
if groupPos == 0 {
groupNo += 1;
if groupNo >= nSelectors as i32 {
error!(BZ_DATA_ERROR);
} else {
groupPos = 50;
gSel = $s.selector[groupNo as usize];
gLimit = gSel;
gPerm = gSel;
gBase = gSel;
gMinlen = $s.minLens[gSel as usize];
}
gSel = match $s.selector[..usize::from(nSelectors)].get(groupNo as usize) {
Some(&gSel) => gSel,
None => error!(BZ_DATA_ERROR),
};
gLimit = gSel;
gPerm = gSel;
gBase = gSel;
gMinlen = $s.minLens[gSel as usize];
groupPos = 50;
}
groupPos -= 1;
};
}

macro_rules! error {
($code:ident) => {
($code:ident) => {{
break 'save_state_and_return ReturnCode::$code;
};
}};
}

match s.state {
Expand Down Expand Up @@ -356,14 +355,11 @@

uc = GET_BYTE!(strm, s);

if uc == 0x17 {
// skips to `State::BZ_X_ENDHDR_2`
current_block = BZ_X_ENDHDR_2;
} else if uc != 0x31 {
error!(BZ_DATA_ERROR);
} else {
current_block = BZ_X_BLKHDR_2;
}
match uc {
0x17 => current_block = BZ_X_ENDHDR_2,
0x31 => current_block = BZ_X_BLKHDR_2,
_ => error!(BZ_DATA_ERROR),

Check warning on line 361 in libbz2-rs-sys/src/decompress.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/decompress.rs#L361

Added line #L361 was not covered by tests
};
Comment on lines +358 to +362
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is just a cleanup that i spotted

}
match current_block {
BZ_X_ENDHDR_2 => {
Expand Down Expand Up @@ -768,12 +764,11 @@
if zn > 20 {
error!(BZ_DATA_ERROR);
} else if zvec <= s.limit[gLimit as usize][zn as usize] {
if !(0..258).contains(&(zvec - s.base[gBase as usize][zn as usize])) {
error!(BZ_DATA_ERROR);
} else {
nextSym = s.perm[gPerm as usize]
[(zvec - s.base[gBase as usize][zn as usize]) as usize];
}
let index = zvec - s.base[gBase as usize][zn as usize];
nextSym = match s.perm[gPerm as usize].get(index as usize) {
Some(&nextSym) => nextSym,
None => error!(BZ_DATA_ERROR),

Check warning on line 770 in libbz2-rs-sys/src/decompress.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/decompress.rs#L770

Added line #L770 was not covered by tests
};
Comment on lines +767 to +771
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the s.perm[i] arrays are 258 elements long, so the check for the index value to be in bounds was happening twice (though of course llvm might optimize that out)

} else {
zn += 1;
current_block = BZ_X_MTF_6;
Expand All @@ -785,43 +780,42 @@
if zn > 20 {
error!(BZ_DATA_ERROR);
} else if zvec <= s.limit[gLimit as usize][zn as usize] {
if !(0..258).contains(&(zvec - s.base[gBase as usize][zn as usize])) {
error!(BZ_DATA_ERROR);
let index = zvec - s.base[gBase as usize][zn as usize];
nextSym = match s.perm[gPerm as usize].get(index as usize) {
Some(&nextSym) => nextSym,
None => error!(BZ_DATA_ERROR),

Check warning on line 786 in libbz2-rs-sys/src/decompress.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/decompress.rs#L786

Added line #L786 was not covered by tests
};
if nextSym == BZ_RUNA as i32 || nextSym == BZ_RUNB as i32 {
current_block = Block46;
} else {
nextSym = s.perm[gPerm as usize]
[(zvec - s.base[gBase as usize][zn as usize]) as usize];
if nextSym == BZ_RUNA as i32 || nextSym == BZ_RUNB as i32 {
current_block = Block46;
} else {
es += 1;
uc = s.seqToUnseq[s.mtfa[s.mtfbase[0_usize] as usize] as usize];
s.unzftab[uc as usize] += es;
match s.smallDecompress {
DecompressMode::Small => {
while es > 0 {
if nblock >= 100000 * nblockMAX100k as u32 {
error!(BZ_DATA_ERROR);
} else {
ll16[nblock as usize] = uc as u16;
nblock += 1;
es -= 1;
}
es += 1;
uc = s.seqToUnseq[s.mtfa[s.mtfbase[0_usize] as usize] as usize];
s.unzftab[uc as usize] += es;
match s.smallDecompress {
DecompressMode::Small => {
while es > 0 {
if nblock >= 100000 * nblockMAX100k as u32 {
error!(BZ_DATA_ERROR);

Check warning on line 798 in libbz2-rs-sys/src/decompress.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/decompress.rs#L798

Added line #L798 was not covered by tests
} else {
ll16[nblock as usize] = uc as u16;
nblock += 1;
es -= 1;
}
}
DecompressMode::Fast => {
while es > 0 {
if nblock >= 100000 * nblockMAX100k as u32 {
error!(BZ_DATA_ERROR);
} else {
tt[nblock as usize] = uc as u32;
nblock += 1;
es -= 1;
}
}
DecompressMode::Fast => {
while es > 0 {
if nblock >= 100000 * nblockMAX100k as u32 {
error!(BZ_DATA_ERROR);

Check warning on line 809 in libbz2-rs-sys/src/decompress.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/decompress.rs#L809

Added line #L809 was not covered by tests
} else {
tt[nblock as usize] = uc as u32;
nblock += 1;
es -= 1;
}
}
}
current_block = Block40;
}
current_block = Block40;
}
} else {
zn += 1;
Expand All @@ -833,12 +827,11 @@
if zn > 20 {
error!(BZ_DATA_ERROR);
} else if zvec <= s.limit[gLimit as usize][zn as usize] {
if !(0..258).contains(&(zvec - s.base[gBase as usize][zn as usize])) {
error!(BZ_DATA_ERROR);
} else {
nextSym = s.perm[gPerm as usize]
[(zvec - s.base[gBase as usize][zn as usize]) as usize];
}
let index = zvec - s.base[gBase as usize][zn as usize];
nextSym = match s.perm[gPerm as usize].get(index as usize) {
Some(&nextSym) => nextSym,
None => error!(BZ_DATA_ERROR),

Check warning on line 833 in libbz2-rs-sys/src/decompress.rs

View check run for this annotation

Codecov / codecov/patch

libbz2-rs-sys/src/decompress.rs#L833

Added line #L833 was not covered by tests
};
} else {
zn += 1;
current_block = BZ_X_MTF_2;
Expand Down Expand Up @@ -1285,8 +1278,6 @@

EOB = s.nInUse + 1;
nblockMAX100k = s.blockSize100k as u8;
groupNo = -1;
groupPos = 0;
Comment on lines -1288 to -1289
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

moved over to hopefully make it clearer why groupNo cannot be unsigned (currently anyway, maybe with some extra refactoring).

s.unzftab.fill(0);

/*-- MTF init --*/
Expand All @@ -1302,6 +1293,8 @@
/*-- end MTF init --*/

nblock = 0;
groupNo = -1;
groupPos = 0;
update_group_pos!(s);

zn = gMinlen;
Expand Down
Loading