Bug 22809 – ImportC: druntime’s definition of __builtin_offsetof leads to dereference of invalid pointer.
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
dmd
Product
D
Version
D2
Platform
All
OS
All
Creation time
2022-02-22T06:52:20Z
Last change time
2022-03-01T09:20:22Z
Keywords
ImportC, pull
Assigned to
No Owner
Creator
dave287091
Comments
Comment #0 by dave287091 — 2022-02-22T06:52:20Z
This C code, using the importc.h from druntime:
typedef unsigned long size_t;
#include "../druntime/import/importc.h"
struct Foo {
int x;
};
int y = __builtin_offsetof(struct Foo, x);
_Static_assert(__builtin_offsetof(struct Foo, x)==0, "");
Expands to:
// off.i
typedef unsigned long size_t;
struct Foo {
int x;
};
int y = ((size_t)((char *)&((struct Foo *)0)->x - (char *)0)); // Error: dereference of invalid pointer `cast(Foo*)0LU`
Static_assert(((size_t)((char *)&((struct Foo *)0)->x - (char *)0))==0, “”);
Which fails to compile.
Additionally, that C code fails to compile with clang, which complains:
off.c:8:16: error: static_assert expression is not an integral constant expression
_Static_assert(__builtin_offsetof(struct Foo, x)==0, "");
^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
./../druntime/import/importc.h:50:33: note: expanded from macro
'__builtin_offsetof'
#define __builtin_offsetof(t,i) ((size_t)((char *)&((t *)0)->i - (char *)0))
^
off.c:8:47: note: cannot access field of null pointer
_Static_assert(__builtin_offsetof(struct Foo, x)==0, "");
^
1 error generated.
Comment #1 by dlang-bot — 2022-03-01T07:50:06Z
@WalterBright created dlang/dmd pull request #13736 "fix Issue 22809 - ImportC: druntime.s definition of __builtin_offseto…" fixing this issue:
- fix Issue 22809 - ImportC: druntime.s definition of __builtin_offsetof leads to dereference of invalid pointer
https://github.com/dlang/dmd/pull/13736
Comment #2 by dlang-bot — 2022-03-01T09:20:22Z
dlang/dmd pull request #13736 "fix Issue 22809 - ImportC: druntime.s definition of __builtin_offseto…" was merged into master:
- 6a451a314256b94599692a6118e6679ebfcceba5 by Walter Bright:
fix Issue 22809 - ImportC: druntime.s definition of __builtin_offsetof leads to dereference of invalid pointer
https://github.com/dlang/dmd/pull/13736