15
8
Fork
You've already forked zig-spec
2

Clarification about reinterpreting memory through over-sized type #39

Open
opened 2022年03月23日 04:35:35 +01:00 by topolarity · 0 comments
topolarity commented 2022年03月23日 04:35:35 +01:00 (Migrated from github.com)
Copy link

The equivalent of this test in C is UB according to ubsan:

constbytesalign(4)=[_]u8{1,2,3,4};constS=externstruct{a:u16,b:u16,c:u32,};constx=@ptrCast(*constS,&bytes);varp=&x.b;// TODO: Triggers ubsan for CBE, since @sizeOf(S) > @sizeOf(bytes)constexpected=switch(native_endian){.Little=>0x0403,.Big=>0x0304,};tryexpect(p.*==expected);

The problem is that we're reinterpreting memory using a type larger than the underlying object. The load itself does not exceed the region of the underlying object however, so this behavior could conceivably be well-defined in Zig.

Is this UB in Zig?

The equivalent of this test in C is UB according to ubsan: ```zig const bytes align(4) = [_]u8{ 1, 2, 3, 4 }; const S = extern struct { a: u16, b: u16, c: u32, }; const x = @ptrCast(*const S, &bytes); var p = &x.b; // TODO: Triggers ubsan for CBE, since @sizeOf(S) > @sizeOf(bytes) const expected = switch (native_endian) { .Little => 0x0403, .Big => 0x0304, }; try expect(p.* == expected); ``` The problem is that we're reinterpreting memory using a type larger than the underlying object. The load itself does not exceed the region of the underlying object however, so this behavior could conceivably be well-defined in Zig. Is this UB in Zig?
alexrp unlocked this conversation 2025年12月13日 01:58:46 +01:00
Sign in to join this conversation.
No Branch/Tag specified
master
sync
disorganized-spec-chunks
No results found.
Labels
Clear labels
bounty
https://ziglang.org/news/announcing-donor-bounties
bug
Observed behavior contradicts documented or intended behavior.
contributor-friendly
This issue is limited in scope and/or knowledge of project internals.
downstream
An issue with a third-party project that uses this project.
enhancement
Solving this issue will likely involve adding new logic or components to the codebase.
infra
An issue related to project infrastructure, e.g. continuous integration.
optimization
A task to improve performance and/or resource usage.
question
No questions on the issue tracker; use a community space instead.
regression
Something that used to work in a previous version stopped working
upstream
An issue with a third-party project that this project uses.
use case
Describes a real use case that is difficult or impossible, but does not propose a solution.
Milestone
Clear milestone
No items
No milestone
Projects
Clear projects
No items
No project
Assignees
Clear assignees
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
ziglang/zig-spec#39
Reference in a new issue
ziglang/zig-spec
No description provided.
Delete branch "%!s()"

Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?