Skip to content

Trying to free a pointer to array with sentinel fails with invalid free #23019

Open
@BlockOG

Description

@BlockOG

Zig Version

0.14.0-dev.3385+055969b10

Steps to Reproduce and Observed Behavior

test "pointer to sentinel array free" {
    const allocator = @import("std").testing.allocator;

    const a = try allocator.allocSentinel(u8, 2, 0);
    defer allocator.free(a[0..2 :0]); // error with invalid free, due to sentinel not being accounted (type is *[2:0]u8)
}

Crashes with:

thread 20107 panic: Invalid free
/home/blockog/zig/lib/std/heap/debug_allocator.zig:870:49: 0x1055dbb in free (test)
            if (bucket.canary != config.canary) @panic("Invalid free");
                                                ^
/home/blockog/zig/lib/std/mem/Allocator.zig:147:25: 0x10568a3 in free__anon_1623 (test)
    return a.vtable.free(a.ptr, memory, alignment, ret_addr);
                        ^
/home/blockog/crash.zig:5:25: 0x1054e6b in test.pointer to sentinel array free (test)
    defer allocator.free(a[0..2 :0]); // error with invalid free, due to sentinel not being accounted (type is *[2:0]u8)
                        ^
/home/blockog/zig/lib/compiler/test_runner.zig:214:25: 0x10dcf13 in mainTerminal (test)
        if (test_fn.func()) |_| {
                        ^
/home/blockog/zig/lib/compiler/test_runner.zig:62:28: 0x10d7b2b in main (test)
        return mainTerminal();
                           ^
/home/blockog/zig/lib/std/start.zig:647:22: 0x10d718b in posixCallMainAndExit (test)
            root.main();
                     ^
???:?:?: 0x0 in ??? (???)

Expected Behavior

It should be able to free it just fine

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