D issues are now tracked on GitHub. This Bugzilla instance remains as a read-only archive.
Issue 22326 - ImportC: struct with flexible array member is incorrectly handled
Summary: ImportC: struct with flexible array member is incorrectly handled
Status: RESOLVED FIXED
Alias: None
Product: D
Classification: Unclassified
Component: dmd (show other issues)
Version: D2
Hardware: All All
: P1 major
Assignee: No Owner
URL:
Keywords: ImportC, pull, wrong-code
Depends on:
Blocks:
 
Reported: 2021-09-21 03:19 UTC by dave287091
Modified: 2021-09-30 01:46 UTC (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this issue.
Description dave287091 2021-09-21 03:19:37 UTC
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).
Comment 1 Dlang Bot 2021-09-22 08:23:02 UTC
@WalterBright created dlang/dmd pull request #13095 "fix Issue 22326 - ImportC: struct with flexible array member is incor…" fixing this issue:

- fix Issue 22326 - ImportC: struct with flexible array member is incorrectly handled

https://github.com/dlang/dmd/pull/13095
Comment 2 Dlang Bot 2021-09-24 08:03:12 UTC
dlang/dmd pull request #13095 "fix Issue 22326 - ImportC: struct with flexible array member is incor…" was merged into stable:

- 0494c010c3d621a08e24715c33980fd3cedb8675 by Walter Bright:
  fix Issue 22326 - ImportC: struct with flexible array member is incorrectly handled

https://github.com/dlang/dmd/pull/13095
Comment 3 Dlang Bot 2021-09-30 01:46:15 UTC
dlang/dmd pull request #13108 "merge stable" was merged into master:

- 03c69439f8525ec0b32f76463e86adc74c6c6383 by Walter Bright:
  fix Issue 22326 - ImportC: struct with flexible array member is incorrectly handled (#13095)

https://github.com/dlang/dmd/pull/13108