Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

#6 offsetof() bug and component reference

open
nobody
None
5
2007-07-21
2007-07-21
Anonymous
No

Hello,

When running the below code, it fails with a component reference failure.

#include <stddef.h>
#include <stdio.h>

typedef struct mb {
int l;
union {
char payload[2]; /* actually a bunch of bytes */
double align;

} u;
} mb;

int main(int argc, char **argv)
{
int l;
struct mb c;

l = offsetof(mb, u.payload[0]);

printf("l -> %d\n", l);
}

During run time:

hi2.c:18:Bounds error: NULL or ILLEGAL pointer used in component reference.
hi2.c:18: Pointer value: NULL
Aborted

Applicable gcc version information as below:

gcc -v
Reading specs from /usr/lib/gcc/i686-pc-linux-gnu/3.4.6/specs
Configured with: /var/tmp/portage/sys-devel/gcc-3.4.6-r2/work/gcc-3.4.6/configure --prefix=/usr --bindir=/usr/i686-pc-linux-gnu/gcc-bin/3.4.6 --includedir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.6/include --datadir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.6 --mandir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.6/man --infodir=/usr/share/gcc-data/i686-pc-linux-gnu/3.4.6/info --with-gxx-include-dir=/usr/lib/gcc/i686-pc-linux-gnu/3.4.6/include/g++-v3 --host=i686-pc-linux-gnu --build=i686-pc-linux-gnu --disable-altivec --enable-nls --without-included-gettext --with-system-zlib --disable-checking --disable-werror --enable-secureplt --disable-libunwind-exceptions --disable-multilib --disable-libgcj --with-arch=i686 --enable-languages=c,c++ --enable-shared --enable-threads=posix --enable-__cxa_atexit --enable-clocale=gnu
Thread model: posix
gcc version 3.4.6 (Gentoo Hardened 3.4.6-r2 p1.5, HTB-3.4.4-1.00, ssp-3.4.6-1.0, pie-8.7.10)

(very recent gentoo hardened with a 2.4 kernel)

Discussion