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

Modifying variable through pointer to "anonymous value" crashes program #3204

Closed
oskarnp opened this issue Sep 9, 2019 · 2 comments
Closed
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@oskarnp
Copy link

oskarnp commented Sep 9, 2019

The following code causes program to crash with Segmentation fault on Windows using master (f7721ac). On 0.4.0 program silently crashes without any trace.
No warnings or errors are generated.

const debug = @import("std").debug;

const Foo = struct {
    bla: i32,
};

fn do_this(f: *Foo) void {
    f.bla = 123;
    debug.warn("f = {}\n", f.bla);
}

pub fn main() void {
    do_this(&Foo{ .bla = 3 });
}

My assumption would be that &Foo{.bla = 3} would behave like a compound literal in C99; that is an lvalue with storage duration of enclosing block scope, just like if I had assigned it to a temporary variable.

@andrewrk andrewrk added this to the 0.6.0 milestone Sep 10, 2019
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Sep 10, 2019
@andrewrk
Copy link
Member

andrewrk commented Sep 10, 2019

Thanks for taking the time to file an issue.

This is a missing compile error - Foo{ .bla = 3 } is a constant; generated in the constant data section. The reference to it is incorrectly mutable. The segfault is coming from writing to constant data section which does not have write perms.

@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Feb 13, 2020
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Mar 12, 2020
@Vexu
Copy link
Member

Vexu commented Apr 8, 2020

Duplicate of #2130, fixed by #4971.

./build/a.zig:285:17: error: expected type '*Foo', found '*const Foo'
    do_this(&Foo{ .bla = 3 });
                ^
./build/a.zig:285:17: note: cast discards const qualifier
    do_this(&Foo{ .bla = 3 });
                ^

@Vexu Vexu closed this as completed Apr 8, 2020
@Vexu Vexu modified the milestones: 0.7.0, 0.6.0 Apr 8, 2020
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