lnstat: fix strdup leak in -w argument parsing
Subsystems (2)
PATCH 1/1 lnstat: fix strdup leak in -w argument parsing
'tmp' string is used for safe tokenizing, but it is not required after getting all the widths in -w option. As 'tmp' string is obtained by strdup call, the caller has to deallocate it to avoid memory leak.
--- misc/lnstat.c | 1 + 1 file changed, 1 insertion(+)
misc/lnstat.c +1 -0
@@ -331,6 +331,7 @@ int main(int argc, char **argv)for (i =0 ; i <MAX_FIELDS ; i++) fp.params[i].print.width = len; } +free (tmp);break ;default :usage (argv[0 ],1 );
Signature · 1 line
2.25.1
Quoted 5 lines · On Tue, 15 Feb 2022 23:53:47 +0300 Maxim Petrov <[email protected]> wrote:
'tmp' string is used for safe tokenizing, but it is not required after getting all the widths in -w option. As 'tmp' string is obtained by strdup call, the caller has to deallocate it to avoid memory leak.
Signed-off-by Maxim Petrov <[email protected]>
Would strdupa() be cleaner/simpler. I have no strong preference.
Quoted 1 line · Stephen Hemminger
Would
strdupa()be cleaner/simpler.
strdupa will not free the allocated memory until the caller returns. In our case the caller is 'main', so despite valgrind will be happy, the memory will be wasted anyway.
However, I guess that the option is mostly used just like '-w 20', so the _wasted anyway_ strdupa memory is only about a few bytes in these cases, and from this perspective I also have no strong preference. But I don't know how this option is used by others.
Quoted 11 lines · On Wed, 16 Feb 2022 22:40:58 +0300 Maxim Petrov <[email protected]> wrote:
Quoted 1 line · Stephen Hemminger
Would
strdupa()be cleaner/simpler.strdupa will not free the allocated memory until the caller returns. In our case the caller is 'main', so despite valgrind will be happy, the memory will be wasted anyway.
However, I guess that the option is mostly used just like '-w 20', so the
_wastedanyway_strdupa memory is only about a few bytes in these cases, and from this perspective I also have no strong preference. But I don't know how this option is used by others.
There are not a lot of paths through the code in main, so not a bit worry.