This is the mail archive of the archer@sourceware.org mailing list for the Archer project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[python] Verify if given var name exists, and plug memory leak.


Hi,

frapy_read_var_frame was leaking the var_name buffer. Also, it didn't
check the return value of lookup_symbol.

Also, update testcase to test functions and methods which weren't being
tested.

Committed.

gdb/
	* python/python-frame.c (frapy_read_var_value): Don't leak variable
	name, and verify if variable with given name exists.

gdb/testsuite/
	* gdb.python/python-frame.exp: Add tests for Frame.read_var_value,
	gdb.newest_frame, gdb.selected_frame and
	gdb.frame_unwind_stop_reason_string.

diff --git a/gdb/python/python-frame.c b/gdb/python/python-frame.c
index 1f152b8..131aad1 100644
--- a/gdb/python/python-frame.c
+++ b/gdb/python/python-frame.c
@@ -413,20 +413,32 @@ frapy_read_var_value (PyObject *self, PyObject *args)
   else if (gdbpy_is_string (sym_obj))
     {
       char *var_name;
-      struct block *block;
+      struct block *block = NULL;
+      struct cleanup *cleanup;
       volatile struct gdb_exception except;
 
+      var_name = python_string_to_target_string (sym_obj);
+      cleanup = make_cleanup (xfree, var_name);
+
       TRY_CATCH (except, RETURN_MASK_ALL)
 	{
-	  struct block *block;
-
 	  FRAPY_REQUIRE_VALID ((frame_object *) self, frame);
 
 	  block = block_for_pc (get_frame_address_in_block (frame));
-	  var = lookup_symbol (python_string_to_target_string (sym_obj), block,
-			       VAR_DOMAIN, NULL);
+	  var = lookup_symbol (var_name, block, VAR_DOMAIN, NULL);
 	}
       GDB_PY_HANDLE_EXCEPTION (except);
+
+      if (!var)
+	{
+	  PyErr_Format (PyExc_ValueError,
+			_("variable '%s' not found"), var_name);
+	  do_cleanups (cleanup);
+
+	  return NULL;
+	}
+
+      do_cleanups (cleanup);
     }
   else
     {
diff --git a/gdb/testsuite/gdb.python/python-frame.exp b/gdb/testsuite/gdb.python/python-frame.exp
index cfa2226..f62f63d 100644
--- a/gdb/testsuite/gdb.python/python-frame.exp
+++ b/gdb/testsuite/gdb.python/python-frame.exp
@@ -63,6 +63,7 @@ if ![runto_main] then {
 
 gdb_breakpoint "f2"
 gdb_continue_to_breakpoint "breakpoint at f2"
+gdb_test "up" "" ""
 
 gdb_py_test_silent_cmd "python frames = gdb.frames ()" "get frames list" 1
 gdb_test "python print frames" "\\(<gdb.Frame object at 0x\[\[:xdigit:\]\]+>, <gdb.Frame object at 0x\[\[:xdigit:\]\]+>, <gdb.Frame object at 0x\[\[:xdigit:\]\]+>\\)" "verify frames list"
@@ -75,9 +76,17 @@ gdb_test "python print 'result =', f0.is_valid ()" " = True" "test Frame.is_vali
 gdb_test "python print 'result =', f0.name ()" " = f2" "test Frame.name"
 gdb_test "python print 'result =', f0.type () == gdb.NORMAL_FRAME" " = True" "test Frame.type"
 gdb_test "python print 'result =', f0.unwind_stop_reason () == gdb.FRAME_UNWIND_NO_REASON" " = True" "test Frame.type"
+gdb_test "python print 'result =', gdb.frame_stop_reason_string (gdb.FRAME_UNWIND_INNER_ID)" " = previous frame inner to this frame \\(corrupt stack\\?\\)" "test gdb.frame_stop_reason_string"
 gdb_test "python print 'result =', f0.pc ()" " = \[0-9\]+" "test Frame.pc"
 gdb_test "python print 'result =', f0.addr_in_block ()" " = \[0-9\]+" "test Frame.addr_in_block"
 gdb_test "python print 'result =', f0.older ().equals (f1)" " = True" "test Frame.older"
 gdb_test "python print 'result =', f1.newer ().equals (f0)" " = True" "test Frame.newer"
+gdb_test "python print 'result =', f0.read_var_value ('b')" \
+  "ValueError: variable 'b' not found.*Error while executing Python code." \
+  "test Frame.read_var_value - error"
+gdb_test "python print 'result =', f0.read_var_value ('a')" " = 1" "test Frame.read_var_value - success"
+
+gdb_test "python print 'result =', gdb.newest_frame ().equals (f0)" " = True" "test gdb.newest_frame"
+gdb_test "python print 'result =', gdb.selected_frame ().equals (f1)" " = True" "test gdb.selected_frame"
 
 gdb_test "python print 'result =', f0.block ()" "<gdb.Block object at 0x\[\[:xdigit:\]\]+>" "test Frame.block"



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]