public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] libxcmd: don't crash if el_gets returns null
@ 2020-04-27 17:07 Darrick J. Wong
  2020-04-27 19:32 ` Christoph Hellwig
  0 siblings, 1 reply; 2+ messages in thread
From: Darrick J. Wong @ 2020-04-27 17:07 UTC (permalink / raw)
  To: sandeen; +Cc: Christoph Hellwig, linux-xfs

From: Darrick J. Wong <darrick.wong@oracle.com>

el_gets returns NULL if it fails to read any characters (due to EOF or
errors occurred).  strdup will crash if it is fed a NULL string, so
check the return value to avoid segfaulting.

Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>
---
 libxcmd/input.c |   20 +++++++++++++-------
 1 file changed, 13 insertions(+), 7 deletions(-)

diff --git a/libxcmd/input.c b/libxcmd/input.c
index 137856e3..a4548d7c 100644
--- a/libxcmd/input.c
+++ b/libxcmd/input.c
@@ -47,6 +47,7 @@ fetchline(void)
 	static EditLine	*el;
 	static History	*hist;
 	HistEvent	hevent;
+	const char	*cmd;
 	char		*line;
 	int		count;
 
@@ -59,13 +60,18 @@ fetchline(void)
 		el_set(el, EL_PROMPT, el_get_prompt);
 		el_set(el, EL_HIST, history, (const char *)hist);
 	}
-	line = strdup(el_gets(el, &count));
-	if (line) {
-		if (count > 0)
-			line[count-1] = '\0';
-		if (*line)
-			history(hist, &hevent, H_ENTER, line);
-	}
+	cmd = el_gets(el, &count);
+	if (!cmd)
+		return NULL;
+
+	line = strdup(cmd);
+	if (!line)
+		return NULL;
+
+	if (count > 0)
+		line[count-1] = '\0';
+	if (*line)
+		history(hist, &hevent, H_ENTER, line);
 	return line;
 }
 #else

^ permalink raw reply related	[flat|nested] 2+ messages in thread

* Re: [PATCH] libxcmd: don't crash if el_gets returns null
  2020-04-27 17:07 [PATCH] libxcmd: don't crash if el_gets returns null Darrick J. Wong
@ 2020-04-27 19:32 ` Christoph Hellwig
  0 siblings, 0 replies; 2+ messages in thread
From: Christoph Hellwig @ 2020-04-27 19:32 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: sandeen, Christoph Hellwig, linux-xfs

On Mon, Apr 27, 2020 at 10:07:30AM -0700, Darrick J. Wong wrote:
> From: Darrick J. Wong <darrick.wong@oracle.com>
> 
> el_gets returns NULL if it fails to read any characters (due to EOF or
> errors occurred).  strdup will crash if it is fed a NULL string, so
> check the return value to avoid segfaulting.
> 
> Signed-off-by: Darrick J. Wong <darrick.wong@oracle.com>

Looks good:

Reviewed-by: Christoph Hellwig <hch@lst.de>

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-04-27 19:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-04-27 17:07 [PATCH] libxcmd: don't crash if el_gets returns null Darrick J. Wong
2020-04-27 19:32 ` Christoph Hellwig

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox