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

memory corruption when storing an array of type in a field #7027

Closed
vrischmann opened this issue Nov 8, 2020 · 0 comments · Fixed by #7031
Closed

memory corruption when storing an array of type in a field #7027

vrischmann opened this issue Nov 8, 2020 · 0 comments · Fixed by #7031
Labels
bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend.
Milestone

Comments

@vrischmann
Copy link
Contributor

Hello,

the following code produces garbage when I run it:

const std = @import("std");

const Foobar = struct {
    const Self = @This();

    myTypes: [128]type,

    str: [1024]u8,
    str_len: usize,

    fn from(comptime s: []const u8) Self {
        comptime var foobar: Foobar = undefined;

        std.mem.copy(u8, &foobar.str, s);
        foobar.str_len = s.len;

        return foobar;
    }

    fn getString(comptime self: *Self) []const u8 {
        return self.str[0..self.str_len];
    }
};

pub fn main() anyerror!void {
    comptime var foobar = Foobar.from(
        \\ Enim tempora est occaecati aut delectus quibusdam modi saepe. Fugiat odit id autem et praesentium provident. Iure voluptas explicabo ipsa provident eaque. Repellat dolorum voluptatem velit harum alias nobis quo et. Sit doloribus sit molestias. Dolorem laborum eligendi aut.
    );

    std.debug.print("foobar: {}\n", .{foobar.getString()});
}

I get this output:

foobar: 0+ *1 10 01  D1 0
                            1 / ! / 0

but if I comment the line myTypes: [128]type it works fine:

foobar:  Enim tempora est occaecati aut delectus quibusdam modi saepe. Fugiat odit id autem et praesentium provident. Iure voluptas explicabo ipsa provident eaque. Repellat dolorum voluptatem velit harum alias nobis quo et. Sit doloribus sit molestias. Dolorem laborum eligendi aut.
@alexnask alexnask added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Nov 8, 2020
@alexnask alexnask added this to the 0.8.0 milestone Nov 8, 2020
LemonBoy added a commit to LemonBoy/zig that referenced this issue Nov 8, 2020
Using the gen_index rather than the src_index is needed to handle
structures containing zero-sized or comptime only types.

Closes ziglang#7027
LemonBoy added a commit to LemonBoy/zig that referenced this issue Nov 11, 2020
Using the gen_index rather than the src_index is needed to handle
structures containing zero-sized or comptime only types.

Closes ziglang#7027
Vexu pushed a commit that referenced this issue Nov 11, 2020
Using the gen_index rather than the src_index is needed to handle
structures containing zero-sized or comptime only types.

Closes #7027
andrewrk pushed a commit that referenced this issue Nov 12, 2020
Using the gen_index rather than the src_index is needed to handle
structures containing zero-sized or comptime only types.

Closes #7027
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.

2 participants