Bug 22326 – ImportC: struct with flexible array member is incorrectly handled

Status
RESOLVED
Resolution
FIXED
Severity
major
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2021-09-21T03:19:37Z
Last change time
2021-09-30T01:46:15Z
Keywords
ImportC, pull, wrong-code
Assigned to
No Owner
Creator
dave287091

Comments

Comment #0 by dave287091 — 2021-09-21T03:19:37Z
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 by dlang-bot — 2021-09-22T08:23:02Z
@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 by dlang-bot — 2021-09-24T08:03:12Z
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 by dlang-bot — 2021-09-30T01:46:15Z
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