[binutils-gdb] [gdb/exp] Fix invalid cast error
Tom de Vries
vries@sourceware.org
Tue Dec 1 07:37:29 GMT 2026
https://sourceware.org/git/gitweb.cgi?p=binutils-gdb.git;h=5d796c33107c0c1385c7c9e8e305e719c68fc24d
commit 5d796c33107c0c1385c7c9e8e305e719c68fc24d
Author: Tom de Vries <tdevries@suse.de>
Date: Tue Dec 1 08:37:26 2026 +0100
[gdb/exp] Fix invalid cast error
Consider test.c:
...
1 #include <stdio.h>
2
3 int
4 main ()
5 {
6 const char* s = "A";
7 printf ("%s", s);
8 return 0;
9 }
...
compiled with debug info:
...
$ gcc -g test.c
...
If we stop at line 7:
...
$ gdb -q a.out -ex "b 7" -ex run
...
Breakpoint 1, main () at test.c:7
7 printf ("%s", s);
(gdb)
...
and try to print the value of this expression, we get:
...
(gdb) print 1 || (int)strcmp ("", "")
❌ Invalid cast.
...
This is PR exp/35062.
A simpler way to reproduce the same error is this:
...
(gdb) ptype (int)strcmp ("", "")
❌ Invalid cast.
...
The function strcmp is the one from the dynamic linker, which is built from
an assembly file, and has type:
...
(gdb) ptype strcmp
type = void (void)
...
where the void return type represents a DW_TAG_unspecified_type.
The problem only reproduces if glibc debuginfo is installed.
However, we can reproduce this without glibc debuginfo using a test-case:
...
$ gdb -q -batch \
outputs/gdb.dwarf2/dw2-unspecified-type/dw2-unspecified-type \
-ex start \
-ex "ptype (int)foo ()"
...
Invalid cast.
...
The problem happens here in evaluate_subexp_do_call:
...
type *return_type = ftype->target_type ();
if (return_type == NULL)
return_type = default_return_type;
if (return_type == NULL)
error_call_unknown_return_type (function_name);
return value::allocate (return_type);
...
Because ftype->target_type () is a stub void type, the function returns
(void)0. Subsequently, we try to cast the value to int, so we have
(int)(void)0, which causes the "invalid cast" error.
Fix this by applying the same fix as commit a34a90995ae ("[gdb/symtab] Fix
handling of DW_TAG_unspecified_type") did in call_function_by_hand_dummy:
treating "ftype->target_type ()->is_stub ()" the same as
"ftype->target_type () == nullptr":
...
- if (return_type == NULL)
+ if (return_type == nullptr || return_type->is_stub ())
return_type = default_return_type;
...
Tested on x86_64-linux.
Reviewed-By: Keith Seitz <keiths@redhat.com>
Bug: https://sourceware.org/bugzilla/show_bug.cgi?id=35062
Diff:
---
gdb/eval.c | 2 +-
gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp | 6 ++++++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/gdb/eval.c b/gdb/eval.c
index b148828b1a9..4f178c309e9 100644
--- a/gdb/eval.c
+++ b/gdb/eval.c
@@ -646,7 +646,7 @@ evaluate_subexp_do_call (expression *exp, enum noside noside,
type *return_type = ftype->target_type ();
- if (return_type == NULL)
+ if (return_type == nullptr || return_type->is_stub ())
return_type = default_return_type;
if (return_type == NULL)
diff --git a/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp b/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp
index fe0974fd8f4..37e552d02d2 100644
--- a/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp
+++ b/gdb/testsuite/gdb.dwarf2/dw2-unspecified-type.exp
@@ -110,4 +110,10 @@ foreach f {foo bar foo2} {
# Call the function, casting the function result to the correct type.
gdb_test "p (int) $f ()" " = 0"
+
+ # Print the type of the call expression, cast to the correct type.
+ # Printing the type causes the call expression to be evaluated without
+ # side-effects, which triggers another path than the tests above.
+ # Regression test for PR exp/35062. Used to result in "Invalid cast".
+ gdb_test "ptype (int) $f ()" "type = int"
}
More information about the Gdb-cvs
mailing list