Nam Nguyen <bits...@gmail.com> added the comment:

Here's a minimal test case for #define bug in LLVM GCC.

If the base struct is 8-byte long or smaller, the code runs correctly.

gcc version 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.1.00)

========
#include <stdio.h>
#include <stdlib.h>

typedef struct {
    int padding;  /* remove this and the sizes will be the same */
    int type;
    int attr;
} struct_a;

typedef struct {
    struct_a base;
    int junk;
} struct_b;

#define BUG(op) \
    ((((struct_a*)(op))->type) ? (void*)((struct_a*)(op) + 1) : \
                                 (void*)((struct_b*)(op) + 1))

static void* func(void* op)
{
    if (((struct_a*)(op))->type)
    {
        return (void*)((struct_a*)(op) + 1);
    }
    return (void*)((struct_b*)(op) + 1);
}

int main(int argc, char** argv)
{
    struct_b* b = malloc(sizeof(struct_b) + 2);
    struct_a* a = (struct_a*)b;
    char* p;
    a->type = 0;
    p = BUG(b);
    printf("expected: %d, actual: %d, b = %p, p = %p\n",
            sizeof(struct_b), p - (char*)b, b, p);
    p = func(b);
    printf("expected: %d, actual: %d, b = %p, p = %p\n",
            sizeof(struct_b), p - (char*)b, b, p);
    return 0;
}
========

$ ./bug
expected: 16, actual: 12, b = 0x10d7008b0, p = 0x10d7008bc
expected: 16, actual: 16, b = 0x10d7008b0, p = 0x10d7008c0

----------

_______________________________________
Python tracker <rep...@bugs.python.org>
<http://bugs.python.org/issue13241>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to