From 307e7ba6e92b1ac97342be471c7a731cef7b4352 Mon Sep 17 00:00:00 2001 From: GreekStapler Date: Tue, 31 Jan 2023 20:41:05 +0000 Subject: [PATCH] fix: Initialise array pointers to 0 before performing check on them. --- src/package/vieter_package_dynarray.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/package/vieter_package_dynarray.c b/src/package/vieter_package_dynarray.c index 2cc20b6..6da07ee 100644 --- a/src/package/vieter_package_dynarray.c +++ b/src/package/vieter_package_dynarray.c @@ -12,12 +12,18 @@ void vieter_package_dynarray_add(vieter_package_dynarray *da, const char *s) { // An empty vieter_package_dynarray does not have an allocated internal array yet if (da->size == 0) { da->array = malloc(sizeof(char*) * da->capacity); + + // Initialise all char*'s to 0 so array[i] == NULL can be used to see if field is empty + memset(da->array, 0, sizeof(char*) * da->capacity); } // Double array size if it's full else if (da->size == da->capacity) { // if the realloc fails, access to memory in da->array is lost da->array = realloc(da->array, sizeof(char*) * da->capacity * 2); da->capacity *= 2; + + // Same as the previous memset, but only for newly allocated pointers + memset(da->array + da->size, 0, sizeof(char*) * da->capacity / 2); } da->array[da->size] = strdup(s);