This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[RFA] Fix display of array of unspecified length inside structures


The following code shows a
problem in current display of 
char arrays of zero length,
the embedded_offset parameter in c_val_print
gets forgotten, resulting in wrong display.



typedef struct test_struct {
  int x,y;
  char name[0];
} test_t;

test_t * test;

#define TESTNAME "dummy test"

int
main ()
{
  test = alloca (sizeof (test_t) + sizeof (TESTNAME) + 1);
  test->x = 7;
  test->y = -6;
  strcpy (test->name, TESTNAME);

  return 0;
}

  I had to debug gdb using 'set print infrun 1'
to understand that the value of the name filed was 
read at the address of test.x, rather than test.name.

The patch below fixes that problem.
Tested on x86_64-unknown-linux-gnu
no regression found.
  c_val_print and p_val_print functions
are the two only occurrences of this print_unpacked_pointer 'goto',
but I am not sure if this problem can also 
arise for other languages.

  When you look at the source,
you will see that just before the 
print_unpacked_pointer label,
addr is calculated with the use of embedded_offset.




Pierre Muller
GDB pascal language maintainer

PS: It could be wise to add some test in the testsuite for
this, but I have no idea where I could insert this kind of test,
any ideas?
PS2: It is probably impossible to make such a test without
alloca or some other memory allocation function, no?
Are there any system restriction for this?


2011-02-18  Pierre Muller  <muller@ics.u-strasbg.fr>

	* c-valprint.c (c_val_print): Add embedded_offset to address
	for arrays of unspecified length.
	* p-valprint.c (pascal_val_print): Likewise.

Index: src/gdb/c-valprint.c
===================================================================
RCS file: /cvs/src/src/gdb/c-valprint.c,v
retrieving revision 1.85
diff -u -p -r1.85 c-valprint.c
--- src/gdb/c-valprint.c	14 Feb 2011 11:33:24 -0000	1.85
+++ src/gdb/c-valprint.c	18 Feb 2011 10:27:41 -0000
@@ -240,7 +240,7 @@ c_val_print (struct type *type, const gd
 	}
       /* Array of unspecified length: treat like pointer to first
 	 elt.  */
-      addr = address;
+      addr = address + embedded_offset;
       goto print_unpacked_pointer;
 
     case TYPE_CODE_MEMBERPTR:
Index: src/gdb/p-valprint.c
===================================================================
RCS file: /cvs/src/src/gdb/p-valprint.c,v
retrieving revision 1.87
diff -u -p -r1.87 p-valprint.c
--- src/gdb/p-valprint.c	14 Feb 2011 11:35:45 -0000	1.87
+++ src/gdb/p-valprint.c	18 Feb 2011 10:27:41 -0000
@@ -128,7 +128,7 @@ pascal_val_print (struct type *type, con
 	  break;
 	}
       /* Array of unspecified length: treat like pointer to first elt.  */
-      addr = address;
+      addr = address + embedded_offset;
       goto print_unpacked_pointer;
 
     case TYPE_CODE_PTR:



Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]