]> www.infradead.org Git - users/hch/xfsprogs.git/commitdiff
xfs_db: don't crash if el_gets returns null
authorDarrick J. Wong <darrick.wong@oracle.com>
Tue, 26 May 2020 18:36:03 +0000 (14:36 -0400)
committerEric Sandeen <sandeen@sandeen.net>
Tue, 26 May 2020 18:36:03 +0000 (14:36 -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: Eric Sandeen <sandeen@redhat.com>
Reviewed-by: Christoph Hellwig <hch@lst.de>
Signed-off-by: Eric Sandeen <sandeen@sandeen.net>
db/input.c

index 553025bc926212eadf9855fc3fd2b8eb62911002..448e84b09c9cba473f082946124be1276ed40424 100644 (file)
@@ -230,14 +230,21 @@ fetchline(void)
        }
 
        if (inputstacksize == 1) {
-               line = xstrdup(el_gets(el, &count));
-               if (line) {
-                       if (count > 0)
-                               line[count-1] = '\0';
-                       if (*line) {
-                               history(hist, &hevent, H_ENTER, line);
-                               logprintf("%s", line);
-                       }
+               const char      *cmd;
+
+               cmd = el_gets(el, &count);
+               if (!cmd)
+                       return NULL;
+
+               line = xstrdup(cmd);
+               if (!line)
+                       return NULL;
+
+               if (count > 0)
+                       line[count-1] = '\0';
+               if (*line) {
+                       history(hist, &hevent, H_ENTER, line);
+                       logprintf("%s", line);
                }
        } else {
                line = fetchline_internal();