lsb

offsetof cannot be evaluated at compile time: not an integral constant-expression

Bug #1327856 reported by Jeff Johnson
6
This bug affects 1 person
Affects Status Importance Assigned to Milestone
lsb
Fix Committed
Medium
Unassigned
Mandriva
Fix Released
Medium

Bug Description

LSB SDK declares the offsetof macro in the following way:

#define offsetof(TYPE,MEMBER) ((size_t)&((TYPE*)0)->MEMBER)

In general, this works fine, but there are some specific cases when such
implementation can lead to compilation errors. In particular, the following
example will compile with LSB SDK using gcc 3.x and fail if gcc 4.x is used:

/********************************/
struct testcase {
    char array[256];
};

int main(void) {
    char buffer[ offsetof( struct testcase, array[0])];
    return 0;
}
/********************************/

error: size of array ‘buffer’ is not an integral constant-expression

This is actually a break between gcc-3* and gcc-4* which was introduced after
fixing this issue:
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=16711

And in gcc 4.x, offsetof is declared in the following way:
#define offsetof(TYPE, MEMBER) __builtin_offsetof (TYPE, MEMBER)

Not sure how this can be handled in LSB SDK; the first idea that comes to my
mind is to simply check if we are compiling with gcc 4.x and provide a
different offsetof declaration in this case.

However, in most cases the current offsetof implementation works fine, so
probably this is not a critical issue. Finally, there is a simple workaround,
at least for the example above:

int i = offsetof( struct testcase, array[0] );
char buffer[i];

Tags: errata
Changed in mandriva:
importance: Unknown → Medium
status: Unknown → Fix Released
To post a comment you must log in.
This report contains Public information  
Everyone can see this information.

Other bug subscribers

Remote bug watches

Bug watches keep track of this bug in other bug trackers.