Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ptrCast to packed struct with packed sub-struct not behaving as I would expect #6067

Closed
loris-olsem opened this issue Aug 16, 2020 · 1 comment

Comments

@loris-olsem
Copy link

Observed with latest release 0.6.0+66d76cc4f.

Please see my example below

To me it looks like that the non 16 bit aligned packed sub-struct is causing a problem here.
Writes to the interpreted pointer do not end up in the right locations in memory.
From the documentation this appeared to me the right way to do an interpret cast, or am I missing something here?

Moving all the fields of Cat (including the culprit byte) into Foo makes it behave it again as I would expect.

const std = @import("std");

const Cat = packed struct {
    culprit: u8, // comment in this field and buff will look as expected
    f1: u16,
    f2: u16,
};

const Foo = packed struct {
    cat1: Cat,
};

pub fn main () !void {
    var buff = [_]u8{0} ** (@sizeOf(Foo));
    var foo = @ptrCast(*Foo, &buff);
    foo.cat1.f1 = 0x11;
    foo.cat1.f2 = 0x22;

    var w = std.io.getStdOut().writer();
    // with culprit byte I get 2200000000000000
    // with *no* culprit byte I get, as expected, 11002200
    try w.print("{x}\n", .{buff});
}
@Vexu
Copy link
Member

Vexu commented Aug 16, 2020

Packed structs are known to be buggy, this is likely #2627. #3133 is the tracking issue to fix them.

@Vexu Vexu closed this as completed Aug 16, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants