Bug 22807 – ImportC: Array index is out of bounds for old-style flexible arrays.

Status
RESOLVED
Resolution
FIXED
Severity
minor
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-02-22T06:24:39Z
Last change time
2022-03-04T09:13:01Z
Keywords
ImportC, pull
Assigned to
No Owner
Creator
dave287091

Comments

Comment #0 by dave287091 — 2022-02-22T06:24:39Z
The following C code fails to compile, with a compile time array bounds check error: // oldarray.c struct OldFashionedHeader { int n; // number of entries in buff char buff[1]; }; int peek(OldFashionedHeader *head){ if(head->n < 2) return 0; return head->buff[1]; // Error: array index 1 is out of bounds `(*head).buff[0 .. 1]` } The above pattern is sometimes seen in old C code and old C apis that predate the addition of C99 flexible array members. Technically this code is not strictly correct as it access out of the bounds of its array, but this pattern can be seen in sqlite, windows APIs (such as the definition of https://docs.microsoft.com/en-us/windows/win32/api/wingdi/ns-wingdi-bitmapcoreinfo ), and GCC accepts it as a variant of its zero-length array extension (which also predates flexible array members), as described here: https://gcc.gnu.org/onlinedocs/gcc/Zero-Length.html D code can bypass the check by accessing the .ptr member, but C code isn’t able to do that.
Comment #1 by bugzilla — 2022-03-03T03:21:38Z
Yes, I forgot about that trick. Too bad this check will have to be disabled for ImportC.
Comment #2 by dlang-bot — 2022-03-03T06:54:20Z
@WalterBright created dlang/dmd pull request #13746 "fix Issue 22807 - ImportC: Array index is out of bounds for old-style…" fixing this issue: - fix Issue 22807 - ImportC: Array index is out of bounds for old-style flexible arrays https://github.com/dlang/dmd/pull/13746
Comment #3 by dlang-bot — 2022-03-04T09:13:01Z
dlang/dmd pull request #13746 "fix Issue 22807 - ImportC: Array index is out of bounds for old-style…" was merged into master: - 46afe3c83432ba866cc35ffcb52bebcf23b72803 by Walter Bright: fix Issue 22807 - ImportC: Array index is out of bounds for old-style flexible arrays https://github.com/dlang/dmd/pull/13746