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

fixup: BitpackIntegerDecoder sometimes reads past end of input buffer #87

Merged
merged 3 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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