This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
FYI: TRY_CATCH audit for python
- From: Tom Tromey <tromey at redhat dot com>
- To: gdb-patches at sourceware dot org
- Date: Fri, 30 Mar 2012 14:05:29 -0600
- Subject: FYI: TRY_CATCH audit for python
I'm checking this in.
After my patch the other day it occurred to me that perhaps the
TRY_CATCH bug lurked elsewhere in the Python code.
I audited all the TRY_CATCH uses in python/*.c. I found one cleanup
bug, and one spot where moving a declaration could make the code easier
to understand. I also fixed a missing error check in the function with
the bad cleanup use.
Built and regtested on x86-64 F16.
Tom
2012-03-30 Tom Tromey <tromey@redhat.com>
* python/python.c (gdbpy_decode_line): Move cleanup creation out
of TRY_CATCH. Fix error handling.
* python/py-value.c (convert_value_from_python): Move 'old'
declaration to innermost scope.
Index: python/py-value.c
===================================================================
RCS file: /cvs/src/src/gdb/python/py-value.c,v
retrieving revision 1.35
diff -u -r1.35 py-value.c
--- python/py-value.c 22 Mar 2012 08:10:44 -0000 1.35
+++ python/py-value.c 30 Mar 2012 20:04:32 -0000
@@ -1250,7 +1250,6 @@
convert_value_from_python (PyObject *obj)
{
struct value *value = NULL; /* -Wall */
- struct cleanup *old;
volatile struct gdb_exception except;
int cmp;
@@ -1319,6 +1318,8 @@
s = python_string_to_target_string (obj);
if (s != NULL)
{
+ struct cleanup *old;
+
old = make_cleanup (xfree, s);
value = value_cstring (s, strlen (s), builtin_type_pychar);
do_cleanups (old);
Index: python/python.c
===================================================================
RCS file: /cvs/src/src/gdb/python/python.c,v
retrieving revision 1.85
diff -u -r1.85 python.c
--- python/python.c 28 Jan 2012 18:08:22 -0000 1.85
+++ python/python.c 30 Mar 2012 20:04:32 -0000
@@ -503,7 +503,7 @@
appease gcc. */
struct symtab_and_line sal;
const char *arg = NULL;
- char *copy = NULL;
+ char *copy_to_free = NULL, *copy = NULL;
struct cleanup *cleanups;
PyObject *result = NULL;
PyObject *return_result = NULL;
@@ -515,14 +515,14 @@
cleanups = make_cleanup (null_cleanup, NULL);
+ sals.sals = NULL;
TRY_CATCH (except, RETURN_MASK_ALL)
{
if (arg)
{
copy = xstrdup (arg);
- make_cleanup (xfree, copy);
+ copy_to_free = copy;
sals = decode_line_1 (©, 0, 0, 0);
- make_cleanup (xfree, sals.sals);
}
else
{
@@ -532,6 +532,13 @@
sals.nelts = 1;
}
}
+
+ if (sals.sals != NULL && sals.sals != &sal)
+ {
+ make_cleanup (xfree, copy_to_free);
+ make_cleanup (xfree, sals.sals);
+ }
+
if (except.reason < 0)
{
do_cleanups (cleanups);
@@ -575,7 +582,16 @@
}
if (copy && strlen (copy) > 0)
- unparsed = PyString_FromString (copy);
+ {
+ unparsed = PyString_FromString (copy);
+ if (unparsed == NULL)
+ {
+ Py_DECREF (result);
+ Py_DECREF (return_result);
+ return_result = NULL;
+ goto error;
+ }
+ }
else
{
unparsed = Py_None;
@@ -585,13 +601,10 @@
PyTuple_SetItem (return_result, 0, unparsed);
PyTuple_SetItem (return_result, 1, result);
+ error:
do_cleanups (cleanups);
return return_result;
-
- error:
- do_cleanups (cleanups);
- return NULL;
}
/* Parse a string and evaluate it as an expression. */