Skip to content

Commit 6d2e76b

Browse files
committed
dupeSentinel instead of dupeZ
closes #25285
1 parent e52cc47 commit 6d2e76b

File tree

18 files changed

+44
-40
lines changed

18 files changed

+44
-40
lines changed

lib/compiler/aro/aro/CodeGen.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ fn genLval(c: *CodeGen, node: NodeIndex) Error!Ir.Ref {
978978
}
979979
}
980980

981-
const duped_name = try c.builder.arena.allocator().dupeZ(u8, slice);
981+
const duped_name = try c.builder.arena.allocator().dupeSentinel(u8, slice, 0);
982982
const ref: Ir.Ref = @enumFromInt(c.builder.instructions.len);
983983
try c.builder.instructions.append(c.builder.gpa, .{ .tag = .symbol, .data = .{ .label = duped_name }, .ty = .ptr });
984984
return ref;
@@ -1200,7 +1200,7 @@ fn genCall(c: *CodeGen, fn_node: NodeIndex, arg_nodes: []const NodeIndex, ty: Ty
12001200
}
12011201
}
12021202

1203-
const duped_name = try c.builder.arena.allocator().dupeZ(u8, slice);
1203+
const duped_name = try c.builder.arena.allocator().dupeSentinel(u8, slice, 0);
12041204
const ref: Ir.Ref = @enumFromInt(c.builder.instructions.len);
12051205
try c.builder.instructions.append(c.builder.gpa, .{ .tag = .symbol, .data = .{ .label = duped_name }, .ty = .ptr });
12061206
break :blk ref;

