]> git.corax.cc Git - mwm/commitdiff
array: Make array_get() return an error if an element wasn't set
authorMatthias Kruk <m@m10k.eu>
Sun, 2 May 2021 11:39:05 +0000 (20:39 +0900)
committerMatthias Kruk <m@m10k.eu>
Sun, 2 May 2021 11:39:05 +0000 (20:39 +0900)
The array_get() function returns success when an entry is retrieved that
was never set. This makes it hard to distinguish uninitialized entries.
This commit changes array_get() to return an error if the caller is
attempting to retrieve an uninitialized entry.

array.c

diff --git a/array.c b/array.c
index 3a143ded3f429210f612a4fc620313988fd83d69..325e68b14a2eac2e2eb4289acd5f206dd2dd374f 100644 (file)
--- a/array.c
+++ b/array.c
@@ -208,8 +208,12 @@ int array_get(struct array *array, int idx, void **data)
        }
 
        real_idx = idx % array->size;
-       *data = array->data[real_idx];
 
+       if(!array->flags[real_idx]) {
+               return(-ENOENT);
+       }
+
+       *data = array->data[real_idx];
        return(0);
 }