Thread (2 messages) 2 messages, 2 authors, 7d ago

[PATCH iproute] lib: fix leak on realloc() failure

flat view
COOLING7d

From: Stephen Hemminger <stephen@networkplumber.org>
Date: 2026-10-01 18:37:36
Subsystem: library code, the rest · Maintainers: Andrew Morton, Linus Torvalds

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
Keyboard shortcuts
hback out one level
jnext message in thread
kprevious message in thread
ldrill in
Escclose help / fold thread tree
?toggle this help