Bug 22777 – stat struct in core.sys.windows.stat assumes CRuntime_DigitalMars
Status
RESOLVED
Resolution
FIXED
Severity
normal
Priority
P1
Component
druntime
Product
D
Version
D2
Platform
All
OS
Windows
Creation time
2022-02-16T02:27:50Z
Last change time
2022-03-04T09:13:45Z
Keywords
pull
Assigned to
No Owner
Creator
duser
Comments
Comment #0 by duser — 2022-02-16T02:27:50Z
test program:
///
import core.sys.windows.stat;
import std.stdio;
struct X { struct_stat sb; ubyte[8] extra; }
void main()
{
X x;
stat(".", &x.sb);
foreach (m; __traits(allMembers, struct_stat))
writefln("%s = %s", m, __traits(getMember, x.sb, m));
writefln("extra = %s", x.extra);
}
///
with "dmd -m32", the output looks normal
with "dmd -m32mscoff" or "dmd -m64" (same output), some of the fields are misaligned and four bytes are written past the end of the struct
Comment #1 by duser — 2022-02-16T02:33:32Z
this seems to be the correct definition of the struct for microsoft C runtime:
struct struct_stat
{
uint st_dev;
ushort st_ino;
ushort st_mode;
short st_nlink;
short st_uid;
short st_gid;
uint st_rdev;
int st_size;
int st_atime;
int st_mtime;
int st_ctime;
}
assembled from wine's msvcrt sources at https://github.com/wine-mirror/wine/blob/e909986/include/msvcrt/sys/stat.h
dlang/druntime pull request #3763 "fix Issue 22777 - stat struct in core.sys.windows.stat assumes CRunti…" was merged into stable:
- 389bbd18960be9d65e88eb314ee4501d191cba07 by human:
fix Issue 22777 - stat struct in core.sys.windows.stat assumes CRuntime_DigitalMars
the new struct is based on `struct _stat` from https://github.com/wine-mirror/wine/blob/e909986/include/msvcrt/sys/stat.h#L58
v2: added pragma(mangle) to the functions for compatibility with newer C runtimes whose default stat struct uses 64-bit time_t
v3: made the structs use time_t
v4: added a comment explaining pragmas
v5: updated comment and removed pragmas, seems like plain stat() always uses 32-bit times after all
https://github.com/dlang/druntime/pull/3763