Skip to content

Commit

Permalink
BitpackIntegerDecoder sometimes reads past end of input buffer (#87)
Browse files Browse the repository at this point in the history
  • Loading branch information
nigels-com authored Dec 10, 2021
1 parent b811b72 commit 11c29f7
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
22 changes: 14 additions & 8 deletions src/Decoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -731,7 +731,19 @@ size_t BitpackIntegerDecoder<RegisterT>::inputProcessAligned( const char *inbuf,
#endif

RegisterT w;
if ( bitOffset > 0 )
if ( bitOffset == 0 )
{
/// The left shift (used below) is not defined if shift is >= size of
/// word
w = low;
}
// Avoid reading the next word, unless it is needed
// If the last record finishes on the last bit of input, avoid UMR
else if ( bitOffset + bitsPerRecord_ <= RegisterBits )
{
w = low >> bitOffset;
}
else
{
/// Get upper word (may or may not contain interesting bits),
RegisterT high = inp[wordPosition + 1];
Expand All @@ -743,13 +755,7 @@ size_t BitpackIntegerDecoder<RegisterT>::inputProcessAligned( const char *inbuf,
/// Shift high to just above the lower bits, shift low LSBit to bit0,
/// OR together. Note shifts are logical (not arithmetic) because using
/// unsigned variables.
w = ( high << ( 8 * sizeof( RegisterT ) - bitOffset ) ) | ( low >> bitOffset );
}
else
{
/// The left shift (used above) is not defined if shift is >= size of
/// word
w = low;
w = ( high << ( RegisterBits - bitOffset ) ) | ( low >> bitOffset );
}

#ifdef E57_MAX_VERBOSE
Expand Down
1 change: 1 addition & 0 deletions src/Decoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ namespace e57
double offset_;
unsigned bitsPerRecord_;
RegisterT destBitMask_;
static constexpr size_t RegisterBits = sizeof( RegisterT ) * 8;
};

class ConstantIntegerDecoder : public Decoder
Expand Down

0 comments on commit 11c29f7

Please sign in to comment.