From 60a8ddad7971bf552dbcfc6d284c0cd62fb57bdd Mon Sep 17 00:00:00 2001 From: Tomasz 'CeDeROM' CEDRO Date: Fri, 14 Aug 2026 00:16:46 +0200 Subject: [PATCH] tools/mkversion: Fix missing free in case of error. * According to strdup(3) manual strdup() allocates memory with malloc(3) and that memory should be released with free(3) when no longer needed. * For non existent path or file open error mkversion used exit() with no prior free() for allocated memory. * This change introduces ret variable, exit label, and free on exit in order to avoid potential memory leak. * tools/mkversion is a tiny short-lived utility and the memory gets freed by the OS upon application termination so that was not a bit issue, but now memory leak scanners should be happy as we have free() in pair to strdup(). Reported-by: xjDeng. Signed-off-by: Tomasz 'CeDeROM' CEDRO --- tools/mkversion.c | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/tools/mkversion.c b/tools/mkversion.c index 92b9fc9fc4d..75c251fbc24 100644 --- a/tools/mkversion.c +++ b/tools/mkversion.c @@ -50,7 +50,6 @@ static inline char *getfilepath(const char *name) static void show_usage(const char *progname) { fprintf(stderr, "USAGE: %s \n", progname); - exit(1); } /**************************************************************************** @@ -61,25 +60,29 @@ int main(int argc, char **argv, char **envp) { char *filepath; FILE *stream; + int ret = 0; if (argc != 2) { fprintf(stderr, "Unexpected number of arguments\n"); show_usage(argv[0]); + exit(1); } filepath = getfilepath(argv[1]); - if (!filepath) + if (filepath == NULL) { fprintf(stderr, "getfilepath failed\n"); - exit(2); + ret = 2; + goto exit; } stream = fopen(filepath, "r"); - if (!stream) + if (stream == NULL) { fprintf(stderr, "open %s failed: %s\n", filepath, strerror(errno)); - exit(3); + ret = 3; + goto exit; } printf("/* version.h -- Autogenerated! Do not edit. */\n\n"); @@ -92,8 +95,7 @@ int main(int argc, char **argv, char **envp) printf("#endif /* __INCLUDE_NUTTX_VERSION_H */\n"); fclose(stream); - /* Exit (without bothering to clean up allocations) */ - +exit: free(filepath); - return 0; + return ret; }