From 8ac478d0ad55edd322b2a4c6931e452d809bf5f3 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Wed, 11 Sep 2019 13:30:18 -0400 Subject: [PATCH] Tests: fix: leak caused by misuse of realloc in multi-lib-test MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit realloc() can return NULL, in which case 'libraries' will never be free'd in the case where the reallocation happens outside of the while()'s first iteration. Signed-off-by: Jérémie Galarneau --- tests/regression/ust/multi-lib/multi-lib-test.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/tests/regression/ust/multi-lib/multi-lib-test.c b/tests/regression/ust/multi-lib/multi-lib-test.c index e145aa21a..b5b2f3147 100644 --- a/tests/regression/ust/multi-lib/multi-lib-test.c +++ b/tests/regression/ust/multi-lib/multi-lib-test.c @@ -201,12 +201,15 @@ int main(int argc, const char **argv) * Populate the libraries array with the arguments passed to the process. */ while (poptPeekArg(optCon) != NULL) { + char **realloced_libraries = NULL; + nb_libraries++; - libraries = realloc(libraries, nb_libraries * sizeof(char *)); - if (!libraries) { + realloced_libraries = realloc(libraries, nb_libraries * sizeof(char *)); + if (!realloced_libraries) { ret = -1; goto error; } + libraries = realloced_libraries; libraries[nb_libraries - 1] = (char *) poptGetArg(optCon); } -- 2.34.1