Skip to content

segmentation fault when initializing dereferenced struct defined directly in function call with var #12092

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

Closed
viviicat opened this issue Jul 12, 2022 · 0 comments
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@viviicat
Copy link

Zig Version

0.9.0, 0.10.0-dev.2977+7d2e14267

Steps to Reproduce

The following minimal reproduction produces a segfault when assigning baz to the struct. If baz is made const the segfault goes away.

const Foo = struct {
    a: Bar,
};

const Bar = struct {
    b: u32,
};

pub fn doCrash(foo: *const Foo) void {
    _ = foo;
}

test "segfault" {
    var baz: u32 = 24;

    doCrash(&.{
        .a = .{
            .b = baz,
        },
    });
}

Expected Behavior

I would expect this to either fail compilation, or properly assign 24 to Foo.Bar.b.

Actual Behavior

Test [1/1] test "segfault"... Segmentation fault at address 0x20551c
/home/vv/Projects/zig-bugs/zig-minimal-array-pointer-struct.zig:18:18: 0x208292 in test "segfault" (test)
            .b = baz,
                 ^

It's not immediately obvious to me why the segfault occurs. Perhaps it is because the struct memory is actually initialized inside the doCrash stack, so it does not have access to baz at that scope?

More alarmingly, in an expanded example, this pattern actually caused a segfault in the compiler. I have yet to figure out how to create a minimal repro of the compiler crash, but perhaps the runtime crash will assist in resolving that, since they both segfault.

I have reproduced this on master, as well as 0.9.0 on Godbolt

@viviicat viviicat added the bug Observed behavior contradicts documented or intended behavior label Jul 12, 2022
@Vexu Vexu added the stage1 The process of building from source via WebAssembly and the C backend. label Jul 12, 2022
@Vexu Vexu added this to the 0.12.0 milestone Jul 12, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 15, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 15, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 15, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 16, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 17, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 18, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 18, 2022
wooster0 added a commit to wooster0/zig that referenced this issue Dec 21, 2022
@andrewrk andrewrk modified the milestones: 0.12.0, 0.11.0 Dec 27, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Projects
None yet
Development

No branches or pull requests

3 participants