[Issue 22326] New: ImportC: struct with flexible array member is incorrectly handled

d-bugmail at puremagic.com d-bugmail at puremagic.com
Tue Sep 21 03:19:37 UTC 2021


https://issues.dlang.org/show_bug.cgi?id=22326

          Issue ID: 22326
           Summary: ImportC: struct with flexible array member is
                    incorrectly handled
           Product: D
           Version: D2
          Hardware: All
                OS: All
            Status: NEW
          Severity: major
          Priority: P1
         Component: dmd
          Assignee: nobody at puremagic.com
          Reporter: dave287091 at gmail.com

C11 states: As a special case, the last element of a structure with more than
one named member may have an incomplete array type; this is called a flexible
array member.

Consider the following C program:

struct S {
    char c;
    int x[]; // flexible array member
};

int printf(const char*, ...);

int main(){
    _Alignas(int) char buff[sizeof(struct S) + sizeof(int[8])];
    struct S* s = (struct S*)buff;
    printf("%zu\n", sizeof(buff)); // prints 56, should print 36 on 64 bit
    printf("%zu\n", sizeof(struct S)); // should print 4, prints 24 on 64bit.
    for(int i = 0; i < 8; i++)
        s->x[i] = i; // program segfaults here
    return 0;
}

I have to compile with -betterC to get it to compile at all (errors with Error:
`tupleof` is not a member of `const(S)`,  Error: invalid `foreach` aggregate
`_error_`
 without it). When it does compile, it gives incorrect results. I’m guessing it
is treating it as a D dynamic array (which would explain the incorrect size).

--


More information about the Digitalmars-d-bugs mailing list