From: Stephen Hemminger <stephen@networkplumber.org>
To: netdev@vger.kernel.org
Cc: Stephen Hemminger <stephen@networkplumber.org>
Subject: [PATCH iproute] lib: fix leak on realloc() failure
Date: Thu, 1 Oct 2026 11:37:31 -0700 [thread overview]
Message-ID: <20261001183731.2282758-1-stephen@networkplumber.org> (raw)
If realloc in the getcommandline failed then the previous
partial command line would leak.
Signed-off-by: Stephen Hemminger <stephen@networkplumber.org>
---
lib/utils.c | 13 +++++++++----
1 file changed, 9 insertions(+), 4 deletions(-)
diff --git a/lib/utils.c b/lib/utils.c
index 6388d981..c4cd3c3d 100644
--- a/lib/utils.c
+++ b/lib/utils.c
@@ -1368,6 +1368,8 @@ static ssize_t getcmdline(char **linep, size_t *lenp, FILE *in)
char *line1 = NULL;
size_t len1 = 0;
ssize_t cc1;
+ char *line2;
+ size_t len2;
cc1 = getline(&line1, &len1, in);
if (cc1 < 0) {
@@ -1383,13 +1385,16 @@ static ssize_t getcmdline(char **linep, size_t *lenp, FILE *in)
if (cp)
*cp = '\0';
- *lenp = strlen(*linep) + strlen(line1) + 1;
- *linep = realloc(*linep, *lenp);
- if (!*linep) {
+ len2 = strlen(*linep) + strlen(line1) + 1;
+ line2 = realloc(*linep, len2);
+ if (line2 == NULL) {
fprintf(stderr, "Out of memory\n");
- *lenp = 0;
+ free(line1);
return -1;
}
+ *linep = line2;
+ *lenp = len2;
+
cc += cc1 - 2;
strcat(*linep, line1);
free(line1);
--
2.53.0
next reply other threads:[~2026-10-01 18:37 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-01 18:37 Stephen Hemminger [this message]
2026-10-02 16:00 ` [PATCH iproute] lib: fix leak on realloc() failure patchwork-bot+netdevbpf
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20261001183731.2282758-1-stephen@networkplumber.org \
--to=stephen@networkplumber.org \
--cc=netdev@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox