Skip to contentSkip to conversation

lib: fix memory leak in getcmdline()

PATCH Applied Mohammad abu saleh 2 participants 2 messages Oct 1 – Oct 2

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 lib: fix memory leak in getcmdline()

getline() may allocate line1 even when it returns -1, and the caller must free the buffer on the error path.

When batch input ends with a missing continuation line, getcmdline() returns without freeing line1. This can be reproduced with:

printf 'link show \\\n' | ./ip/ip -batch -

Free line1 before returning. Valgrind reports 120 bytes definitely lost before this change.

Fixes c2f3a0f94de9 ("Fix batch mode, allow continuation, comments, etc.")
---
 lib/utils.c | 1 +
 1 file changed, 1 insertion(+)
lib/utils.c +1 -0
index 50602e59..6388d981 100644
@@ -1372,6 +1372,7 @@ static ssize_t getcmdline(char **linep, size_t *lenp, FILE *in)
 		cc1 = getline(&line1, &len1, in);
 		if (cc1 < 0) {
 			fprintf(stderr, "Missing continuation line\n");
+			free(line1);
 			return cc1;
 		}
 
Signature · 1 line
2.53.0
#1 lore raw
This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Hello:

This patch was applied to iproute2/iproute2.git (main) by Stephen Hemminger <[email protected]>:

Quoted 9 lines · On Thu, 1 Oct 2026 04:31:29 +0200 you wrote:

getline() may allocate line1 even when it returns -1, and the caller must free the buffer on the error path.

When batch input ends with a missing continuation line, getcmdline() returns without freeing line1. This can be reproduced with:

printf 'link show \\\n' | ./ip/ip -batch -

[...]

Here is the summary with links:
- [iproute2] lib: fix memory leak in getcmdline()

    https://git.kernel.org/pub/scm/network/iproute2/iproute2.git/commit/?id=fde561a2a135

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html