This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
[commit] Fix struct and struct-pointer casts
- From: Daniel Jacobowitz <drow at false dot org>
- To: gdb-patches at sourceware dot org
- Cc: Aleksandar Ristovski <aristovski at qnx dot com>
- Date: Mon, 8 Sep 2008 18:21:26 -0400
- Subject: [commit] Fix struct and struct-pointer casts
This is a fix related to Aleksandar's patch from April. It's been
bugging me for a while but I only today tracked it down.
value_cast_structs returned its argument if the cast failed. The
argument, of course, was of the uncasted type. So one symptom of
the bug was "print (struct One) two" returning a value of type Two.
This only shows up in C++, where TYPE_NAME is set on structs.
The more insidious problem was that value_cast_pointers does not
change the enclosing type if value_cast_structs succeeds - that's a
feature, and an oversight when we broke the code out into a separate
function. So you'd end up with pointers where the enclosing type was
unchanged but the type was changed. value_fetch_lazy reads bytes the
size of the enclosing type. So we could read fewer bytes than
necessary, and print garbage for the rest.
This turned up a related bug; the lack of check_typedef in
value_fetch_lazy meant that sometimes we'd read no bytes at all.
Tested on x86_64 and checked in.
--
Daniel Jacobowitz
CodeSourcery
2008-09-08 Daniel Jacobowitz <dan@codesourcery.com>
* valops.c (value_cast_structs): Return NULL for failure.
(value_cast): Handle NULL from value_cast_structs.
(value_fetch_lazy): Call check_typedef. Remove unused variable.
* gdb.base/structs3.c, gdb.base/structs3.exp: New files.
Index: valops.c
===================================================================
RCS file: /cvs/src/src/gdb/valops.c,v
retrieving revision 1.194
diff -u -p -r1.194 valops.c
--- valops.c 21 Aug 2008 20:13:08 -0000 1.194
+++ valops.c 8 Sep 2008 22:12:34 -0000
@@ -193,7 +193,8 @@ allocate_space_in_inferior (int len)
/* Cast struct value VAL to type TYPE and return as a value.
Both type and val must be of TYPE_CODE_STRUCT or TYPE_CODE_UNION
- for this to work. Typedef to one of the codes is permitted. */
+ for this to work. Typedef to one of the codes is permitted.
+ Returns NULL if the cast is neither an upcast nor a downcast. */
static struct value *
value_cast_structs (struct type *type, struct value *v2)
@@ -244,7 +245,8 @@ value_cast_structs (struct type *type, s
return value_at (type, addr2);
}
}
- return v2;
+
+ return NULL;
}
/* Cast one pointer or reference type to another. Both TYPE and
@@ -397,7 +399,12 @@ value_cast (struct type *type, struct va
if ((code1 == TYPE_CODE_STRUCT || code1 == TYPE_CODE_UNION)
&& (code2 == TYPE_CODE_STRUCT || code2 == TYPE_CODE_UNION)
&& TYPE_NAME (type) != 0)
- return value_cast_structs (type, arg2);
+ {
+ struct value *v = value_cast_structs (type, arg2);
+ if (v)
+ return v;
+ }
+
if (code1 == TYPE_CODE_FLT && scalar)
return value_from_double (type, value_as_double (arg2));
else if (code1 == TYPE_CODE_DECFLOAT && scalar)
@@ -615,9 +622,8 @@ value_fetch_lazy (struct value *val)
if (VALUE_LVAL (val) == lval_memory)
{
CORE_ADDR addr = VALUE_ADDRESS (val) + value_offset (val);
- int length = TYPE_LENGTH (value_enclosing_type (val));
+ int length = TYPE_LENGTH (check_typedef (value_enclosing_type (val)));
- struct type *type = value_type (val);
if (length)
read_memory (addr, value_contents_all_raw (val), length);
}
Index: testsuite/gdb.base/structs3.c
===================================================================
RCS file: testsuite/gdb.base/structs3.c
diff -N testsuite/gdb.base/structs3.c
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.base/structs3.c 8 Sep 2008 22:12:34 -0000
@@ -0,0 +1,42 @@
+/* This testcase is part of GDB, the GNU debugger.
+
+ Copyright 2008
+ Free Software Foundation, Inc.
+
+ This program is free software; you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation; either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <http://www.gnu.org/licenses/>. */
+
+struct One
+{
+ int x;
+};
+
+struct Two
+{
+ struct One one;
+ int x, y;
+};
+
+struct Two two = { { 1 }, 2, 3 };
+
+typedef struct One tOne;
+typedef struct Two tTwo;
+
+tOne *onep = &two.one;
+tTwo *twop = &two;
+
+int main()
+{
+ onep->x = twop->y;
+ return 0;
+}
Index: testsuite/gdb.base/structs3.exp
===================================================================
RCS file: testsuite/gdb.base/structs3.exp
diff -N testsuite/gdb.base/structs3.exp
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ testsuite/gdb.base/structs3.exp 8 Sep 2008 22:12:34 -0000
@@ -0,0 +1,37 @@
+# This testcase is part of GDB, the GNU debugger.
+
+# Copyright 2008 Free Software Foundation, Inc.
+
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+set testfile "structs3"
+set srcfile ${testfile}.c
+set binfile ${objdir}/${subdir}/${testfile}
+
+if { [prepare_for_testing structs3.exp "structs3" "" {debug}] } {
+ return -1
+}
+
+set vhn "\\$\[0-9\]+"
+
+# Check the real contents.
+gdb_test "print two" "$vhn = {one = {x = 1}, x = 2, y = 3}"
+
+# Check through the pointer.
+gdb_test "print *twop" "$vhn = {one = {x = 1}, x = 2, y = 3}"
+
+# Check through a pointer to a smaller type, casted up.
+gdb_test "print *(struct Two *)onep" "$vhn = {one = {x = 1}, x = 2, y = 3}"
+
+gdb_test "print *(tTwo *)onep" "$vhn = {one = {x = 1}, x = 2, y = 3}"