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

zig segfaults with array and pointer code #1914

Closed
Akuli opened this issue Feb 3, 2019 · 8 comments
Closed

zig segfaults with array and pointer code #1914

Akuli opened this issue Feb 3, 2019 · 8 comments
Labels
bug Observed behavior contradicts documented or intended behavior
Milestone

Comments

@Akuli
Copy link
Contributor

Akuli commented Feb 3, 2019

I'm new to zig, so my code might be doing something that's a horribly bad idea. Anyway, it makes zig crash with no output.

// run with:  zig test nameofthisfile.zig

const A = struct {
    b_list_pointer: *const []B,
};
const B = struct {
    a_pointer: *const A,
};

const b_list: []B = []B{ };
const a = A{ .b_list_pointer = &b_list };

test "segfault bug" {
    const assert = @import("std").debug.assert;
    const obj = B{ .a_pointer = &a };
    assert(obj.a_pointer == &a);    // this makes zig crash
}

Edit: information about versions of things:

akuli@Akuli-Desktop:~$ zig version
0.3.0+bbe857be
akuli@Akuli-Desktop:~$ uname -a
Linux Akuli-Desktop 4.9.0-8-amd64 #1 SMP Debian 4.9.130-2 (2018-10-27) x86_64 GNU/Linux
akuli@Akuli-Desktop:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Devuan
Description:	Devuan GNU/Linux 2.0 (ascii)
Release:	2.0
Codename:	ascii
akuli@Akuli-Desktop:~$ 

I can't try this with the latest master because I'm having some issues with cmake.

@Akuli
Copy link
Contributor Author

Akuli commented Feb 3, 2019

I managed to get cmake and llvm 7 and all the things to work, and turns out the zig on master no longer has this bug.

@Akuli Akuli closed this as completed Feb 3, 2019
@Akuli
Copy link
Contributor Author

Akuli commented Feb 3, 2019

The bug doesn't seem to be quite gone yet, even on latest master. This code makes the compiler crash with zig from latest master (0.3.0+c90c2568):

// run with:  zig test nameofthisfile.zig

const std = @import("std");

const A = struct {
    pointer: *B,
};

pub const B = struct {
    // the one with ? works, the one without ? segfaults zig

    //pointer_array: ?[]*A,
    pointer_array: []*A,
};

var b_value = B { .pointer_array = []*A { } };

test "basic stuff" {
    std.debug.assert(&b_value == &b_value);
}

This seems to have something to do with types that refer to each other. Interestingly, the problem goes away if the reference is made nullable. Is this the idiomatic way to create cyclic-referring things, or is this a bug?

Edit: pub and var don't seem to have anything to do with this problem. The same behaviour happens without pub, and const instead of var.

@Akuli Akuli reopened this Feb 3, 2019
@emekoi
Copy link
Contributor

emekoi commented Feb 3, 2019

possibly related: #1850.

@Akuli
Copy link
Contributor Author

Akuli commented Feb 3, 2019

I don't think that's related. This one is a segfault, not an assertion error.

@andrewrk andrewrk added this to the 0.4.0 milestone Feb 3, 2019
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Feb 3, 2019
@Akuli
Copy link
Contributor Author

Akuli commented Mar 10, 2019

Maybe #1735 is related?

@andrewrk
Copy link
Member

I don't think that's related. This one is a segfault, not an assertion error.

An assertion in a debug build is likely a segfault in a release build.

@andrewrk
Copy link
Member

This is fixed in master branch. I don't know when it was fixed, but I added your tests as additional regression tests, just to be safe.

@Akuli
Copy link
Contributor Author

Akuli commented Mar 25, 2019

I think it might be good to figure out when exactly this got fixed, and what caused it to get fixed. Note that the first code example got "fixed" at some point, but changing it slightly showed that it wasn't quite fixed after all.

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
Projects
None yet
Development

No branches or pull requests

3 participants