From: Sung-hun Kim Date: Thu, 29 Jun 2023 05:41:04 +0000 (+0900) Subject: dump_systemstate-service: Fix a build error on GCC-13 X-Git-Tag: accepted/tizen/unified/dev/20230726.115826~1 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=1b1e49367b5b03d61abf792cf70085e34f862666;p=platform%2Fcore%2Fsystem%2Fcrash-worker.git dump_systemstate-service: Fix a build error on GCC-13 Building on GCC-13 stops on the below build error: [ 16s] [ 64%] Built target test1-default-sleep [ 17s] /home/abuild/rpmbuild/BUILD/crash-worker-7.0.1/src/dump_systemstate-service/dump_systemstate-service.c: In function 'split': [ 17s] /home/abuild/rpmbuild/BUILD/crash-worker-7.0.1/src/dump_systemstate-service/dump_systemstate-service.c:101:25: error: pointer 'result' may be used after 'realloc' [-Werror=use-after-free] [ 17s] 101 | free(result); [ 17s] | ^~~~~~~~~~~~ [ 17s] /home/abuild/rpmbuild/BUILD/crash-worker-7.0.1/src/dump_systemstate-service/dump_systemstate-service.c:92:37: note: call to 'realloc' here [ 17s] 92 | char **result_tmp = realloc(result, (*size + 1) * sizeof(char *)); [ 17s] | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Since the memory area pointed by `result` can be freed by `realloc`, `result` should be freed conditionally. Change-Id: I09ee12f83073665ef37788fa948b72ce3883ea3f Signed-off-by: Sung-hun Kim --- diff --git a/src/dump_systemstate-service/dump_systemstate-service.c b/src/dump_systemstate-service/dump_systemstate-service.c index da234f0..aa71c33 100644 --- a/src/dump_systemstate-service/dump_systemstate-service.c +++ b/src/dump_systemstate-service/dump_systemstate-service.c @@ -92,13 +92,20 @@ char** split(const char *str, size_t *size) char **result_tmp = realloc(result, (*size + 1) * sizeof(char *)); char *chunk_copy = strdup(chunk); - if (result_tmp == NULL || chunk_copy == NULL) { + if (result_tmp == NULL) { _E("Out of memory"); - if (result_tmp != NULL) - free(result_tmp); + if (result != NULL) + free(result); + result = NULL; + if (chunk_copy != NULL) free(chunk_copy); - free(result); + goto out; + } + + if (chunk_copy == NULL) { + _E("Out of memory"); + free(result_tmp); result = NULL; goto out; }