Uh oh!
There was an error while loading. Please reload this page.
std.mem.zeroes: Zero out entire extern union, including padding - #17286
Conversation
977fc53 to
c3bc290CompareUh oh!
There was an error while loading. Please reload this page.
065afea to
d611c75Comparejayschwa
commented
Sep 27, 2023
@kcbanner, I'm running into a problem with Line 767 in ab3ac1e Lines 4105 to 4108 in ab3ac1e I think there are two ways this could be fixed:
Which way do you think is better? |
jayschwa
commented
Sep 27, 2023
It looks like |
kcbanner
commented
Sep 28, 2023
I think option 1 (make the field an optional pointer) makes sense, that was an oversight on my part when I added it. |
48c5358 to
914063dCompare914063d to
58949b9Comparejayschwa
commented
Oct 1, 2023
This is the change that ended up working. Initializing |
andrewrk
left a comment
There was a problem hiding this comment.
For both extern structs and extern unions, it should be:
varitem: T=undefined;
@memset(asBytes(&item), 0);
returnitem;If I understand correctly, the motivation for making these changes here is that the compiler is handling this perfectly correct code (for structs; extern unions should be changed to this too) in a problematic way.
I suggest, instead of working around this in the standard library, to work around it in the implementation of @memset in the compiler.
kcbanner
commented
Oct 1, 2023
I'm working on this problem here: https://github.com/ziglang/zig/compare/master...kcbanner:zig:extern_union_comptime_memory?expand=1 I'm tracking down one last case with writing to fields of packed unions overwriting too many bits, then I'll PR it. |
I chose to recursively initialize the fields for two reasons:
If you still think plain |
andrewrk
commented
Oct 1, 2023
In Zig, extern structs have well-defined memory layout, which means that they can be used as a bag of bytes that has anything there. It would be illegal behavior if zig code dereferenced one of the non-optional pointers from Therefore I think the meaning of
Zig does not define optional pointers in terms of the C programming language. Zig defines the There is no door open; the Zig language stands on its own and does not depend on the C language specification. |
Okay, I'll change it to plain @kcbanner, should I keep the Windows structure change? Update: I decided to drop that other commit since the Windows structure change will no longer be necessary. |
58949b9 to
929ed11Compareextern struct and extern unionextern union, including padding
Fixes#17258