uintset: NULL-check before use
PATCH v2 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
@@ -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
PATCH v2 2/2 unit: Sanity check for NULL l_uintset
--- unit/test-uintset.c | 8 ++++++++ 1 file changed, 8 insertions(+)
unit/test-uintset.c +8 -0
@@ -120,6 +120,13 @@ static void test_uintset_3(const void *data)l_uintset_free (set); } +static void test_uintset_4 (const void *data) +{ +assert (!l_uintset_take (NULL ,1 )); +assert (!l_uintset_put (NULL ,1 )); +assert (!l_uintset_contains (NULL ,1 )); +} +static void test_uintset_find_unused (const void *data) {struct l_uintset *set;
@@ -167,6 +174,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
Hi Tim,
Quoted 5 lines · Tim Kourt
Postpone the variable usage until after the set is NULL-checked.
--- ell/uintset.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
Both applied, thanks.
Regards,
-Denis