Skip to content

Commit

Permalink
Fix PR python/19438, PR python/18393 - initialize dictionaries
Browse files Browse the repository at this point in the history
This fixes PR python/19438 and PR python/18393.  Both bugs are about
invoking dir() on some Python object implemented by gdb, and getting a
crash.

The crash happens because the dictionary field of these objects was
not initialized.  Apparently what happens is that this field can be
lazily initialized by Python when assigning to an attribute; and it
can also be handled ok when using dir() but without __dict__ defined;
but gdb defines __dict__ because this isn't supplied automatically by
Python.

The docs on this seem rather sparse, but this patch works ok.

An alternative might be to lazily create the dictionary in
gdb_py_generic_dict, but I went with this approach because it seemed
more straightforward.

Built and regtested on x86-64 Fedora 23.

2016-05-23  Tom Tromey  <[email protected]>

	PR python/19438, PR python/18393:
	* python/py-objfile.c (objfpy_initialize): Initialize self->dict.
	* python/py-progspace.c (pspy_initialize): Initialize self->dict.

2016-05-23  Tom Tromey  <[email protected]>

	PR python/19438, PR python/18393:
	* gdb.python/py-progspace.exp: Add "dir" test.
	* gdb.python/py-objfile.exp: Add "dir" test.
  • Loading branch information
tromey committed May 23, 2016
1 parent d9eca1d commit 0f6ed0e
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 2 deletions.
6 changes: 6 additions & 0 deletions gdb/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2016-05-23 Tom Tromey <[email protected]>

PR python/19438, PR python/18393:
* python/py-objfile.c (objfpy_initialize): Initialize self->dict.
* python/py-progspace.c (pspy_initialize): Initialize self->dict.

2016-05-23 Gary Benson <[email protected]>

* nat/gdb_thread_db.h (td_thr_validate_ftype): Remove typedef.
Expand Down
5 changes: 4 additions & 1 deletion gdb/python/py-objfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ static int
objfpy_initialize (objfile_object *self)
{
self->objfile = NULL;
self->dict = NULL;

self->dict = PyDict_New ();
if (self->dict == NULL)
return 0;

self->printers = PyList_New (0);
if (self->printers == NULL)
Expand Down
5 changes: 4 additions & 1 deletion gdb/python/py-progspace.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,10 @@ static int
pspy_initialize (pspace_object *self)
{
self->pspace = NULL;
self->dict = NULL;

self->dict = PyDict_New ();
if (self->dict == NULL)
return 0;

self->printers = PyList_New (0);
if (self->printers == NULL)
Expand Down
6 changes: 6 additions & 0 deletions gdb/testsuite/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
2016-05-23 Tom Tromey <[email protected]>

PR python/19438, PR python/18393:
* gdb.python/py-progspace.exp: Add "dir" test.
* gdb.python/py-objfile.exp: Add "dir" test.

2016-05-23 Yao Qi <[email protected]>

* gdb.arch/thumb-prologue.exp: Use standard_testfile.
Expand Down
2 changes: 2 additions & 0 deletions gdb/testsuite/gdb.python/py-objfile.exp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,8 @@ gdb_test "python print (objfile.filename)" "${testfile}" \
gdb_test "python print (objfile.username)" "${testfile}" \
"Get objfile user name"

gdb_test_no_output "python dir(objfile)"

gdb_test "python print (gdb.lookup_objfile (\"${testfile}\").filename)" \
"${testfile}" "print lookup_objfile filename"
gdb_test "python print (gdb.lookup_objfile (\"junk\"))" \
Expand Down
2 changes: 2 additions & 0 deletions gdb/testsuite/gdb.python/py-progspace.exp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ gdb_test "python print (gdb.current_progspace().filename)" "None" \
"current progspace filename (None)"
gdb_test "python print (gdb.progspaces())" "\\\[<gdb.Progspace object at $hex>\\\]"

gdb_test_no_output "python dir(gdb.current_progspace())"

gdb_load ${binfile}

gdb_py_test_silent_cmd "python progspace = gdb.current_progspace()" \
Expand Down

0 comments on commit 0f6ed0e

Please sign in to comment.