Fix PR cli/23785: Check if file exists when invoking "restore FILE binary"
authorSergio Durigan Junior <sergiodj@redhat.com>
Thu, 18 Oct 2018 16:56:25 +0000 (12:56 -0400)
committerSergio Durigan Junior <sergiodj@redhat.com>
Thu, 18 Oct 2018 21:39:09 +0000 (17:39 -0400)
This simple patch fixes the segfault reported on PR cli/23785, which
happens when using the "restore FILE binary" command with a
non-existent file.  We just have to check if the file handler returned
by "gdb_fopen_cloexec" is not NULL, and error out if it is.

A test has also been added to gdb.base/restore.exp in order to
exercise this scenario.

No regressions introduced.

gdb/ChangeLog:
2018-10-18  Sergio Durigan Junior  <sergiodj@redhat.com>

PR cli/23785
* cli/cli-dump.c (restore_binary_file): Check if "file" is
NULL.

gdb/testsuite/ChangeLog:
2018-10-18  Sergio Durigan Junior  <sergiodj@redhat.com>

PR cli/23785
* gdb.base/restore.exp: New test to check if "restore" with an
invalid file doesn't segfault.

gdb/ChangeLog
gdb/cli/cli-dump.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.base/restore.exp

index 81ac022499fd8e3ca7f66246fd95cb22fa9d8ef6..e177cf28caeefe7588cbdff6c1d158fe3a65bbf9 100644 (file)
@@ -1,3 +1,9 @@
+2018-10-18  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       PR cli/23785
+       * cli/cli-dump.c (restore_binary_file): Check if "file" is
+       NULL.
+
 2018-10-17  Paul Koning  <paul_koning@dell.com>
 
        * charset.c (convert_between_encodings): Fix unsigned overflow.
index e9e393cd55bd7b7e63de8857e45cf1e9954d65e4..520c893e0bcc58164cd1470edf3b815c642b9c7a 100644 (file)
@@ -468,6 +468,9 @@ restore_binary_file (const char *filename, struct callback_data *data)
   gdb_file_up file = gdb_fopen_cloexec (filename, FOPEN_RB);
   long len;
 
+  if (file == NULL)
+    error (_("Failed to open %s: %s"), filename, safe_strerror (errno));
+
   /* Get the file size for reading.  */
   if (fseek (file.get (), 0, SEEK_END) == 0)
     {
index 8fa224ac4cdc3a6232c177024a826c9b61797a12..a7bd87d81f2f02475642235373570e440e61425e 100644 (file)
@@ -1,3 +1,9 @@
+2018-10-18  Sergio Durigan Junior  <sergiodj@redhat.com>
+
+       PR cli/23785
+       * gdb.base/restore.exp: New test to check if "restore" with an
+       invalid file doesn't segfault.
+
 2018-10-18  Tom de Vries  <tdevries@suse.de>
 
        * gdb.ada/bp_inlined_func.exp: Fix capitalized test name.
index 995dc6e0b2728bea9117f2b9addc0ba8ca4eee7d..2aa57f6449db354086a95405c9e46c7719f691c2 100644 (file)
@@ -86,3 +86,11 @@ set prev_timeout $timeout
 set timeout 30
 restore_tests
 set timeout $prev_timeout
+
+# Test PR cli/23785
+clean_restart $binfile
+if { ![runto_main] } {
+    return -1
+}
+gdb_test "restore non-existent-file binary" \
+    "Failed to open non-existent-file: .*"
This page took 0.037255 seconds and 4 git commands to generate.