lib/std/Build/Watch/FsEvents.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ pub fn setPaths(fse: *FsEvents, gpa: Allocator, steps: []const *std.Build.Step)
193193
} else {
194194
fse.watch_roots = try gpa.realloc(fse.watch_roots, need_dirs.count());
195195
for (fse.watch_roots, need_dirs.keys()) |*out, in| {
196-
out.* = try paths_arena.dupeZ(u8, in);
196+
out.* = try paths_arena.dupeSentinel(u8, in, 0);
197197
}
198198
}
199199
if (enable_debug_logs) {

lib/std/debug.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,7 @@ pub fn defaultPanic(
661661

662662
if (uefi.system_table.boot_services) |bs| {
663663
// ExitData buffer must be allocated using boot_services.allocatePool (spec: page 220)
664-
const exit_data = uefi.raw_pool_allocator.dupeZ(u16, exit_msg) catch @trap();
664+
const exit_data = uefi.raw_pool_allocator.dupeSentinel(u16, exit_msg, 0) catch @trap();
665665
bs.exit(uefi.handle, .aborted, exit_data) catch {};
666666
}
667667
@trap();

lib/std/fs/get_app_data_dir.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
4646
.haiku => {
4747
var dir_path_buf: [std.fs.max_path_bytes]u8 = undefined;
4848
const rc = std.c.find_directory(.B_USER_SETTINGS_DIRECTORY, -1, true, &dir_path_buf, dir_path_buf.len);
49-
const settings_dir = try allocator.dupeZ(u8, mem.sliceTo(&dir_path_buf, 0));
49+
const settings_dir = try allocator.dupeSentinel(u8, mem.sliceTo(&dir_path_buf, 0), 0);
5050
defer allocator.free(settings_dir);
5151
switch (rc) {
5252
0 => return fs.path.join(allocator, &[_][]const u8{ settings_dir, appname }),

lib/std/fs/test.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ const TestContext = struct {
105105
const allocator = self.arena.allocator();
106106
const transformed_path = try self.transform_fn(allocator, self.dir, relative_path);
107107
if (native_os == .windows) {
108-
const transformed_sep_path = try allocator.dupeZ(u8, transformed_path);
108+
const transformed_sep_path = try allocator.dupeSentinel(u8, transformed_path, 0);
109109
std.mem.replaceScalar(u8, transformed_sep_path, switch (self.path_sep) {
110110
'/' => '\\',
111111
'\\' => '/',
@@ -123,7 +123,7 @@ const TestContext = struct {
123123
pub fn toCanonicalPathSep(self: *TestContext, path: [:0]const u8) ![:0]const u8 {
124124
if (native_os == .windows) {
125125
const allocator = self.arena.allocator();
126-
const transformed_sep_path = try allocator.dupeZ(u8, path);
126+
const transformed_sep_path = try allocator.dupeSentinel(u8, path, 0);
127127
std.mem.replaceScalar(u8, transformed_sep_path, '/', '\\');
128128
return transformed_sep_path;
129129
}

lib/std/mem.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4711,7 +4711,7 @@ test isAligned {
47114711
}
47124712

47134713
test "freeing empty string with null-terminated sentinel" {
4714-
const empty_string = try testing.allocator.dupeZ(u8, "");
4714+
const empty_string = try testing.allocator.dupeSentinel(u8, "", 0);
47154715
testing.allocator.free(empty_string);
47164716
}
47174717

lib/std/mem/Allocator.zig

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -451,12 +451,16 @@ pub fn dupe(allocator: Allocator, comptime T: type, m: []const T) Error![]T {
451451
return new_buf;
452452
}
453453

454-
/// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory.
455-
pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) Error![:0]T {
454+
/// Copies `m` to newly allocated memory, with a sentinel-terminated element. Caller owns the memory.
455+
pub fn dupeSentinel(allocator: Allocator, comptime T: type, m: []const T, comptime s: T) Error![:s]T {
456456
const new_buf = try allocator.alloc(T, m.len + 1);
457457
@memcpy(new_buf[0..m.len], m);
458-
new_buf[m.len] = 0;
459-
return new_buf[0..m.len :0];
458+
new_buf[m.len] = s;
459+
return new_buf[0..m.len :s];
460+
}
461+
/// Deprecated in favor of `dupeSentinel`
462+
pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) Error![:0]T {
463+
return try dupeSentinel(allocator, T, m, 0);
460464
}
461465

462466
/// An allocator that always fails to allocate.

lib/std/net.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -910,7 +910,7 @@ pub fn getAddressList(gpa: Allocator, name: []const u8, port: u16) GetAddressLis
910910
errdefer result.deinit();
911911

912912
if (native_os == .windows) {
913-
const name_c = try gpa.dupeZ(u8, name);
913+
const name_c = try gpa.dupeSentinel(u8, name, 0);
914914
defer gpa.free(name_c);
915915

916916
const port_c = try std.fmt.allocPrintSentinel(gpa, "{d}", .{port}, 0);
@@ -982,7 +982,7 @@ pub fn getAddressList(gpa: Allocator, name: []const u8, port: u16) GetAddressLis
982982
}
983983

984984
if (builtin.link_libc) {
985-
const name_c = try gpa.dupeZ(u8, name);
985+
const name_c = try gpa.dupeSentinel(u8, name, 0);
986986
defer gpa.free(name_c);
987987

988988
const port_c = try std.fmt.allocPrintSentinel(gpa, "{d}", .{port}, 0);

lib/std/os/plan9.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, _: mode_t) usize {
297297
const dir_path = std.mem.span(@as([*:0]u8, @ptrCast(&dir_path_buf)));
298298
const total_path = std.fs.path.join(alloc, &.{ dir_path, std.mem.span(path) }) catch unreachable; // the allocation shouldn't fail because it should not exceed max_path_bytes
299299
fba.reset();
300-
const total_path_z = alloc.dupeZ(u8, total_path) catch unreachable; // should not exceed max_path_bytes + 1
300+
const total_path_z = alloc.dupeSentinel(u8, total_path, 0) catch unreachable; // should not exceed max_path_bytes + 1
301301
return open(total_path_z.ptr, flags);
302302
}
303303

lib/std/process.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ pub fn execve(
17211721
const arena = arena_allocator.allocator();
17221722

17231723
const argv_buf = try arena.allocSentinel(?[*:0]const u8, argv.len, null);
1724-
for (argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr;
1724+
for (argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr;
17251725

17261726
const envp = m: {
17271727
if (env_map) |m| {
@@ -1999,7 +1999,7 @@ pub fn createEnvironFromExisting(
19991999
},
20002000
.nothing => {},
20012001
};
2002-
envp_buf[i] = try arena.dupeZ(u8, mem.span(line));
2002+
envp_buf[i] = try arena.dupeSentinel(u8, mem.span(line), 0);
20032003
i += 1;
20042004
}
20052005

0 commit comments

Comments
 (0)