Correct l_uintset_isempty() logic
cover Correct l_uintset_isempty() logic
The loop iteration over the 'bits' array in l_uintset_isempty() exceeded the maximum offset for the given l_uintset, resulting in out-of-bounds bits being considered for emptiness. Add a test that demonstrates the problem, and correct the maximum offset when iterating over the l_uintset 'bits' field.
Ossama Othman (2):
unit: Add l_uintset_isempty() test
uintset: Do not exceed max offset in empty check
ell/uintset.c | 5 ++++- unit/test-uintset.c | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)
Signature · 1 line
2.20.1
PATCH 1/2 unit: Add l_uintset_isempty() test
--- unit/test-uintset.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+)
unit/test-uintset.c +17 -0
@@ -347,6 +347,22 @@ static void test_uintset_intersect_test(const void *user_data)l_uintset_free (set_r); } +static void test_uintset_isempty (const void *user_data) +{ +struct l_uintset *a =NULL ; +struct l_uintset *b =l_uintset_new (32 ); +struct l_uintset *c =l_uintset_new (32 ); + +assert (l_uintset_put (c,10 )); + +assert (l_uintset_isempty (a)); +assert (l_uintset_isempty (b)); +assert (!l_uintset_isempty (c)); + +l_uintset_free (c); +l_uintset_free (b); +} +int main (int argc,char *argv[]) {l_test_init (&argc, &argv);
@@ -364,6 +380,7 @@ int main(int argc, char *argv[]) &intersect_data_1);l_test_add ("l_uintset intersect test 2" , test_uintset_intersect_test, &intersect_data_2); +l_test_add ("l_uintset isempty" , test_uintset_isempty,NULL );return l_test_run (); }
Signature · 1 line
2.20.1
PATCH 2/2 uintset: Do not exceed max offset in empty check
In reply to Ossama OthmanDo not exceed the maximum l_uintset bits offset when checking if the set is empty.
--- ell/uintset.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
ell/uintset.c +4 -1
@@ -516,11 +516,14 @@ LIB_EXPORT struct l_uintset *l_uintset_intersect(const struct l_uintset *set_a, LIB_EXPORT bool l_uintset_isempty(const struct l_uintset *set) {uint16_t i; +uint32_t offset_max;if (unlikely (!set))return true; -for (i =0 ; i < set->size; i++) { + offset_max = (set->size +BITS_PER_LONG -1 ) /BITS_PER_LONG ; + +for (i =0 ; i < offset_max; i++) {if (set->bits[i])return false; }
Signature · 1 line
2.20.1
Hi Ossama,
Quoted 6 lines · Ossama Othman
The loop iteration over the 'bits' array in
l_uintset_isempty()exceeded the maximum offset for the givenl_uintset, resulting in out-of-bounds bits being considered for emptiness. Add a test that demonstrates the problem, and correct the maximum offset when iterating over thel_uintset'bits' field.
Quoted 8 lines
Ossama Othman (2):
unit: Addl_uintset_isempty()test
uintset: Do not exceed max offset in empty checkell/uintset.c | 5 ++++- unit/test-uintset.c | 17 +++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-)
Nice catch, both applied.
Regards,
-Denis