public inbox for gdb-patches@sourceware.org
 help / color / mirror / Atom feed
* [PATCH] gdb: fix "list ." related crash
@ 2024-01-23 10:50 Guinevere Larsen
  2024-01-23 15:47 ` Tom Tromey
  0 siblings, 1 reply; 3+ messages in thread
From: Guinevere Larsen @ 2024-01-23 10:50 UTC (permalink / raw)
  To: gdb-patches; +Cc: Guinevere Larsen

When a user attempts to use the "list ." command with an inferior that
doesn't have debug symbols, GDB would crash. This was reported as PR
gdb/31256.

The crash would happen when attempting to get the current symtab_and_line
for the stop location, because the symtab would return a null pointer
and we'd attempt to dereference it to print the line.

This commit fixes that by checking for an empty symtab and erroring out
of the function if it happens.

Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31256
---
 gdb/cli/cli-cmds.c                      |  2 ++
 gdb/testsuite/gdb.base/list-nodebug.c   | 21 ++++++++++++++
 gdb/testsuite/gdb.base/list-nodebug.exp | 37 +++++++++++++++++++++++++
 3 files changed, 60 insertions(+)
 create mode 100644 gdb/testsuite/gdb.base/list-nodebug.c
 create mode 100644 gdb/testsuite/gdb.base/list-nodebug.exp

diff --git a/gdb/cli/cli-cmds.c b/gdb/cli/cli-cmds.c
index c1c78344a7e..df11f956245 100644
--- a/gdb/cli/cli-cmds.c
+++ b/gdb/cli/cli-cmds.c
@@ -1291,6 +1291,8 @@ list_command (const char *arg, int from_tty)
 	      set_default_source_symtab_and_line ();
 	      cursal = get_current_source_symtab_and_line ();
 	    }
+	  if (cursal.symtab == nullptr)
+	    error (_("No debug information available to print source lines."));
 	  list_around_line (arg, cursal);
 	  /* Set the repeat args so just pressing "enter" after using "list ."
 	     will print the following lines instead of the same lines again. */
diff --git a/gdb/testsuite/gdb.base/list-nodebug.c b/gdb/testsuite/gdb.base/list-nodebug.c
new file mode 100644
index 00000000000..078517c011e
--- /dev/null
+++ b/gdb/testsuite/gdb.base/list-nodebug.c
@@ -0,0 +1,21 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+   Copyright 2024 Free Software Foundation, Inc.
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
+
+int main ()
+{
+    return 0;
+}
diff --git a/gdb/testsuite/gdb.base/list-nodebug.exp b/gdb/testsuite/gdb.base/list-nodebug.exp
new file mode 100644
index 00000000000..08de05423af
--- /dev/null
+++ b/gdb/testsuite/gdb.base/list-nodebug.exp
@@ -0,0 +1,37 @@
+# Copyright 2024 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
+
+# Test that using the command "list" in a file with no debug information
+# will not crash GDB and will give reasonable output.
+
+standard_testfile .c
+
+if {[prepare_for_testing "failed to prepare" ${testfile} ${srcfile} \
+    {nodebug}]} {
+    return -1
+}
+
+if {![runto_main]} {
+    untested "couldn't run to main"
+    return
+}
+
+# Check that GDB doesn't crash when we use list . on an inferior with
+# no debug information
+gdb_test "list ." "No debug.*" "first 'list .'"
+# This should be called twice because the first list invocation since
+# printing a frame may take a different codepath, which wouldn't
+# trigger the crash.
+gdb_test "list ." "No debug.*" "second 'list .'"
-- 
2.43.0


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: fix "list ." related crash
  2024-01-23 10:50 [PATCH] gdb: fix "list ." related crash Guinevere Larsen
@ 2024-01-23 15:47 ` Tom Tromey
  2024-01-23 15:59   ` Guinevere Larsen
  0 siblings, 1 reply; 3+ messages in thread
From: Tom Tromey @ 2024-01-23 15:47 UTC (permalink / raw)
  To: Guinevere Larsen; +Cc: gdb-patches

>>>>> "Guinevere" == Guinevere Larsen <blarsen@redhat.com> writes:

Guinevere> When a user attempts to use the "list ." command with an inferior that
Guinevere> doesn't have debug symbols, GDB would crash. This was reported as PR
Guinevere> gdb/31256.

Guinevere> The crash would happen when attempting to get the current symtab_and_line
Guinevere> for the stop location, because the symtab would return a null pointer
Guinevere> and we'd attempt to dereference it to print the line.

Guinevere> This commit fixes that by checking for an empty symtab and erroring out
Guinevere> of the function if it happens.

Guinevere> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31256

Looks good to me.  Thank you.
I think it's fine to backport this to gdb-14, too, if that's relevant.

Approved-By: Tom Tromey <tom@tromey.com>

Tom

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH] gdb: fix "list ." related crash
  2024-01-23 15:47 ` Tom Tromey
@ 2024-01-23 15:59   ` Guinevere Larsen
  0 siblings, 0 replies; 3+ messages in thread
From: Guinevere Larsen @ 2024-01-23 15:59 UTC (permalink / raw)
  To: Tom Tromey; +Cc: gdb-patches

On 23/01/2024 16:47, Tom Tromey wrote:
>>>>>> "Guinevere" == Guinevere Larsen <blarsen@redhat.com> writes:
> Guinevere> When a user attempts to use the "list ." command with an inferior that
> Guinevere> doesn't have debug symbols, GDB would crash. This was reported as PR
> Guinevere> gdb/31256.
>
> Guinevere> The crash would happen when attempting to get the current symtab_and_line
> Guinevere> for the stop location, because the symtab would return a null pointer
> Guinevere> and we'd attempt to dereference it to print the line.
>
> Guinevere> This commit fixes that by checking for an empty symtab and erroring out
> Guinevere> of the function if it happens.
>
> Guinevere> Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=31256
>
> Looks good to me.  Thank you.
> I think it's fine to backport this to gdb-14, too, if that's relevant.
>
> Approved-By: Tom Tromey <tom@tromey.com>
Thanks for the quick approval. I've pushed to master and will backport 
to gdb-14 as well.

-- 
Cheers,
Guinevere Larsen
She/Her/Hers


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2024-01-23 15:59 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-01-23 10:50 [PATCH] gdb: fix "list ." related crash Guinevere Larsen
2024-01-23 15:47 ` Tom Tromey
2024-01-23 15:59   ` Guinevere Larsen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).