[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH] readline: fix memory leak in replacement readline.
|
From: |
Nick Bowler |
|
Subject: |
[PATCH] readline: fix memory leak in replacement readline. |
|
Date: |
Tue, 30 May 2023 21:26:19 -0400 |
The getline function allocates memory that has to be freed by the caller
regardless of whether or not the call succeeds. This is the case at
least on current GNU libc getline, and also the case with Gnulib's own
replacement getline implementation.
Gnulib's readline replacement, which calls getline internally, leaks this
memory whenever that call returns -1 (either at EOF or some error).
* lib/readline.c (readline): free allocated memory after getline failure.
---
lib/readline.c | 6 +++++-
1 file changed, 5 insertions(+), 1 deletion(-)
diff --git a/lib/readline.c b/lib/readline.c
index 8e24090c46ad..9ea9b45c81c6 100644
--- a/lib/readline.c
+++ b/lib/readline.c
@@ -30,6 +30,7 @@
#include "readline.h"
#include <stdio.h>
+#include <stdlib.h>
#include <string.h>
char *
@@ -45,7 +46,10 @@ readline (const char *prompt)
}
if (getline (&out, &size, stdin) < 0)
- return NULL;
+ {
+ free(out);
+ return NULL;
+ }
while (*out && (out[strlen (out) - 1] == '\r'
|| out[strlen (out) - 1] == '\n'))
--
2.39.2
- [PATCH] readline: fix memory leak in replacement readline.,
Nick Bowler <=