Skip to contentSkip to conversation

lnstat: fix strdup leak in -w argument parsing

PATCH Superseded Maxim Petrov 2 participants 4 messages Feb 15, 2022 – Feb 16, 2022

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

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
index 98904d45..c3293a8e 100644
@@ -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
#1 lore raw
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.

Would strdupa() be cleaner/simpler. I have no strong preference.

#2 lore raw
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.

#3 lore raw
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 _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.

There are not a lot of paths through the code in main, so not a bit worry.