zig icon indicating copy to clipboard operation
zig copied to clipboard

`std.heap.DebugAllocator` with `.safety = true` broken on freestanding targets

Open Spooky309 opened this issue 2 months ago • 0 comments

Zig Version

0.16.0-dev.1334+06d08daba

Steps to Reproduce and Observed Behavior

Use this config:

const GPA = std.heap.DebugAllocator(.{
    .never_unmap = true,
    .backing_allocator_zeroes = false,
});

with root.os:

fn nullAllocator() std.mem.Allocator {
    return .{ .ptr = undefined, .vtable = undefined };
}
pub const os = struct {
    pub const heap = struct {
        // NEVER USE THIS! This is just to stop it from defaulting fields to POSIX page allocator
        //  DebugAllocator should have its .backing_allocator explicitly set to something else.
        pub const page_allocator = nullAllocator();
    };
};

Yields:

std\posix.zig:3270:18: error: struct 'posix.system__struct_2784' has no member named 'isatty'
    return system.isatty(handle) != 0;
           ~~~~~~^~~~~~~
std\posix.zig:49:13: note: struct declared here
    else => struct {
            ^~~~~~
referenced by:
    isTty: std\fs\File.zig:130:24
    supportsAnsiEscapeCodes: std\fs\File.zig:242:19
    getOrEnableAnsiEscapeSupport: std\fs\File.zig:220:40
    detect: std\Io\tty.zig:58:46
    detectConfig: std\Io\tty.zig:10:19
    free: std\heap\debug_allocator.zig:938:67
    allocator: std\heap\debug_allocator.zig:344:22

Setting .safety to false makes it compile, it seems to me that the only issue are these calls to detectConfig to find out if we can use ANSI colour codes or not.

Expected Behavior

Somewhere in that call stack has a root.os escape hatch, so I can define my own supportsAnsiEscapeCodes function, or somewhere higher up, I'm not sure.

Spooky309 avatar Nov 19 '25 11:11 Spooky309