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

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