libxcmd: don't crash if el_gets returns null
PATCH 1/1 libxcmd: don't crash if el_gets returns null
From: Darrick J. Wong <[email protected]>
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 <[email protected]>
--- libxcmd/input.c | 20 +++++++++++++------- 1 file changed, 13 insertions(+), 7 deletions(-)
libxcmd/input.c +13 -7
@@ -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
Quoted 7 lines · Darrick J. Wong
From: Darrick J. Wong <[email protected]>
el_getsreturns 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 <[email protected]>
Looks good:
Reviewed-by Christoph Hellwig <[email protected]>