diff mbox

Fix valgrind error from gdb.decode_line

Message ID 20200104214656.15488-1-tom@tromey.com
State New
Headers show

Commit Message

Tom Tromey Jan. 4, 2020, 9:46 p.m. UTC
PR symtab/12535 points out that gdb.decode_line("") will cause a
valgrind report.

I think the empty linespec does not really make sense.  So, this patch
changes gdb.decode_line to treat a whitespace-only linespec the same
as a non-existing argument.

gdb/ChangeLog
2020-01-04  Tom Tromey  <tom@tromey.com>

	PR symtab/12535:
	* python/python.c (gdbpy_decode_line): Treat empty string the same
	as no argument.

gdb/testsuite/ChangeLog
2020-01-04  Tom Tromey  <tom@tromey.com>

	PR symtab/12535:
	* gdb.python/python.exp: Test decode_line with empty string
	argument.

Change-Id: I1d95812b4b7a21d69a3e9afd05b9e3141a931897
---
 gdb/ChangeLog                       | 6 ++++++
 gdb/python/python.c                 | 9 +++++++++
 gdb/testsuite/ChangeLog             | 6 ++++++
 gdb/testsuite/gdb.python/python.exp | 4 ++++
 4 files changed, 25 insertions(+)

Comments

Tom Tromey Jan. 15, 2020, 12:37 a.m. UTC | #1
>>>>> "Tom" == Tom Tromey <tom@tromey.com> writes:

Tom> PR symtab/12535 points out that gdb.decode_line("") will cause a
Tom> valgrind report.

I'm checking this in.

Tom
diff mbox

Patch

diff --git a/gdb/python/python.c b/gdb/python/python.c
index bf214fae6e2..74d11b570ac 100644
--- a/gdb/python/python.c
+++ b/gdb/python/python.c
@@ -810,6 +810,15 @@  gdbpy_decode_line (PyObject *self, PyObject *args)
   if (! PyArg_ParseTuple (args, "|s", &arg))
     return NULL;
 
+  /* Treat a string consisting of just whitespace the same as
+     NULL.  */
+  if (arg != NULL)
+    {
+      arg = skip_spaces (arg);
+      if (*arg == '\0')
+	arg = NULL;
+    }
+
   if (arg != NULL)
     location = string_to_event_location_basic (&arg, python_language,
 					       symbol_name_match_type::WILD);
diff --git a/gdb/testsuite/gdb.python/python.exp b/gdb/testsuite/gdb.python/python.exp
index f002497e9d0..a50a7b43e2c 100644
--- a/gdb/testsuite/gdb.python/python.exp
+++ b/gdb/testsuite/gdb.python/python.exp
@@ -234,6 +234,10 @@  gdb_test "python print (len(symtab))" "2" "test decode_line current location"
 gdb_test "python print (symtab\[0\])" "None" "test decode_line expression parse"
 gdb_test "python print (len(symtab\[1\]))" "1" "test decode_line current location"
 
+# Test that decode_line with an empty string argument does not crash.
+gdb_py_test_silent_cmd "python symtab2 = gdb.decode_line('')" \
+    "test decode_line with empty string" 1
+
 if { [is_remote host] } {
     set python_c [string_to_regexp "python.c"]
 } else {