Skip to contentSkip to conversation

uintset: NULL-check before use

PATCH Superseded Tim Kourt 1 participant 2 messages Mar 1, 2019

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Tim Kourt

author
#0 lore raw

PATCH 1/2 uintset: NULL-check before use

Postpone the variable usage until after the set is NULL-checked.

---
 ell/uintset.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)
ell/uintset.c +7 -3
index 2b95ac2..1d96e68 100644
@@ -202,11 +202,13 @@ LIB_EXPORT void l_uintset_free(struct l_uintset *set)
  **/
 LIB_EXPORT bool l_uintset_take(struct l_uintset *set, uint32_t number)
 {
-	uint32_t offset = (number - set->min) / BITS_PER_LONG;
+	uint32_t offset;
 
 	if (unlikely(!set))
 		return false;
 
+	offset = (number - set->min) / BITS_PER_LONG;
+
 	number -= set->min;
 
 	if (number > set->size)
@@ -233,12 +235,13 @@ LIB_EXPORT bool l_uintset_take(struct l_uintset *set, uint32_t number)
  **/
 LIB_EXPORT bool l_uintset_put(struct l_uintset *set, uint32_t number)
 {
-	uint32_t bit = number - set->min;
+	uint32_t bit;
 	uint32_t offset;
 
 	if (unlikely(!set))
 		return false;
 
+	bit = number - set->min;
 	if (bit >= set->size)
 		return false;
 
@@ -257,12 +260,13 @@ LIB_EXPORT bool l_uintset_put(struct l_uintset *set, uint32_t number)
  **/
 LIB_EXPORT bool l_uintset_contains(struct l_uintset *set, uint32_t number)
 {
-	uint32_t bit = number - set->min;
+	uint32_t bit;
 	uint32_t offset;
 
 	if (unlikely(!set))
 		return false;
 
+	bit = number - set->min;
 	if (bit >= set->size)
 		return false;
 
Signature · 1 line
2.13.6
author
#1 lore raw

PATCH 2/2 unit: Sanity check for NULL l_uintset

---
 unit/test-uintset.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
unit/test-uintset.c +15 -0
index 35fc935..9f20939 100644
@@ -120,6 +120,20 @@ static void test_uintset_3(const void *data)
 	l_uintset_free(set);
 }
 
+static void test_uintset_4(const void *data)
+{
+	struct l_uintset *set;
+
+	set = l_uintset_new(2);
+	assert(set);
+
+	assert(!l_uintset_take(NULL, 1));
+	assert(!l_uintset_put(NULL, 1));
+	assert(!l_uintset_contains(NULL, 1));
+
+	l_uintset_free(set);
+}
+
 static void test_uintset_find_unused(const void *data)
 {
 	struct l_uintset *set;
@@ -167,6 +181,7 @@ int main(int argc, char *argv[])
 	l_test_add("l_uintset sanity check", test_uintset, NULL);
 	l_test_add("l_uintset sanity check #2", test_uintset_2, NULL);
 	l_test_add("l_uintset sanity check #3", test_uintset_3, NULL);
+	l_test_add("l_uintset sanity check #4", test_uintset_4, NULL);
 	l_test_add("l_uintset find unused tests", test_uintset_find_unused,
 							NULL);
 
Signature · 1 line
2.13.6