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


Reply via email to