From: Darrick J. Wong Date: Sat, 9 May 2020 17:09:44 +0000 (-0400) Subject: libxcmd: don't crash if el_gets returns null X-Git-Tag: xfsprogs-5.7-fixes_2020-06-25~23 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b0c0cdb7ed8ee981e14fab8c23ec86bb2cc64aec;p=thirdparty%2Fxfsprogs-dev.git libxcmd: don't crash if el_gets returns null 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 Reviewed-by: Christoph Hellwig Signed-off-by: Eric Sandeen --- diff --git a/libxcmd/input.c b/libxcmd/input.c index 203110df7..e3fa626a7 100644 --- a/libxcmd/input.c +++ b/libxcmd/input.c @@ -33,6 +33,7 @@ fetchline(void) static EditLine *el; static History *hist; HistEvent hevent; + const char *cmd; char *line; int count; @@ -45,13 +46,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