Skip to contentSkip to conversation

Correct l_uintset_isempty() logic

PATCH In review Ossama Othman 2 participants 4 messages Sep 16, 2019

lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 99888b3..3c9790e 100644
@@ -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
#2 lore raw

PATCH 2/2 uintset: Do not exceed max offset in empty check

In reply to Ossama Othman

Do 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
index 50119a0..b88e28e 100644
@@ -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
#3 lore raw
In reply to Ossama Othman

Hi Ossama,

Quoted 6 lines · Ossama Othman

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.

Quoted 8 lines

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(-)

Nice catch, both applied.

Regards,
-Denis