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

@Type(.Enum) with invalid tag_type causes segfault and invalid behaviour #6459

Closed
Snektron opened this issue Sep 29, 2020 · 0 comments · Fixed by #6461
Closed

@Type(.Enum) with invalid tag_type causes segfault and invalid behaviour #6459

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

Comments

@Snektron
Copy link
Collaborator

The following example causes the compiler to crash:

const builtin = @import("builtin");

export fn a() void {
    const enum_info = .{
        .layout = .Auto,
        .tag_type = struct {x: u32},
        .fields = &[_]builtin.TypeInfo.EnumField{
            .{.name = "python", .value = 10},
            .{.name = "cobra", .value = 20},
        },
        .decls = &[_]builtin.TypeInfo.Declaration{},
        .is_exhaustive = false,
    };
    const X = @Type(.{.Enum = enum_info});   

    @compileLog(X.python); 
}

The following doesn't crash, but @enumToInt returns zero:

const builtin = @import("builtin");

export fn a() void {
    const enum_info = .{
        .layout = .Auto,
        .tag_type = comptime_int,
        .fields = &[_]builtin.TypeInfo.EnumField{
            .{.name = "python", .value = 10},
            .{.name = "cobra", .value = 20},
        },
        .decls = &[_]builtin.TypeInfo.Declaration{},
        .is_exhaustive = false,
    };
    const X = @Type(.{.Enum = enum_info});   

    @compileLog(@intToEnum(X.python)); 
}

I suppose there should be some guard on tag_type internally. I haven't tested the other @Type variants.

@Vexu Vexu added bug Observed behavior contradicts documented or intended behavior stage1 The process of building from source via WebAssembly and the C backend. labels Sep 29, 2020
@Vexu Vexu added this to the 0.8.0 milestone Sep 29, 2020
tadeokondrak added a commit to tadeokondrak/zig that referenced this issue Sep 29, 2020
tadeokondrak added a commit to tadeokondrak/zig that referenced this issue Sep 30, 2020
@Vexu Vexu modified the milestones: 0.8.0, 0.7.0 Oct 3, 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

Successfully merging a pull request may close this issue.

2 participants