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

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

The change makes the following code panic:

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
}

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.

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.

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.

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

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

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", .{});
    }
}

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

Obviously you can just cast it without making copy:

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.

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.

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.

good idea.