Alignment issue on x86_64?

Rémi Cardona remi.cardona@lri.fr
Mon Jun 23 15:08:00 GMT 2008


Hi all,

I've spent several days trying to figure out why a program would send 
wrong values to an Xlib call (gcc version 4.3.1, ld 2.18 but I've 
reproduced with 4.2.4).

In a nutshell, here's the problem :

void *utils_atom_get_property(
         Display *dpy, Window win, Atom to_get, Atom type, unsigned int 
*size)
{
         unsigned char *retval;
         Atom  type_ret;
         unsigned long  bytes_after, num_ret;
         long length;
         int  format_ret;
         void *data;
         int ok;

         retval = NULL;
         length = 0x7fffffff;
         ok = XGetWindowProperty(
                 dpy, win, to_get, 0L, length, False, type, &type_ret,
                 &format_ret, &num_ret, &bytes_after, &retval);
...


The problem is with the "type" parameter. When utils_atom_get_property 
is called with type=137 :
  - if I break inside that function, "type" is correctly set to 137
  - but if I break inside XGetWindowProperty, "type" changes completely 
and seems to take some of the bytes of the adjacent parameter 
(0x7fff00000089,  when "size" is 0x7fff379c7704, the coincidence is just 
too big IMHO).

Out of sheer frustration, I just shuffled the declarations at the top of 
the function and it "solved" my bug. Here's the patch I applied to my 
own code (no other changes, just moving the order of the declarations):

@@ -41,11 +41,11 @@ static int atom_size(int format)
  void *utils_atom_get_property(
         Display *dpy, Window win, Atom to_get, Atom type, unsigned int 
*size)
  {
-       unsigned char *retval;
         Atom  type_ret;
-       unsigned long  bytes_after, num_ret;
-       long length;
         int  format_ret;
+       long length;
+       unsigned char *retval;
+       unsigned long  bytes_after, num_ret;
         void *data;
         int ok;


I tried reducing my program to a simpler test case, but it's not as 
trivial. Am I looking at a GCC bug? What can I do to further track it 
down? Would *.s output for that particular C file help?

Am I in the right place for such issues?

Thanks in advance for any pointers or help

-- 
Rémi Cardona
LRI, INRIA
remi.cardona@lri.fr
remi@gentoo.org



More information about the Gcc-help mailing list