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

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