All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/2] uintset: NULL-check before use
@ 2019-03-01  0:51 Tim Kourt
  2019-03-01  0:51 ` [PATCH 2/2] unit: Sanity check for NULL l_uintset Tim Kourt
  0 siblings, 1 reply; 2+ messages in thread
From: Tim Kourt @ 2019-03-01  0:51 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1426 bytes --]

Postpone the variable usage until after the set is NULL-checked.
---
 ell/uintset.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

diff --git a/ell/uintset.c b/ell/uintset.c
index 2b95ac2..1d96e68 100644
--- a/ell/uintset.c
+++ b/ell/uintset.c
@@ -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;
 
-- 
2.13.6


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH 2/2] unit: Sanity check for NULL l_uintset
  2019-03-01  0:51 [PATCH 1/2] uintset: NULL-check before use Tim Kourt
@ 2019-03-01  0:51 ` Tim Kourt
  0 siblings, 0 replies; 2+ messages in thread
From: Tim Kourt @ 2019-03-01  0:51 UTC (permalink / raw)
  To: ell

[-- Attachment #1: Type: text/plain, Size: 1103 bytes --]

---
 unit/test-uintset.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

diff --git a/unit/test-uintset.c b/unit/test-uintset.c
index 35fc935..9f20939 100644
--- a/unit/test-uintset.c
+++ b/unit/test-uintset.c
@@ -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);
 
-- 
2.13.6


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2019-03-01  0:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-03-01  0:51 [PATCH 1/2] uintset: NULL-check before use Tim Kourt
2019-03-01  0:51 ` [PATCH 2/2] unit: Sanity check for NULL l_uintset Tim Kourt

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.