From 7b025702b42e0832ea3553bb65661e51fca22d7c Mon Sep 17 00:00:00 2001
From: Morris Jette <jette@schedmd.com>
Date: Tue, 19 Feb 2013 15:27:58 -0800
Subject: [PATCH] Make local readline functions all consistent code

---
 src/sacctmgr/sacctmgr.c | 7 +++++--
 src/scontrol/scontrol.c | 6 ++++--
 src/sreport/sreport.c   | 7 +++++--
 3 files changed, 14 insertions(+), 6 deletions(-)

diff --git a/src/sacctmgr/sacctmgr.c b/src/sacctmgr/sacctmgr.c
index 3f238f29d89..b49d3d18752 100644
--- a/src/sacctmgr/sacctmgr.c
+++ b/src/sacctmgr/sacctmgr.c
@@ -254,6 +254,7 @@ static char *_getline(const char *prompt)
 	char buf[4096];
 	char *line;
 	int len;
+
 	printf("%s", prompt);
 
 	/* Set "line" here to avoid a warning, discard later */
@@ -261,11 +262,13 @@ static char *_getline(const char *prompt)
 	if (line == NULL)
 		return NULL;
 	len = strlen(buf);
-	if ((len > 0) && (buf[len-1] == '\n'))
+	if ((len == 0) || (len >= 4096))
+		return NULL;
+	if (buf[len-1] == '\n')
 		buf[len-1] = '\0';
 	else
 		len++;
-	line = malloc (len * sizeof(char));
+	line = malloc(len * sizeof(char));
 	if (!line)
 		return NULL;
 	return strncpy(line, buf, len);
diff --git a/src/scontrol/scontrol.c b/src/scontrol/scontrol.c
index c8596495c4f..260b6b7e595 100644
--- a/src/scontrol/scontrol.c
+++ b/src/scontrol/scontrol.c
@@ -251,14 +251,16 @@ static char *_getline(const char *prompt)
 
 	/* Set "line" here to avoid a warning, discard later */
 	line = fgets(buf, 4096, stdin);
+	if (line == NULL)
+		return NULL;
 	len = strlen(buf);
-	if (len == 0)
+	if ((len == 0) || (len >= 4096))
 		return NULL;
 	if (buf[len-1] == '\n')
 		buf[len-1] = '\0';
 	else
 		len++;
-	line = malloc (len * sizeof(char));
+	line = malloc(len * sizeof(char));
 	if (!line)
 		return NULL;
 	return strncpy(line, buf, len);
diff --git a/src/sreport/sreport.c b/src/sreport/sreport.c
index f93e53754d8..2c9607a2a7a 100644
--- a/src/sreport/sreport.c
+++ b/src/sreport/sreport.c
@@ -213,6 +213,7 @@ static char *_getline(const char *prompt)
 	char buf[4096];
 	char *line;
 	int len;
+
 	printf("%s", prompt);
 
 	/* Set "line" here to avoid a warning, discard later */
@@ -220,11 +221,13 @@ static char *_getline(const char *prompt)
 	if (line == NULL)
 		return NULL;
 	len = strlen(buf);
-	if ((len > 0) && (buf[len-1] == '\n'))
+	if ((len == 0) || (len >= 4096))
+		return NULL;
+	if (buf[len-1] == '\n')
 		buf[len-1] = '\0';
 	else
 		len++;
-	line = malloc (len * sizeof(char));
+	line = malloc(len * sizeof(char));
 	if (!line)
 		return NULL;
 	return strncpy(line, buf, len);
-- 
GitLab