[patch] fix for crash in python pretty-printer

Paul Pluzhnikov
2009-07-07T18:01:49Z

Greetings,

This is rather on the obvious side.
While debugging a buggy pretty-printer, GDB crashed on me:

Program received signal SIGSEGV, Segmentation fault.
0x000000000045db13 in do_my_cleanups (pmy_chain=0xa7c020, old_chain=0x1e65a10) at ../../gdb/utils.c:390
390 *pmy_chain = ptr->next; /* Do this first incase recursion */

ptr is NULL at that point.

(top) bt
#0 0x000000000045db13 in do_my_cleanups (pmy_chain=0xa7c020, old_chain=0x1e65a10) at ../../gdb/utils.c:390
#1 0x000000000045dadf in do_cleanups (...) at ../../gdb/utils.c:374
#2 0x00000000004d684b in apply_val_pretty_printer (...) at ../../gdb/python/python-prettyprint.c:526
#3 0x000000000051286a in value_print (...) at ../../gdb/valprint.c:388
#4 0x0000000000514f53 in print_formatted (...) at ../../gdb/printcmd.c:316
...

Attached is a fix (applies both to mainline and archer-tromey-python).
Tested on Linux/x86_64 with no regressions.

P.S. Is there an "easy" way to find such mis-uses of TRY_CATCH?
This one took me 2 hours to find :-(

Thanks,
--
Paul Pluzhnikov

2009-07-07 Paul Pluzhnikov <ppluzhnikov@google.com>

* python/python-value.c (valpy_getitem): Don't return from TRY_CATCH.

diff --git a/gdb/python/python-value.c b/gdb/python/python-value.c
index 8c85ef6..489b65b 100644
--- a/gdb/python/python-value.c
+++ b/gdb/python/python-value.c
@@ -290,8 +290,7 @@ valpy_getitem (PyObject *self, PyObject *key)
{
value_object *self_value = (value_object *) self;
char *field = NULL;
- struct value *idx = NULL;
- struct value *res_val = NULL; /* Initialize to appease gcc warning. */
+ struct value *res_val = NULL;
volatile struct gdb_exception except;

if (gdbpy_is_string (key))
@@ -313,12 +312,17 @@ valpy_getitem (PyObject *self, PyObject *key)
value code throw an exception if the index has an invalid
type. */
struct value *idx = convert_value_from_python (key);
- if (idx == NULL)
- return NULL;
-
- res_val = value_subscript (tmp, value_as_long (idx));
+ if (idx != NULL)
+ res_val = value_subscript (tmp, value_as_long (idx));
}
}
+
+ if (res_val == NULL)
+ {
+ gdb_assert (field == NULL);
+ return NULL;
+ }
+
if (field)
xfree (field);
GDB_PY_HANDLE_EXCEPTION (except);