From 8663f98b156ab3a9c146c418ed93e157da3e39a1 Mon Sep 17 00:00:00 2001 From: Matthias Kruk Date: Sun, 2 May 2021 20:39:05 +0900 Subject: [PATCH] array: Make array_get() return an error if an element wasn't set 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 | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/array.c b/array.c index 3a143de..325e68b 100644 --- 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); } -- 2.47.3