From: Thomas Egerer Date: Fri, 6 Sep 2024 11:29:40 +0000 (+0200) Subject: array: Don't use realloc() with zero size in array_compress() X-Git-Tag: 6.0.0rc1~50 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=cf7fb47788dfb83bb5d8bd0bffdb582e381a2f0a;p=thirdparty%2Fstrongswan.git array: Don't use realloc() with zero size in array_compress() The behavior of realloc(3) with zero size was apparently implementation defined. While glibc documents the behavior as equivalent to free(3), that might not apply to other C libraries. With C17, this behavior has been deprecated, and with C23, the behavior is now undefined. It's also why valgrind warns about this use. Hence, when array_compress() would call realloc() with a zero size, we now call free() explicitly and set the pointer to NULL. Signed-off-by: Thomas Egerer --- diff --git a/src/libstrongswan/collections/array.c b/src/libstrongswan/collections/array.c index 8acc8051d5..8b6c6d7397 100644 --- a/src/libstrongswan/collections/array.c +++ b/src/libstrongswan/collections/array.c @@ -197,7 +197,17 @@ void array_compress(array_t *array) } if (tail) { - array->data = realloc(array->data, get_size(array, array->count)); + size_t size = get_size(array, array->count); + + if (size) + { + array->data = realloc(array->data, size); + } + else + { + free(array->data); + array->data = NULL; + } array->tail = 0; } }