]> git.ipfire.org Git - thirdparty/binutils-gdb.git/commitdiff
Fix PR python/19438, PR python/18393 - initialize dictionaries
authorTom Tromey <tom@tromey.com>
Thu, 19 May 2016 03:19:17 +0000 (21:19 -0600)
committerTom Tromey <tom@tromey.com>
Mon, 23 May 2016 16:08:34 +0000 (10:08 -0600)
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  <tom@tromey.com>

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  <tom@tromey.com>

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

gdb/ChangeLog
gdb/python/py-objfile.c
gdb/python/py-progspace.c
gdb/testsuite/ChangeLog
gdb/testsuite/gdb.python/py-objfile.exp
gdb/testsuite/gdb.python/py-progspace.exp

index 9239665feabbad0d77bee9bb76b2c5a614a7691a..07bc5d20dc16a67ba55b92482d8a9a0182a88856 100644 (file)
@@ -1,3 +1,9 @@
+2016-05-23  Tom Tromey  <tom@tromey.com>
+
+       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  <gbenson@redhat.com>
 
        * nat/gdb_thread_db.h (td_thr_validate_ftype): Remove typedef.
index cd26c5b49cd1261b262828690be7d757b00134e2..82df4b20199fde61aa53f54a8eb7639fb9ed61da 100644 (file)
@@ -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)
index e1258c76254c4568da6848a2e5f7d0e2396b3c2d..6fc53cb95d33724f9c2eb2a1d372b7e906b5545d 100644 (file)
@@ -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)
index 2aadae090ae5e9f368a498c2593d5b459afe98dd..1f0e3f99f59fb37bd3e818ece2ac6682825028dd 100644 (file)
@@ -1,3 +1,9 @@
+2016-05-23  Tom Tromey  <tom@tromey.com>
+
+       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  <yao.qi@linaro.org>
 
        * gdb.arch/thumb-prologue.exp: Use standard_testfile.
index 1bbb4cf20ba9f00b983487cdaba6c00937bd2c0e..62ca309198372d7bc4522c803fac55499a8d3d31 100644 (file)
@@ -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\"))" \
index 5c1db1c645d792ea98a2e8c4ae8286d1ba86f18d..396d0f2957cb6493d90b83ca05db19965ab179b3 100644 (file)
@@ -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()" \