This is an automated email from the ASF dual-hosted git repository.

xiaoxiang781216 pushed a commit to branch releases/13.0
in repository https://gitbox.apache.org/repos/asf/nuttx.git


The following commit(s) were added to refs/heads/releases/13.0 by this push:
     new 60a8ddad797 tools/mkversion: Fix missing free in case of error.
60a8ddad797 is described below

commit 60a8ddad7971bf552dbcfc6d284c0cd62fb57bdd
Author: Tomasz 'CeDeROM' CEDRO <[email protected]>
AuthorDate: Fri Aug 14 00:16:46 2026 +0200

    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 <[email protected]>
---
 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 <abs path to .version>\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;
 }

Reply via email to