Skip to content

Commit

Permalink
fix undefined behavior of testing beyond the end of the iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
bostick committed Oct 29, 2024
1 parent 18a771e commit b59c7b6
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions lib/tbt-parser-util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,9 +375,19 @@ zlib_inflate(

ASSERT(it <= end);

//
// it is undefined behavior to test beyond the end of the iterator by doing:
// (it + CHUNK <= end)
//
// however, it is fine to do:
// CHUNK <= (end - it)
//
// (end - it) returns a difference type and may be negative
//

if (it == end) {
break;
} else if (it + CHUNK <= end) {
} else if (CHUNK <= (end - it)) {
strm.avail_in = CHUNK;
} else {
strm.avail_in = static_cast<uInt>(end - it);
Expand Down Expand Up @@ -415,7 +425,7 @@ zlib_inflate(

} while (strm.avail_out == 0);

if (it + CHUNK < end) {
if (CHUNK < (end - it)) {
strm.avail_in = CHUNK;
it += CHUNK;
} else {
Expand Down

0 comments on commit b59c7b6

Please sign in to comment.