Skip to content

Commit 8713d1c

Browse files
authored
bit_lib: don't read past the buffer when the bits fit one byte (#4424)
bit_lib_get_bits reads data[position/8 + 1] whenever position isn't byte-aligned, even when all the requested bits live in the current byte. When position/8 is the last byte of the buffer that's a one-byte over-read. The extra byte only contributes bits that get shifted back out, so the return value is unchanged and optimized builds often drop the load, but at -O0 AddressSanitizer flags it and the access is still out of bounds. Skip the next-byte read when shift + length <= 8. This is the same fix that already landed in fbtng-corelibs; lib/bit_lib here is an independent copy that never picked it up, so the TODO FL-3534 comment is still here. Signed-off-by: Cole Munz <colemunz@gmail.com>
1 parent 376dc48 commit 8713d1c

1 file changed

Lines changed: 3 additions & 1 deletion

File tree

lib/bit_lib/bit_lib.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ uint8_t bit_lib_get_bits(const uint8_t* data, size_t position, uint8_t length) {
3737
uint8_t shift = position % 8;
3838
if(shift == 0) {
3939
return data[position / 8] >> (8 - length);
40+
} else if(shift + length <= 8) {
41+
// Requested bits fit in the current byte, don't read the next one
42+
return (uint8_t)(data[position / 8] << shift) >> (8 - length);
4043
} else {
41-
// TODO FL-3534: fix read out of bounds
4244
uint8_t value = (data[position / 8] << (shift));
4345
value |= data[position / 8 + 1] >> (8 - shift);
4446
value = value >> (8 - length);

0 commit comments

Comments
 (0)