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

missing compile error for address-of constant data #2130

Closed
andrewrk opened this issue Mar 29, 2019 · 4 comments · Fixed by #4971
Closed

missing compile error for address-of constant data #2130

andrewrk opened this issue Mar 29, 2019 · 4 comments · Fixed by #4971
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@andrewrk
Copy link
Member

andrewrk commented Mar 29, 2019

test "sample" {
    var x: []u8 = &[_]u8{'h', 'i'};
    x[0] = 0;
}

Segfaults at runtime. Expected behavior is compile error. Address of constant data should be a const pointer.

Thanks to @allochi for discovering this bug.

@andrewrk andrewrk added the bug Observed behavior contradicts documented or intended behavior label Mar 29, 2019
@andrewrk andrewrk added this to the 0.5.0 milestone Mar 29, 2019
@andrewrk andrewrk modified the milestones: 0.5.0, 0.6.0 Sep 27, 2019
@andrewrk andrewrk added the stage1 The process of building from source via WebAssembly and the C backend. label Jan 7, 2020
@andrewrk
Copy link
Member Author

andrewrk commented Jan 7, 2020

Confirmed still a bug in master branch.

@helinwang
Copy link

helinwang commented Dec 1, 2021

Thanks for looking at the issue!
Could I know why this is closed, I still hit a similar issue:

    var gpa = std.heap.GeneralPurposeAllocator(.{}){};
    defer {
        const leaked = gpa.deinit();
        if (leaked) expect(false) catch @panic("TEST FAIL");
    }
    var allocator = gpa.allocator;

    var files = try std.ArrayList([]const u8).initCapacity(&allocator, 100);
    defer files.deinit();

The code segfault with:

Segmentation fault at address 0x0
/snap/zig/4341/lib/std/mem/Allocator.zig:289:40: 0x23be1b in std.mem.Allocator.allocAdvancedWithRetAddr (main)
    const byte_slice = try self.allocFn(self, byte_count, a, len_align, return_address);

@Vexu
Copy link
Member

Vexu commented Dec 1, 2021

You are copying the interface out of the implementation, see #591.

@helinwang
Copy link

helinwang commented Dec 1, 2021

Thanks! I checked the issue, as mentioned in the issue, the problem is due to

Any struct that uses @fieldParentPtr on an "interface" field relies on the API user not making a copy of the interface field.

I created a snippet to somewhat illustrate the issue, in case anyone else find it helpful.

const Point = struct {
    x: f32,
    y: f32,
};

fn setYBasedOnX(x: *f32, y: f32) void {
    const point = @fieldParentPtr(Point, "x", x);
    point.y = y;
}

test "field parent pointer" {
    var point = Point{
        .x = 0.1234,
        .y = 0.5678,
    };
    var x = @as(f32, 3.0);
    setYBasedOnX(&x, 0.9);
    std.debug.print("{}", .{point}); // point.x being accidentally set to 0.9!
}

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
3 participants