Skip to content

langref: clarify the lifetime of locals in inline functions, inline for, and blocks #25338

@jeffective

Description

@jeffective

Inline functions

Please clarify in the language reference if this is illegal:

pub fn inline returnsLocal() *i32 {
    var local: i32 = 0;
    return &local;
}

Inline for

Please clarify in the language reference if this is illegal:

const std = @import("std");

pub fn main() void {
    var bufs: [4]*const [4]u8 = undefined;
    inline for (0..4) |i| {
        const buf: [4]u8 = @splat(@intCast(i));
        bufs[i] = &buf;
    }
    for (bufs) |buf| {
        std.debug.print("{x}\n", .{buf});
    }
}
00000000
01010101
02020202
03030303

Blocks

Please clarify in the language reference if this is illegal:

const std = @import("std");

pub fn main() void {
    var x: *const i32 = &0;
    {
        var y: i32 = 1;
        x = &y;
    }
    std.debug.print("x = {}\n", .{x.*});
}
x = 1

Related community thread: https://ziggit.dev/t/exploiting-inline-for-to-avoid-stack-curruption/8697/5

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions