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 an rvalue reference of a string literal causes segfault #3444

Closed
marler8997 opened this issue Oct 13, 2019 · 3 comments
Closed

Modifying an rvalue reference of a string literal causes segfault #3444

marler8997 opened this issue Oct 13, 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

@marler8997
Copy link
Contributor

marler8997 commented Oct 13, 2019

Take the following example:

fn popFirstElement(stringRef: *[]const u8) void {
    stringRef.* = stringRef.*[1..];
}

test "bug" {
    popFirstElement(&"foo"[0..]);
}

The &"foo"[0..] expression is taking the address of an rvalue. However, when it is modified in the popFirstElement function this segfault occurs:

1/1 test "bug"...Segmentation fault at address 0x201578
/home/marler8997/git/zog/bug.zig:2:30: 0x205723 in popFirstElement (test)
    stringRef.* = stringRef.*[1..];
                             ^
/home/marler8997/git/zog/bug.zig:6:20: 0x205592 in test "bug" (test)
    popFirstElement(&"foo"[0..]);
                   ^
/home/marler8997/bin/zig-linux-x86_64-0.5.0+8b459216/lib/zig/std/special/test_runner.zig:13:25: 0x2284e1 in std.special.main (test)
        if (test_fn.func()) |_| {
                        ^
/home/marler8997/bin/zig-linux-x86_64-0.5.0+8b459216/lib/zig/std/special/start.zig:204:37: 0x227355 in std.special.posixCallMainAndExit (test)
            const result = root.main() catch |err| {
                                    ^
/home/marler8997/bin/zig-linux-x86_64-0.5.0+8b459216/lib/zig/std/special/start.zig:102:5: 0x2271cf in std.special._start (test)
    @noInlineCall(posixCallMainAndExit);
    ^

Tests failed. Use the following command to reproduce the failure:
/home/marler8997/git/zog/zig-cache/o/lNWDEb7CbHCUpRDUVTsn8ZiwsvJwp9_hF8wwgR91Xl4TNulOrqSQPw1Tzn-dT9B_/test

I'm not sure how rvalue references work in Zig, but I'm guessing that they are supposed to be const. In this case this would mean that the expression &"foo"[0..] should become type *const []const u8 rather than *[]const u8. If this was the case, then this would have been a compile error because you wouldn't be able to pass a const reference to the popFirstElement function.

@marler8997
Copy link
Contributor Author

marler8997 commented Oct 13, 2019

Curiously, this seems to work with integer values. See this example:

fn setZero(numberRef: *u8) void {
    numberRef.* = 0;
}

fn return42() u8 { return 42; }

test "bug" {
   setZero(&return42());
}

Or another way to write it:

fn return42() u8 { return 42; }

test "bug" {
   (&return42()).* = 0;
}

@marler8997
Copy link
Contributor Author

Actually this also works if you don't use a string literal rvalue, but use a function return rvalue instead:

fn popFirstElement(stringRef: *[]const u8) void {
    stringRef.* = stringRef.*[1..];
}

fn returnString() []const u8 {
    return "foo";
}

test "bug" {
    popFirstElement(&returnString());
}

@marler8997 marler8997 changed the title Address of RValue isn't const and causes segfault when modified Modifying address of string literal as an rvalue-reference causes segfault when modified Oct 13, 2019
@marler8997 marler8997 changed the title Modifying address of string literal as an rvalue-reference causes segfault when modified Modifying an rvalue reference of a string literal causes segfault Oct 13, 2019
@andrewrk andrewrk added this to the 0.6.0 milestone Oct 14, 2019
@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Oct 14, 2019
@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Feb 18, 2020
@andrewrk andrewrk modified the milestones: 0.6.0, 0.7.0 Feb 18, 2020
@Vexu
Copy link
Member

Vexu commented Apr 8, 2020

Duplicate of #2130, fixed by #4971.

./build/a.zig:279:27: error: expected type '*[]const u8', found '*const *const [3:0]u8'
    popFirstElement(&"foo"[0..]);
                          ^
./build/a.zig:279:27: note: cast discards const qualifier
    popFirstElement(&"foo"[0..]);
                          ^

@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