[binutils-gdb/gdb-17-branch] Fix gdb.Value.dynamic_type attribute

Hannes Domani ssbssa@sourceware.org
Tue Sep 9 19:26:07 GMT 2025


https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=b37bffccbd5f5ee4b29b43dc900ef45d57d3a556

commit b37bffccbd5f5ee4b29b43dc900ef45d57d3a556
Author: Hannes Domani <ssbssa@yahoo.de>
Date:   Tue Sep 9 19:28:36 2025 +0200

    Fix gdb.Value.dynamic_type attribute
    
    gdb currently crashes if you try to get the dynamic_type from a
    gdb.Value of a POD struct:
    
    (gdb) py print(gdb.parse_and_eval('pod').dynamic_type)
    Fatal signal: Segmentation fault
    
    It happens because value_rtti_type() returns NULL for them, and this is
    not handled correctly.
    
    Fixed by using val->type() as a fallback in this case.
    
    Approved-By: Simon Marchi <simon.marchi@efficios.com>

Diff:
---
 gdb/python/py-value.c                 | 3 +++
 gdb/testsuite/gdb.python/py-value.c   | 1 +
 gdb/testsuite/gdb.python/py-value.exp | 2 ++
 3 files changed, 6 insertions(+)

diff --git a/gdb/python/py-value.c b/gdb/python/py-value.c
index 7cde7a5a327..5d8fab90c04 100644
--- a/gdb/python/py-value.c
+++ b/gdb/python/py-value.c
@@ -478,6 +478,9 @@ valpy_get_dynamic_type (PyObject *self, void *closure)
 	type = value_rtti_type (val, NULL, NULL, NULL);
       else
 	type = val->type ();
+
+      if (type == nullptr)
+	type = val->type ();
     }
   catch (const gdb_exception &except)
     {
diff --git a/gdb/testsuite/gdb.python/py-value.c b/gdb/testsuite/gdb.python/py-value.c
index 5052950ce0d..f6dbf5563e6 100644
--- a/gdb/testsuite/gdb.python/py-value.c
+++ b/gdb/testsuite/gdb.python/py-value.c
@@ -68,6 +68,7 @@ struct Derived : public Base {
 Base *base = new Derived ();
 Derived derived;
 Base &base_ref = derived;
+struct str pod;
 
 void ptr_ref(int*& rptr_int)
 {
diff --git a/gdb/testsuite/gdb.python/py-value.exp b/gdb/testsuite/gdb.python/py-value.exp
index 089bf7563bf..b4e80a21135 100644
--- a/gdb/testsuite/gdb.python/py-value.exp
+++ b/gdb/testsuite/gdb.python/py-value.exp
@@ -458,6 +458,8 @@ proc test_subscript_regression {exefile lang} {
 	 "Derived \[*\]"
      gdb_test "python print (gdb.parse_and_eval('base_ref').dynamic_type)" \
 	 "Derived \[&\]"
+     gdb_test "python print (gdb.parse_and_eval('pod').dynamic_type)" \
+	 "str"
      # A static type case.
      gdb_test "python print (gdb.parse_and_eval('5').dynamic_type)" \
 	 "int"


More information about the Gdb-cvs mailing list