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

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

Open
BlockOG opened this issue Feb 26, 2025 · 3 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@BlockOG
Copy link

BlockOG commented Feb 26, 2025

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

@BlockOG BlockOG added the bug Observed behavior contradicts documented or intended behavior label Feb 26, 2025
@nektro
Copy link
Contributor

nektro commented Feb 26, 2025

can you include the stack trace of what you get running the code?

@nektro
Copy link
Contributor

nektro commented Feb 27, 2025

no crash

test {
    const allocator = std.testing.allocator;
    const a = try allocator.allocSentinel(u8, 2, 0);
    const b = a[0..];
    allocator.free(b);
}

crash

test {
    const allocator = std.testing.allocator;
    const a = try allocator.allocSentinel(u8, 2, 0);
    const b = a[0..2 :0];
    allocator.free(b);
}

no crash

test {
    const allocator = std.testing.allocator;
    const a = try allocator.allocSentinel(u8, 2, 0);
    const b = a[0..3];
    allocator.free(b);
}

@BlockOG
Copy link
Author

BlockOG commented Feb 27, 2025

Yeah, it's specifically when the type given is a pointer to an array with a sentinel

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
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants