[PATCH v2 1/2] readline_simple: return -1 if getc fails

Gaël PORTAY gael.portay at savoirfairelinux.com
Tue Aug 8 21:13:19 PDT 2017


The getc function may return an errno code if an error happens.

This patch prevents readline from printing a non printable character and
from looping to infinity and beyong.

Signed-off-by: Gaël PORTAY <gael.portay at savoirfairelinux.com>
---
 lib/readline_simple.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lib/readline_simple.c b/lib/readline_simple.c
index c4d3d240e..7dd18e426 100644
--- a/lib/readline_simple.c
+++ b/lib/readline_simple.c
@@ -47,7 +47,7 @@ int readline (const char *prompt, char *line, int len)
 	int	n = 0;				/* buffer index		*/
 	int	plen = 0;			/* prompt length	*/
 	int	col;				/* output column cnt	*/
-	char	c;
+	int	c;
 
 	/* print prompt */
 	if (prompt) {
@@ -58,6 +58,10 @@ int readline (const char *prompt, char *line, int len)
 
 	for (;;) {
 		c = getchar();
+		if (c < 0) {
+			*p = '\0';
+			return (-1);
+		}
 
 		/*
 		 * Special character handling
-- 
2.13.2




More information about the barebox mailing list