# Why does std.Io.Dir.realPathFileAlloc return sentinel-terminated slice since 0.16?

**URL:** <https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934>\
**Category:** Help\
**Tags:** language\
**Created:** [October 13, 2026, 3:55am UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934 "2026-10-13T03:55:45Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 13, 2026, 3:55am UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/1 "2026-10-13T03:55:45Z")

</div>

The corresponding `std.fs.Dir.realpathAlloc` in 0.15 returns non-sentinel-terminated slice.

The change makes the following code panic:

```zig
const std = @import("std");

pub fn main(init: std.process.Init) !void {
    const null_term_path: [:0]u8 = try std.Io.Dir.cwd().realPathFileAlloc(init.io, "/tmp", init.gpa);
    const x: []u8 = null_term_path;
    defer init.gpa.free(x); // panic
}

```

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 13, 2026, 4:14am UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/2 "2026-10-13T04:14:52Z")

</div>

For simple use cases, this is not a problem. For some more complex use cases, now, I must track whether or not the path is originated from a `[:0]u8` value.

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 13, 2026, 4:30am UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/3 "2026-10-13T04:30:03Z")

</div>

The question should be renamed to `why does std.fs.Dir.realpathAlloc in 0.15- return non-sentinel-terminated slice?`.

The new API should be better.

---

<div class="post-metadata">

**Author:** ![vulpesx](https://ziggit.dev/user_avatar/ziggit.dev/vulpesx/32/3989_2.png) [@vulpesx](https://ziggit.dev/u/vulpesx)\
**Post date:** [October 13, 2026, 4:47am UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/4 "2026-10-13T04:47:31Z")

</div>

If you need to care if it is sentinel terminated sometimes it is easier to just make that always the case

As for why this is the case/wasn’t before, I have no idea, I am guessing in the compiler they realised they often need it to be NUL terminated? or just that allocating 1 more byte for a more versatile value isn’t much of a cost.

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 13, 2026, 4:59am UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/5 "2026-10-13T04:59:55Z")

</div>

> [@vulpesx](#):
>
> If you need to care if it is sentinel terminated sometimes it is easier to just make that always the case

The reality is complex. Not a big problem. Just need to change `[]u8` to a union now.

---

<div class="post-metadata">

**Author:** ![makro](https://ziggit.dev/user_avatar/ziggit.dev/makro/32/10507_2.png) [@makro](https://ziggit.dev/u/makro)\
**Post date:** [October 17, 2026, 5:46pm UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/6 "2026-10-17T17:46:34Z")

</div>

Why track it, just cast to a non-sentinel-terminated slice, and pass a duplicate (copy) around with other normal slice string pointers:

```zig
test "convert [*:0] path to slice" {
    const alloc = std.testing.allocator;

    const c_path: [*:0]const u8 = "something";
    std.debug.print("{s}\n", .{c_path});

    const slice_path = try alloc.dupe(u8, c_path[0..std.mem.len(c_path)]);
    defer alloc.free(slice_path);
    std.debug.print("{s}\n", .{slice_path});

    // Would fail with c_path, but works with slice_path
    if (std.mem.eql(u8, slice_path, "something")) {
        std.debug.print("equal\n", .{});
    }
}

```

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 18, 2026, 2:29pm UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/7 "2026-10-18T14:29:08Z")

</div>

> [@makro](#):
>
> Why track it, just cast to a non-sentinel-terminated slice, and pass a duplicate (copy) around with other normal slice string pointers:

For performance reason, I don’t want to make the duplication.

---

<div class="post-metadata">

**Author:** ![makro](https://ziggit.dev/user_avatar/ziggit.dev/makro/32/10507_2.png) [@makro](https://ziggit.dev/u/makro)\
**Post date:** [October 18, 2026, 3:12pm UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/8 "2026-10-18T15:12:11Z")

</div>

Obviously you can just cast it without making copy:

```zig
const slice_path = c_path[0..std.mem.len(c_path));

```

Advice here was that don’t pass `[*:0]` into your functions if you already have normal string slices used elsewhere, as you said “I must track whether or not the path is originated from a `[:0]u8` value.” sounds like you could have other (allocated) slices too. Depending if your functions want managed or unmanaged slices (if they free them or not), choose either (with dupe or without) way to drop `[*:0]` as early as possible. Passing extra origin/type of path parameter further is also perf/size/complexity hit later in code.

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 18, 2026, 4:06pm UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/9 "2026-10-18T16:06:39Z")

</div>

A data structure in my code might accept three kinds of slices:

- `[:0]u8` from `Dir.realPathFileAlloc`
- `[]u8` from `Allocator.dupe`
- umanaged `[]8`

For the slices of the former two kinds, they have to be freed after being used.

Before Zig 0.16, there were only two kinds (the latter two).

I didn’t find this bug in time when migrate the code from 0.15 to 0.16 (yes, my tests are still not sufficient). The bug was found after the code was migrated to 0.17.

---

<div class="post-metadata">

**Author:** ![ScottRedig](https://ziggit.dev/user_avatar/ziggit.dev/scottredig/32/5880_2.png) [@ScottRedig](https://ziggit.dev/u/ScottRedig)\
**Post date:** [October 18, 2026, 6:04pm UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/10 "2026-10-18T18:04:16Z")

</div>

You could use `Allocator.dupeSentinel` instead of just dupe, so the first two types are aligned, and the code which frees the slices can always free the same kind.

---

<div class="post-metadata">

**Author:** ![zigo](https://ziggit.dev/user_avatar/ziggit.dev/zigo/32/2414_2.png) [@zigo](https://ziggit.dev/u/zigo)\
**Post date:** [October 19, 2026, 3:45am UTC](https://ziggit.dev/t/why-does-std-io-dir-realpathfilealloc-return-sentinel-terminated-slice-since-0-16/17934/11 "2026-10-19T03:45:08Z")

</div>

good idea.
