Skip to content

Segfault when reassigning to a const slice parameter #4015

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

Closed
lukechampine opened this issue Dec 31, 2019 · 1 comment · Fixed by #4975
Closed

Segfault when reassigning to a const slice parameter #4015

lukechampine opened this issue Dec 31, 2019 · 1 comment · Fixed by #4975
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@lukechampine
Copy link
Contributor

The following test should result in a compile-time error, "cannot assign to constant":

pub fn reassign(s: []const u8) void {
    s = s[0..];
}

test "reassign slice" {
    const foo = "foo";
    reassign(foo);
}

Instead, it segfaults on Windows and Linux, and passes (erroneously) on MacOS.

If foo is declared as a var, or if reassign takes a []u8, the test passes (erroneously) instead of segfaulting. Presumably this is because the string is no longer being stored in read-only memory. If the reassignment is changed to s = s, the compiler correctly emits an error. So my guess is that the slice expression is tripping up the "assign to constant" check.

@daurnimator daurnimator 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
@ghost
Copy link

ghost commented Dec 31, 2019

Maybe related #3800

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

Successfully merging a pull request may close this issue.

3 participants