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

Runtime segfault in .{ SomeStruct { .field = not_comptime_known } } #3915

Closed
ghost opened this issue Dec 15, 2019 · 3 comments
Closed

Runtime segfault in .{ SomeStruct { .field = not_comptime_known } } #3915

ghost opened this issue Dec 15, 2019 · 3 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

@ghost
Copy link

ghost commented Dec 15, 2019

tldr This crashes:

    var yes = true;
    var crashes_at_runtime = .{
        SomeStruct { .is_happy = yes },
    };

But not if yes is a const.


I have an anonymous list/tuple which contains structs. If I try to initialize a field in one of the structs to something that isn't comptime known (e.g. variable, function call), I get a runtime segfault.

const SomeStruct = struct {
    is_happy: bool,
};

fn getTrue() bool {
    return true;
}

pub fn main() void {
    var works0 = [_]SomeStruct {
        SomeStruct { .is_happy = getTrue() },
    };

    var works1 = struct { _0: SomeStruct } {
        ._0 = SomeStruct { .is_happy = getTrue() },
    };

    var works2 = .{
        SomeStruct { .is_happy = true },
    };

    var crashes_at_runtime = .{
        SomeStruct { .is_happy = getTrue() },
    };
}
Segmentation fault at address 0x204371
/home/dbandstra/host/asdf.zig:23:41: 0x22fff1 in main (run)
        SomeStruct { .is_happy = getTrue() },
                                        ^
/home/dbandstra/zig/build/lib/zig/std/special/start.zig:256:22: 0x22ef8b in std.special.posixCallMainAndExit (run)
            root.main();
                     ^
/home/dbandstra/zig/build/lib/zig/std/special/start.zig:130:5: 0x22ee6f in std.special._start (run)
    @call(.{ .modifier = .never_inline }, posixCallMainAndExit, .{});
    ^
Aborted (core dumped)
@andrewrk andrewrk added this to the 0.6.0 milestone Dec 31, 2019
@andrewrk andrewrk added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Dec 31, 2019
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Apr 8, 2020
@andrewrk andrewrk modified the milestones: 0.7.0, 0.8.0 Aug 13, 2020
@ghost
Copy link
Author

ghost commented Sep 28, 2020

related but I think not the same: #4597

@devjobe
Copy link

devjobe commented Mar 8, 2021

The following produces a segmentation fault with 0.7.1 and zig-linux-x86_64-0.8.0-dev.1431+c760532be

    var x : i32 = 1;
    var y = .{ .{ x } };

Looks similar but I've only used zig for a couple of hours.

@andrewrk andrewrk modified the milestones: 0.9.0, 0.10.0 May 19, 2021
@MichaelBelousov
Copy link

MichaelBelousov commented Feb 5, 2022

The following code seems to have the same segfault but only when the runtime value is heap allocated

const E  = union(enum) { e: []const u8 };
const W = union(enum) { w: E };
test "bad" {
    const e = try std.testing.allocator.create(E);
    e.* = E{.e="test"};
    const w = .{ W{ .w=e.* }, }; // SIGSEGV here
    _ = w;
}

if you instead point e to some stack allocated E, it will not segfault at runtime

Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 28, 2022
Vexu added a commit to Vexu/zig that referenced this issue Dec 29, 2022
@andrewrk andrewrk removed this from the 0.12.0 milestone Dec 29, 2022
@andrewrk andrewrk added this to the 0.11.0 milestone Dec 29, 2022
TUSF pushed a commit to TUSF/zig that referenced this issue May 9, 2024
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