Skip to content

Runtime crash when invoking function pointer via try on a function body #24229

Open
@emmarion

Description

@emmarion

Zig Version

0.15.0-dev.832+1ca213dab

Steps to Reproduce and Observed Behavior

The following code compiles fine:

fn nop() void {}

fn err() !fn () void {
    return nop;
}

pub fn main() !void {
    (&(try err()))();
}

However, zig build run results in a crash (below is from my mac):

Illegal instruction at address 0x104c2b630
/Users/eric/zig/0.15.0-dev.832+1ca213dab/files/lib/std/fmt.zig:133:32: 0x104c2b630 in vlaBoundNotPositive (zlang2)
            try writer.writeAll(literal);
                               ^
/Users/eric/zig/0.15.0-dev.832+1ca213dab/files/lib/std/start.zig:675:37: 0x104c03d8b in main (zlang2)
            const result = root.main() catch |err| {
                                    ^
???:?:?: 0x18025eb97 in ??? (???)
???:?:?: 0x0 in ??? (???)
run
└─ run zlang2 failure
error: the following command terminated unexpectedly:
/Users/eric/devel/zig/zlang2/zig-out/bin/zlang2 
Build Summary: 3/5 steps succeeded; 1 failed
run transitive failure
└─ run zlang2 failure
error: the following build command failed with exit code 1:
.zig-cache/o/c1611bcd89c01f37ebd9f0c47fbcd03f/build /Users/eric/zig/0.15.0-dev.832+1ca213dab/files/zig /Users/eric/zig/0.15.0-dev.832+1ca213dab/files/lib /Users/eric/devel/zig/zlang2 .zig-cache /Users/eric/.cache/zig --seed 0xe32a5675 -Z72570bc6f1641f27 run

For what it's worth, the following code also exhibits this same incorrect behavior:

fn nop() void {}

fn err() !fn () void {
    return nop;
}

pub fn main() void {
    (&(err() catch unreachable))();
}

Expected Behavior

Executable should run without crashing.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugObserved behavior contradicts documented or intended behavior

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions