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 <tomek@cedro.info>
This commit is contained in:
Tomasz 'CeDeROM' CEDRO 2026-08-14 00:16:46 +02:00 committed by Xiang Xiao
parent 2c1bc73261
commit 60a8ddad79

View file

@ -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;
}