diff --git a/build.zig b/build.zig index 8ebedc5b5e124d965f1d0ae889ecdde579ac529a..9508d36be6132ca658a40810481368c087034cdd 100644 --- a/build.zig +++ b/build.zig @@ -308,7 +308,7 @@ pub fn build(b: *std.Build) !void { }, } }; - const version = try b.allocator.dupeZ(u8, version_slice); + const version = try b.allocator.dupeSentinel(u8, version_slice, 0); exe_options.addOption([:0]const u8, "version", version); if (enable_llvm) { diff --git a/lib/compiler/aro/aro/CodeGen.zig b/lib/compiler/aro/aro/CodeGen.zig index 74dc454ffb623835659db25989848c29aafc2808..fdbcb9909e0d17fb3c08ded8ff4283a754d096f2 100644 --- a/lib/compiler/aro/aro/CodeGen.zig +++ b/lib/compiler/aro/aro/CodeGen.zig @@ -894,7 +894,7 @@ fn genLval(c: *CodeGen, node_index: Node.Index) Error!Ir.Ref { } } - const duped_name = try c.builder.arena.allocator().dupeZ(u8, slice); + const duped_name = try c.builder.arena.allocator().dupeSentinel(u8, slice, 0); const ref: Ir.Ref = @enumFromInt(c.builder.instructions.len); try c.builder.instructions.append(c.builder.gpa, .{ .tag = .symbol, .data = .{ .label = duped_name }, .ty = .ptr }); return ref; @@ -1112,7 +1112,7 @@ fn genCall(c: *CodeGen, call: Node.Call) Error!Ir.Ref { } } - const duped_name = try c.builder.arena.allocator().dupeZ(u8, slice); + const duped_name = try c.builder.arena.allocator().dupeSentinel(u8, slice, 0); const ref: Ir.Ref = @enumFromInt(c.builder.instructions.len); try c.builder.instructions.append(c.builder.gpa, .{ .tag = .symbol, .data = .{ .label = duped_name }, .ty = .ptr }); break :blk ref; diff --git a/lib/std/Build/Watch/FsEvents.zig b/lib/std/Build/Watch/FsEvents.zig index dd22a5881df3cc888d9cc44a9e9f546f42ce3169..0a56ce182255176222ef4b9a7a7bbb22abc9350d 100644 --- a/lib/std/Build/Watch/FsEvents.zig +++ b/lib/std/Build/Watch/FsEvents.zig @@ -195,7 +195,7 @@ pub fn setPaths(fse: *FsEvents, gpa: Allocator, steps: []const *std.Build.Step) } else { fse.watch_roots = try gpa.realloc(fse.watch_roots, need_dirs.count()); for (fse.watch_roots, need_dirs.keys()) |*out, in| { - out.* = try paths_arena.dupeZ(u8, in); + out.* = try paths_arena.dupeSentinel(u8, in, 0); } } if (enable_debug_logs) { diff --git a/lib/std/Io/Dir.zig b/lib/std/Io/Dir.zig index 95388eac789f9a16d523bd28e10a16aaa591c18c..15785cfd8a69a04cf7682110dc84754424a7b306 100644 --- a/lib/std/Io/Dir.zig +++ b/lib/std/Io/Dir.zig @@ -944,7 +944,7 @@ pub const RealPathFileAllocError = RealPathFileError || Allocator.Error; pub fn realPathFileAlloc(dir: Dir, io: Io, sub_path: []const u8, allocator: Allocator) RealPathFileAllocError![:0]u8 { var buffer: [max_path_bytes]u8 = undefined; const n = try realPathFile(dir, io, sub_path, &buffer); - return allocator.dupeZ(u8, buffer[0..n]); + return allocator.dupeSentinel(u8, buffer[0..n], 0); } /// Same as `realPathFile` except `absolute_path` is asserted to be an absolute @@ -974,7 +974,7 @@ pub fn realPathFileAbsolute(io: Io, absolute_path: []const u8, out_buffer: []u8) pub fn realPathFileAbsoluteAlloc(io: Io, absolute_path: []const u8, allocator: Allocator) RealPathFileAllocError![:0]u8 { var buffer: [max_path_bytes]u8 = undefined; const n = try realPathFileAbsolute(io, absolute_path, &buffer); - return allocator.dupeZ(u8, buffer[0..n]); + return allocator.dupeSentinel(u8, buffer[0..n], 0); } pub const DeleteFileError = error{ diff --git a/lib/std/Io/Dispatch.zig b/lib/std/Io/Dispatch.zig index 93a1bd93db28ffff82b100979c983168d3b9e8b1..c672421e3350aebbc955c97bb1693213e2b64744 100644 --- a/lib/std/Io/Dispatch.zig +++ b/lib/std/Io/Dispatch.zig @@ -4086,7 +4086,7 @@ fn processReplace(userdata: ?*anyopaque, options: process.ReplaceOptions) proces const arena = arena_allocator.allocator(); const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = -1; @@ -4222,7 +4222,7 @@ fn spawn(ev: *Evented, options: process.SpawnOptions) process.SpawnError!Spawned // Therefore, we do all the allocation for the execve() before the fork(). // This means we must do the null-termination of argv and env vars here. const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = if (prog_pipe[1] == -1) -1 else prog_fileno; diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig index 7b0541f2b17ed7b991ba8cdb9f92dcf872660060..d32e5ca7bab34950226862f26c6647f6dae5821b 100644 --- a/lib/std/Io/Threaded.zig +++ b/lib/std/Io/Threaded.zig @@ -14954,7 +14954,7 @@ fn processReplace(userdata: ?*anyopaque, options: process.ReplaceOptions) proces const arena = arena_allocator.allocator(); const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = -1; @@ -15061,7 +15061,7 @@ fn spawnPosix(t: *Threaded, options: process.SpawnOptions) process.SpawnError!Sp // Therefore, we do all the allocation for the execve() before the fork(). // This means we must do the null-termination of argv and env vars here. const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const prog_fileno = 3; comptime assert(@max(posix.STDIN_FILENO, posix.STDOUT_FILENO, posix.STDERR_FILENO) + 1 == prog_fileno); diff --git a/lib/std/Io/Uring.zig b/lib/std/Io/Uring.zig index fee0d218184a2d5b40980f9a0b8feb5e6e9399dc..7ab63f6711b326488d5a569a2145ff81751c2935 100644 --- a/lib/std/Io/Uring.zig +++ b/lib/std/Io/Uring.zig @@ -4226,7 +4226,7 @@ fn processReplace(userdata: ?*anyopaque, options: process.ReplaceOptions) proces const arena = arena_allocator.allocator(); const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = -1; @@ -4369,7 +4369,7 @@ fn spawn(ev: *Evented, options: process.SpawnOptions) process.SpawnError!Spawned // Therefore, we do all the allocation for the execve() before the fork(). // This means we must do the null-termination of argv and env vars here. const argv_buf = try arena.allocSentinel(?[*:0]const u8, options.argv.len, null); - for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeZ(u8, arg)).ptr; + for (options.argv, 0..) |arg, i| argv_buf[i] = (try arena.dupeSentinel(u8, arg, 0)).ptr; const env_block = env_block: { const prog_fd: i32 = if (prog_pipe[1] == -1) -1 else prog_fileno; diff --git a/lib/std/debug.zig b/lib/std/debug.zig index c700e9b060da71ad7056a9feffc5396300652c7a..b5653f77d082f726b2f791d298e84674f93a0c82 100644 --- a/lib/std/debug.zig +++ b/lib/std/debug.zig @@ -517,7 +517,7 @@ pub fn defaultPanic(msg: []const u8, first_trace_addr: ?usize) noreturn { if (uefi.system_table.boot_services) |bs| { // ExitData buffer must be allocated using boot_services.allocatePool (spec: page 220) - const exit_data = uefi.raw_pool_allocator.dupeZ(u16, exit_msg) catch @trap(); + const exit_data = uefi.raw_pool_allocator.dupeSentinel(u16, exit_msg, 0) catch @trap(); bs.exit(uefi.handle, .aborted, exit_data) catch {}; } @trap(); diff --git a/lib/std/fs/test.zig b/lib/std/fs/test.zig index 686ae840823e0ce4c12da76d4f8c8c5cb466b4c3..580804e1029bb4d4704ec1c56c928e89331956d2 100644 --- a/lib/std/fs/test.zig +++ b/lib/std/fs/test.zig @@ -135,7 +135,7 @@ const TestContext = struct { const allocator = self.arena.allocator(); const transformed_path = try self.transform_fn(allocator, self.io, self.dir, relative_path); if (native_os == .windows) { - const transformed_sep_path = try allocator.dupeZ(u8, transformed_path); + const transformed_sep_path = try allocator.dupeSentinel(u8, transformed_path, 0); std.mem.replaceScalar(u8, transformed_sep_path, switch (self.path_sep) { '/' => '\\', '\\' => '/', @@ -153,7 +153,7 @@ const TestContext = struct { pub fn toCanonicalPathSep(self: *TestContext, path: [:0]const u8) ![:0]const u8 { if (native_os == .windows) { const allocator = self.arena.allocator(); - const transformed_sep_path = try allocator.dupeZ(u8, path); + const transformed_sep_path = try allocator.dupeSentinel(u8, path, 0); std.mem.replaceScalar(u8, transformed_sep_path, '/', '\\'); return transformed_sep_path; } diff --git a/lib/std/mem.zig b/lib/std/mem.zig index 125f1688d3f1b41beb14e3259717555473beeb8d..f0b664f12e77ae032ada79b362c51decc3479f69 100644 --- a/lib/std/mem.zig +++ b/lib/std/mem.zig @@ -4964,7 +4964,7 @@ test isAligned { } test "freeing empty string with null-terminated sentinel" { - const empty_string = try testing.allocator.dupeZ(u8, ""); + const empty_string = try testing.allocator.dupeSentinel(u8, "", 0); testing.allocator.free(empty_string); } diff --git a/lib/std/mem/Allocator.zig b/lib/std/mem/Allocator.zig index 00298ae71ee144513b3fec1481d0ffb52594b664..ec3426146cc0b93f1ea9b7e4a364cc61ab6d641f 100644 --- a/lib/std/mem/Allocator.zig +++ b/lib/std/mem/Allocator.zig @@ -456,12 +456,6 @@ pub fn dupe(allocator: Allocator, comptime T: type, m: []const T) Error![]T { return new_buf; } -/// Deprecated in favor of `dupeSentinel` -/// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory. -pub fn dupeZ(allocator: Allocator, comptime T: type, m: []const T) Error![:0]T { - return allocator.dupeSentinel(T, m, 0); -} - /// Copies `m` to newly allocated memory, with a null-terminated element. Caller owns the memory. pub fn dupeSentinel( allocator: Allocator, diff --git a/lib/std/os/plan9.zig b/lib/std/os/plan9.zig index 36bf83b21c02f42bf1f24f439a355eea9df78a13..48e82c3e11405f5482b2b7b85a715ee7329faf08 100644 --- a/lib/std/os/plan9.zig +++ b/lib/std/os/plan9.zig @@ -299,7 +299,7 @@ pub fn openat(dirfd: i32, path: [*:0]const u8, flags: u32, _: mode_t) usize { const dir_path = std.mem.span(@as([*:0]u8, @ptrCast(&dir_path_buf))); 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 fba.reset(); - const total_path_z = alloc.dupeZ(u8, total_path) catch unreachable; // should not exceed max_path_bytes + 1 + const total_path_z = alloc.dupeSentinel(u8, total_path, 0) catch unreachable; // should not exceed max_path_bytes + 1 return open(total_path_z.ptr, flags); } diff --git a/lib/std/process.zig b/lib/std/process.zig index 5807e79548706ed472b81b600e2d9001dedbbe20..bbcd2ccfe089d562083f9bf61f5c9e4839348dff 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -85,7 +85,7 @@ pub fn currentPathAlloc(io: Io, allocator: Allocator) CurrentPathAllocError![:0] error.NameTooLong => unreachable, else => |e| return e, }; - return allocator.dupeZ(u8, buffer[0..n]); + return allocator.dupeSentinel(u8, buffer[0..n], 0); } test currentPathAlloc { @@ -734,7 +734,7 @@ pub fn executablePathAlloc(io: Io, allocator: Allocator) ExecutablePathAllocErro error.NameTooLong => unreachable, else => |e| return e, }; - return allocator.dupeZ(u8, buffer[0..n]); + return allocator.dupeSentinel(u8, buffer[0..n], 0); } pub const ExecutablePathError = ExecutablePathBaseError || error{NameTooLong}; diff --git a/lib/std/process/Environ.zig b/lib/std/process/Environ.zig index 57d5ea02711885f583f60c65fd26adfb3712f11b..3f467a8db936c6ee0720e1921f46cebc08f0111f 100644 --- a/lib/std/process/Environ.zig +++ b/lib/std/process/Environ.zig @@ -754,7 +754,7 @@ pub fn createPosixBlock( }, .nothing => {}, }; - envp[envp_len] = try gpa.dupeZ(u8, mem.span(entry)); + envp[envp_len] = try gpa.dupeSentinel(u8, mem.span(entry), 0); envp_len += 1; } diff --git a/lib/std/zig/LibCInstallation.zig b/lib/std/zig/LibCInstallation.zig index 96265165bde27768ccbc318bc899071bf1c8c8c8..305b1be2783324e25b87dee08cd0cc0bc705d496 100644 --- a/lib/std/zig/LibCInstallation.zig +++ b/lib/std/zig/LibCInstallation.zig @@ -70,7 +70,7 @@ pub fn parse(allocator: Allocator, io: Io, libc_file: []const u8, target: *const if (value.len == 0) { @field(self, field.name) = null; } else { - found_keys[i].allocated = try allocator.dupeZ(u8, value); + found_keys[i].allocated = try allocator.dupeSentinel(u8, value, 0); @field(self, field.name) = found_keys[i].allocated; } break; diff --git a/src/Compilation.zig b/src/Compilation.zig index 67d845c4221fb8eced8e4224e60325adec1b3371..6e7bfe8be53aa89b795f8b78dd3babf799745a25 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1869,7 +1869,7 @@ pub fn create(gpa: Allocator, arena: Allocator, io: Io, diag: *CreateDiagnostic, const comp: *Compilation = comp: { // We put the `Compilation` itself in the arena. Freeing the arena will free the module. // It's initialized later after we prepare the initialization options. - const root_name = try arena.dupeZ(u8, options.root_name); + const root_name = try arena.dupeSentinel(u8, options.root_name, 0); // The "any" values provided by resolved config only account for // explicitly-provided settings. We now make them additionally account diff --git a/src/codegen/llvm.zig b/src/codegen/llvm.zig index 847191ec1d67a51efc586ce87e209f6915e51f4b..aa611fc474fc279c8d15d4f1e3c609a93fcbe5fa 100644 --- a/src/codegen/llvm.zig +++ b/src/codegen/llvm.zig @@ -918,7 +918,7 @@ pub const Object = struct { } const target_triple_sentinel = - try o.gpa.dupeZ(u8, o.builder.target_triple.slice(&o.builder).?); + try o.gpa.dupeSentinel(u8, o.builder.target_triple.slice(&o.builder).?, 0); defer o.gpa.free(target_triple_sentinel); const emit_asm_msg = options.asm_path orelse "(none)"; diff --git a/src/link/Lld.zig b/src/link/Lld.zig index eba1ee6a8dcec4289f8bb5579b6e7ad7e4cea79b..5eb1e4e4467318ba972c59809ce28f00b66a3e79 100644 --- a/src/link/Lld.zig +++ b/src/link/Lld.zig @@ -287,7 +287,7 @@ fn linkAsArchive(lld: *Lld, arena: Allocator) !void { const comp = base.comp; const directory = base.emit.root_dir; // Just an alias to make it shorter to type. const full_out_path = try directory.join(arena, &[_][]const u8{base.emit.sub_path}); - const full_out_path_z = try arena.dupeZ(u8, full_out_path); + const full_out_path_z = try arena.dupeSentinel(u8, full_out_path, 0); const opt_zcu = comp.zcu; const zcu_obj_path: ?Cache.Path = if (opt_zcu != null) p: { @@ -326,7 +326,7 @@ fn linkAsArchive(lld: *Lld, arena: Allocator) !void { object_files.appendAssumeCapacity(try key.status.success.object_path.toStringZ(arena)); } for (comp.win32_resource_table.keys()) |key| { - object_files.appendAssumeCapacity(try arena.dupeZ(u8, key.status.success.res_path)); + object_files.appendAssumeCapacity(try arena.dupeSentinel(u8, key.status.success.res_path, 0)); } if (zcu_obj_path) |p| object_files.appendAssumeCapacity(try p.toStringZ(arena)); if (compiler_rt_path) |p| object_files.appendAssumeCapacity(try p.toStringZ(arena)); diff --git a/src/main.zig b/src/main.zig index 95ee9a82f15ac3b6591fe07bc061dd9c336b1f4e..68378d89e1715264e1e4a428318ec41727839205 100644 --- a/src/main.zig +++ b/src/main.zig @@ -5971,7 +5971,7 @@ extern "c" fn ZigLlvmAr_main(argc: c_int, argv: [*:null]?[*:0]u8) c_int; fn argsCopyZ(alloc: Allocator, args: []const []const u8) ![:null]?[*:0]u8 { var argv = try alloc.allocSentinel(?[*:0]u8, args.len, null); for (args, 0..) |arg, i| { - argv[i] = try alloc.dupeZ(u8, arg); // TODO If there was an argsAllocZ we could avoid this allocation. + argv[i] = try alloc.dupeSentinel(u8, arg, 0); // TODO If there was an argsAllocZ we could avoid this allocation. } return argv; } @@ -6601,7 +6601,7 @@ fn cmdDumpLlvmInts( if (!build_options.have_llvm) fatal("compiler does not use LLVM; cannot dump LLVM integer sizes", .{}); - const triple = try arena.dupeZ(u8, args[0]); + const triple = try arena.dupeSentinel(u8, args[0], 0); const llvm = @import("codegen/llvm/bindings.zig"); diff --git a/tools/docgen.zig b/tools/docgen.zig index 33ab5ca3cba7fd2a27f887b29daf468aad32c5f7..ab9419d55ab98eff79e07c01a831f6ec07168728 100644 --- a/tools/docgen.zig +++ b/tools/docgen.zig @@ -679,7 +679,7 @@ fn tokenizeAndPrintRaw( raw_src: []const u8, ) !void { const src_non_terminated = mem.trim(u8, raw_src, " \r\n"); - const src = try allocator.dupeZ(u8, src_non_terminated); + const src = try allocator.dupeSentinel(u8, src_non_terminated, 0); try out.writeAll(""); var tokenizer = std.zig.Tokenizer.init(src); diff --git a/tools/doctest.zig b/tools/doctest.zig index fde277118ef44b3f3407f788f3dee56561f72592..90c7108d297acaf6db6da02eb29b87af2ac151a9 100644 --- a/tools/doctest.zig +++ b/tools/doctest.zig @@ -607,7 +607,7 @@ fn printSourceBlock(arena: Allocator, out: *Writer, source_bytes: []const u8, na fn tokenizeAndPrint(arena: Allocator, out: *Writer, raw_src: []const u8) !void { const src_non_terminated = mem.trim(u8, raw_src, " \r\n"); - const src = try arena.dupeZ(u8, src_non_terminated); + const src = try arena.dupeSentinel(u8, src_non_terminated, 0); try out.writeAll(""); var tokenizer = std.zig.Tokenizer.init(src);