bug in bit field treatment

Boris Tobotras tobotras@jet.msk.su
Thu Jan 24 02:30:00 GMT 2002


	This sample shows the bug:

#include <stdlib.h>

struct test {
        char q1: 4,
                q2: 4;
        int q3;
};

main( void )
{
        struct test *q = ( test * )
                ((( char * ) malloc( sizeof( struct     test ) * 10 )) + 2);
        q->q1 = 1;
        q->q2 = 2;
}

	Verified on at least both SPARC and Alpha, gcc accesses q1/q2 as 
aligned on int boundary. If however you change int q3 to be char, bug is 
disappeared. SPARC assembly code for q->q1 = 1 follows:

        ld      [%fp-20], %o2
        ld      [%o2], %o1
        sethi   %hi(268434432), %o0
        or      %o0, 1023, %o0
        and     %o1, %o0, %o0
        sethi   %hi(268435456), %o1
        or      %o0, %o1, %o0
        st      %o0, [%o2]

	Second ld craches the program. With "char q3", however, it 
becomes:

        ld      [%fp-20], %o1
        ldub    [%o1], %o0
        and     %o0, 15, %o0
        or      %o0, 16, %o0
        stb     %o0, [%o1]

	which is right. Tested with 2.95.3 and 3.0.2. Thanks,
-- 
	Best regards, -- Boris.




More information about the Gcc-bugs mailing list