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

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