From 2d3293ad7634a8e66725831a4a74b66118fc2d3d Mon Sep 17 00:00:00 2001 From: James Zern Date: Mon, 4 Apr 2022 10:51:13 -0700 Subject: [PATCH] ExUtilInitCommandLineArguments: fix leak on error argv_data would leak if the argv_ allocation failed or the MAX_ARGC cap was hit Tested: for i in `seq 1 171`; do export MALLOC_FAIL_AT=$i ./examples/img2webp jpeg_file -o /dev/null ./examples/img2webp -mixed jpeg_file -o /dev/null done Change-Id: I39864691e96d5456f324d95a3653bba0f6d6a7be --- examples/example_util.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/examples/example_util.c b/examples/example_util.c index b0436541..fa38d3c2 100644 --- a/examples/example_util.c +++ b/examples/example_util.c @@ -103,7 +103,10 @@ int ExUtilInitCommandLineArguments(int argc, const char* argv[], } args->own_argv_ = 1; args->argv_ = (const char**)WebPMalloc(MAX_ARGC * sizeof(*args->argv_)); - if (args->argv_ == NULL) return 0; + if (args->argv_ == NULL) { + ExUtilDeleteCommandLineArguments(args); + return 0; + } argc = 0; for (cur = strtok((char*)args->argv_data_.bytes, sep); @@ -111,6 +114,7 @@ int ExUtilInitCommandLineArguments(int argc, const char* argv[], cur = strtok(NULL, sep)) { if (argc == MAX_ARGC) { fprintf(stderr, "ERROR: Arguments limit %d reached\n", MAX_ARGC); + ExUtilDeleteCommandLineArguments(args); return 0; } assert(strlen(cur) != 0);