www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.bugs - [Issue 22326] New: ImportC: struct with flexible array member is


          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 puremagic.com
          Reporter: dave287091 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
 without it). When it does compile, it gives incorrect results. I’m guessing
is treating it as a D dynamic array (which would explain the incorrect size).

Sep 20 2021