Blame SOURCES/gdb-rhbz1653410-avoid-crash-when-calling-warning-too-early.patch

ce65b8
From FEDORA_PATCHES Mon Sep 17 00:00:00 2001
ce65b8
From: Tom Tromey <tom@tromey.com>
ce65b8
Date: Fri, 5 Oct 2018 14:54:35 -0600
ce65b8
Subject: gdb-rhbz1653410-avoid-crash-when-calling-warning-too-early.patch
ce65b8
ce65b8
;; Fix for 'GDB crashes when running from a deleted directory'
ce65b8
;; (Tom Tromey, RHBZ#1653410)
ce65b8
ce65b8
Avoid crash when calling warning too early
ce65b8
ce65b8
I noticed that if you pass the name of an existing file (not a
ce65b8
directory) as the argument to --data-directory, gdb will crash:
ce65b8
ce65b8
    $ ./gdb -nx  --data-directory  ./gdb
ce65b8
    ../../binutils-gdb/gdb/target.c:590:56: runtime error: member call on null pointer of type 'struct target_ops'
ce65b8
ce65b8
This was later reported as PR gdb/23838.
ce65b8
ce65b8
This happens because warning ends up calling
ce65b8
target_supports_terminal_ours, which calls current_top_target, which
ce65b8
returns nullptr this early.
ce65b8
ce65b8
This fixes the problem by handling this case specially in
ce65b8
target_supports_terminal_ours.  I also changed
ce65b8
target_supports_terminal_ours to return bool.
ce65b8
ce65b8
gdb/ChangeLog
ce65b8
2018-11-08  Tom Tromey  <tom@tromey.com>
ce65b8
ce65b8
	PR gdb/23555:
ce65b8
	PR gdb/23838:
ce65b8
	* target.h (target_supports_terminal_ours): Return bool.
ce65b8
	* target.c (target_supports_terminal_ours): Handle case where
ce65b8
	current_top_target returns nullptr.  Return bool.
ce65b8
ce65b8
gdb/testsuite/ChangeLog
ce65b8
2018-11-08  Tom Tromey  <tom@tromey.com>
ce65b8
ce65b8
	PR gdb/23555:
ce65b8
	PR gdb/23838:
ce65b8
	* gdb.base/warning.exp: New file.
ce65b8
ce65b8
diff --git a/gdb/ChangeLog b/gdb/ChangeLog
ce65b8
--- a/gdb/ChangeLog
ce65b8
+++ b/gdb/ChangeLog
ce65b8
@@ -1,3 +1,11 @@
ce65b8
+2018-11-08  Tom Tromey  <tom@tromey.com>
ce65b8
+
ce65b8
+	PR gdb/23555:
ce65b8
+	PR gdb/23838:
ce65b8
+	* target.h (target_supports_terminal_ours): Return bool.
ce65b8
+	* target.c (target_supports_terminal_ours): Handle case where
ce65b8
+	current_top_target returns nullptr.  Return bool.
ce65b8
+
ce65b8
 2018-08-16  Gary Benson <gbenson@redhat.com>
ce65b8
 
ce65b8
 	PR gdb/13000:
ce65b8
diff --git a/gdb/target.c b/gdb/target.c
ce65b8
--- a/gdb/target.c
ce65b8
+++ b/gdb/target.c
ce65b8
@@ -584,10 +584,16 @@ target_terminal::info (const char *arg, int from_tty)
ce65b8
 
ce65b8
 /* See target.h.  */
ce65b8
 
ce65b8
-int
ce65b8
+bool
ce65b8
 target_supports_terminal_ours (void)
ce65b8
 {
ce65b8
-  return current_top_target ()->supports_terminal_ours ();
ce65b8
+  /* This can be called before there is any target, so we must check
ce65b8
+     for nullptr here.  */
ce65b8
+  target_ops *top = current_top_target ();
ce65b8
+
ce65b8
+  if (top == nullptr)
ce65b8
+    return false;
ce65b8
+  return top->supports_terminal_ours ();
ce65b8
 }
ce65b8
 
ce65b8
 static void
ce65b8
diff --git a/gdb/target.h b/gdb/target.h
ce65b8
--- a/gdb/target.h
ce65b8
+++ b/gdb/target.h
ce65b8
@@ -1576,7 +1576,7 @@ extern int target_remove_breakpoint (struct gdbarch *gdbarch,
ce65b8
 /* Return true if the target stack has a non-default
ce65b8
   "terminal_ours" method.  */
ce65b8
 
ce65b8
-extern int target_supports_terminal_ours (void);
ce65b8
+extern bool target_supports_terminal_ours (void);
ce65b8
 
ce65b8
 /* Kill the inferior process.   Make it go away.  */
ce65b8
 
ce65b8
diff --git a/gdb/testsuite/ChangeLog b/gdb/testsuite/ChangeLog
ce65b8
--- a/gdb/testsuite/ChangeLog
ce65b8
+++ b/gdb/testsuite/ChangeLog
ce65b8
@@ -1,3 +1,9 @@
ce65b8
+2018-11-08  Tom Tromey  <tom@tromey.com>
ce65b8
+
ce65b8
+	PR gdb/23555:
ce65b8
+	PR gdb/23838:
ce65b8
+	* gdb.base/warning.exp: New file.
ce65b8
+
ce65b8
 2018-09-04  Gary Benson <gbenson@redhat.com>
ce65b8
 
ce65b8
 	* gdb.base/batch-exit-status.exp: Use gdb_test_multiple and expect
ce65b8
diff --git a/gdb/testsuite/gdb.base/warning.exp b/gdb/testsuite/gdb.base/warning.exp
ce65b8
new file mode 100644
ce65b8
--- /dev/null
ce65b8
+++ b/gdb/testsuite/gdb.base/warning.exp
ce65b8
@@ -0,0 +1,36 @@
ce65b8
+# Copyright 2018 Free Software Foundation, Inc.
ce65b8
+
ce65b8
+# This program is free software; you can redistribute it and/or modify
ce65b8
+# it under the terms of the GNU General Public License as published by
ce65b8
+# the Free Software Foundation; either version 3 of the License, or
ce65b8
+# (at your option) any later version.
ce65b8
+#
ce65b8
+# This program is distributed in the hope that it will be useful,
ce65b8
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
ce65b8
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
ce65b8
+# GNU General Public License for more details.
ce65b8
+#
ce65b8
+# You should have received a copy of the GNU General Public License
ce65b8
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
ce65b8
+
ce65b8
+# Test that an early warning does not cause a crash.
ce65b8
+
ce65b8
+if {[is_remote host]} {
ce65b8
+    unsupported "warning.exp can only run on local host"
ce65b8
+    return
ce65b8
+}
ce65b8
+
ce65b8
+set tname [standard_temp_file warning]
ce65b8
+set fd [open $tname w]
ce65b8
+puts $fd "anything"
ce65b8
+close $fd
ce65b8
+
ce65b8
+set save $INTERNAL_GDBFLAGS
ce65b8
+set INTERNAL_GDBFLAGS "-nw -nx -data-directory $tname"
ce65b8
+
ce65b8
+gdb_start
ce65b8
+
ce65b8
+# Make sure gdb started up.
ce65b8
+gdb_test "echo 23\\n" "23"
ce65b8
+
ce65b8
+set INTERNAL_GDBFLAGS $save