Skip to content

Seg-fault when taking gobal tagged union by pointer #11619

@A1Liu

Description

@A1Liu

Zig Version

0.10.0-dev.2137+94b9bcd03

Steps to Reproduce

const std = @import("std");

const A = union(enum) { hello: usize, merp: void };

var global_a: A = .{ .hello = 12 };
var global_usize: usize = 0;

fn doSomethingWithUsize(ptr: *usize) void {
    ptr.* = ptr.* + 1;
    std.debug.print("{}\n", .{ptr.*});
}

pub fn main() void {
    // this works fine
    doSomethingWithUsize(&global_usize);

    // this seg-faults
    switch (global_a) {
        .merp => return,
        .hello => |*value| {
            doSomethingWithUsize(value);
        },
    }
}

Expected Behavior

output:

1
13

Actual Behavior

output:

1
Segmentation fault at address 0x203990
/home/aliu/code/aliu/src/test.zig:9:19: 0x228ee5 in doSomethingWithUsize (play)
    ptr.* = ptr.* + 1;
                  ^
/home/aliu/code/aliu/src/test.zig:21:33: 0x226cd1 in main (play)
            doSomethingWithUsize(value);
                                ^
/snap/zig/5206/lib/std/start.zig:571:22: 0x21ffec in std.start.callMain (play)
            root.main();
                     ^
/snap/zig/5206/lib/std/start.zig:515:12: 0x20730e in std.start.callMainWithArgs (play)
    return @call(.{ .modifier = .always_inline }, callMain, .{});
           ^
/snap/zig/5206/lib/std/start.zig:425:17: 0x2062d6 in std.start.posixCallMainAndExit (play)
    std.os.exit(@call(.{ .modifier = .always_inline }, callMainWithArgs, .{ argc, argv, envp }));
                ^
/snap/zig/5206/lib/std/start.zig:338:5: 0x2060e2 in std.start._start (play)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
The following command terminated unexpectedly:
cd /home/aliu/code/aliu && /home/aliu/code/aliu/zig-cache/o/af290971e2abd3d72663aa1a8e8bf6b3/play
error: the following build command failed with exit code 1:
/home/aliu/code/aliu/zig-cache/o/01695c4cb106cf7038aae3c7d80242f1/build /snap/zig/5206/zig /home/aliu/code/aliu /home/aliu/code/aliu/zig-cache /home/aliu/.cache/zig run

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behaviorstage1The process of building from source via WebAssembly and the C backend.

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions