diff --git a/sys/sys/bitstring.h b/sys/sys/bitstring.h index f15c34e61de3..a8c8a4e2a396 100644 --- a/sys/sys/bitstring.h +++ b/sys/sys/bitstring.h @@ -202,6 +202,11 @@ bit_ffs_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result) bitstr_t _test; int _value, _offset; + if (_start >= _nbits) { + *_result = -1; + return; + } + if (_nbits > 0) { _curbitstr = _bitstr + _bit_idx(_start); _stopbitstr = _bitstr + _bit_idx(_nbits - 1); @@ -231,6 +236,11 @@ bit_ffc_at(bitstr_t *_bitstr, int _start, int _nbits, int *_result) bitstr_t _test; int _value, _offset; + if (_start >= _nbits) { + *_result = -1; + return; + } + if (_nbits > 0) { _curbitstr = _bitstr + _bit_idx(_start); _stopbitstr = _bitstr + _bit_idx(_nbits - 1); diff --git a/tests/sys/sys/bitstring_test.c b/tests/sys/sys/bitstring_test.c index fbfe78275b23..66cb8b559b1a 100644 --- a/tests/sys/sys/bitstring_test.c +++ b/tests/sys/sys/bitstring_test.c @@ -246,6 +246,17 @@ BITSTRING_TC_DEFINE(bit_ffs_at) nbits, memloc, i, found_set_bit); } } + + /* Pass a start value beyond the size of the bit string */ + bit_ffs_at(bitstr, nbits, nbits, &found_set_bit); + ATF_REQUIRE_MSG(found_set_bit == -1, + "bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d", + nbits, memloc, nbits, found_set_bit); + + bit_ffs_at(bitstr, nbits + 3, nbits, &found_set_bit); + ATF_REQUIRE_MSG(found_set_bit == -1, + "bit_ffs_at_%d_%s: Failed with high start value of %d, Result %d", + nbits, memloc, nbits + 3, found_set_bit); } BITSTRING_TC_DEFINE(bit_ffc_at) @@ -297,6 +308,17 @@ BITSTRING_TC_DEFINE(bit_ffc_at) nbits, memloc, i, found_clear_bit); } } + + /* Pass a start value beyond the size of the bit string */ + bit_ffc_at(bitstr, nbits, nbits, &found_clear_bit); + ATF_REQUIRE_MSG(found_clear_bit == -1, + "bit_ffc_at_%d_%s: Failed with high start value, Result %d", + nbits, memloc, found_clear_bit); + + bit_ffc_at(bitstr, nbits + 3, nbits, &found_clear_bit); + ATF_REQUIRE_MSG(found_clear_bit == -1, + "bit_ffc_at_%d_%s: Failed with high start value of %d, Result %d", + nbits, memloc, nbits + 3, found_clear_bit); } BITSTRING_TC_DEFINE(bit_nclear)