]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/commitdiff
libxcmd: don't crash if el_gets returns null
authorDarrick J. Wong <darrick.wong@oracle.com>
Sat, 9 May 2020 17:09:44 +0000 (13:09 -0400)
committerEric Sandeen <sandeen@sandeen.net>
Sat, 9 May 2020 17:09:44 +0000 (13:09 -0400)
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>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
libxcmd/input.c

index 203110df7a0b56b152da248b12a3443dad18f3b6..e3fa626a7252223172ca5a00f8b508d076b8e829 100644 (file)
@@ -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