commit 42988fc5f43abdbac5ef5abc7cb5f74f8bc55ad4 (tree)
parent b85524d0c83686b7492aee78d5f766e76b59fc90
Author: Andrew Kelley <andrew@ziglang.org>
Date: Tue, 30 Dec 2025 21:53:56 -0800
std.process.Environ.Block: enhance type safety
Diffstat:
3 files changed, 14 insertions(+), 17 deletions(-)
diff --git a/lib/std/Io/Threaded.zig b/lib/std/Io/Threaded.zig
@@ -12688,7 +12688,8 @@ fn scanEnviron(t: *Threaded) void {
}
}
} else {
- for (t.environ.block) |line| {
+ for (t.environ.block) |opt_line| {
+ const line = opt_line.?;
var line_i: usize = 0;
while (line[line_i] != 0 and line[line_i] != '=') : (line_i += 1) {}
const key = line[0..line_i];
diff --git a/lib/std/process/Environ.zig b/lib/std/process/Environ.zig
@@ -15,7 +15,7 @@ block: Block,
pub const Block = switch (native_os) {
.windows => []const u16,
- else => []const [*:0]const u8,
+ else => [:null]const ?[*:0]const u8,
};
pub const Map = struct {
@@ -364,7 +364,8 @@ pub fn createMap(env: Environ, allocator: Allocator) CreateMapError!Map {
}
return result;
} else {
- for (env.block) |line| {
+ for (env.block) |opt_line| {
+ const line = opt_line.?;
var line_i: usize = 0;
while (line[line_i] != 0 and line[line_i] != '=') : (line_i += 1) {}
const key = line[0..line_i];
@@ -579,15 +580,10 @@ pub const CreateBlockOptions = struct {
/// Creates a null-delimited environment variable block in the format expected
/// by POSIX, from a different one.
pub fn createBlock(existing: Environ, arena: Allocator, options: CreateBlockOptions) Allocator.Error![:null]?[*:0]u8 {
- const existing_block: [*:null]const ?[*:0]const u8 = @ptrCast(existing.block);
- const existing_count, const contains_zig_progress = c: {
- var count: usize = 0;
- var contains = false;
- while (existing_block[count]) |line| : (count += 1) {
- contains = contains or mem.eql(u8, mem.sliceTo(line, '='), "ZIG_PROGRESS");
- }
- break :c .{ count, contains };
- };
+ const contains_zig_progress = for (existing.block) |opt_line| {
+ if (mem.eql(u8, mem.sliceTo(opt_line.?, '='), "ZIG_PROGRESS")) break true;
+ } else false;
+
const ZigProgressAction = enum { nothing, edit, delete, add };
const zig_progress_action: ZigProgressAction = a: {
const fd = options.zig_progress_fd orelse break :a .nothing;
@@ -600,7 +596,7 @@ pub fn createBlock(existing: Environ, arena: Allocator, options: CreateBlockOpti
};
const envp_count: usize = c: {
- var count: usize = existing_count;
+ var count: usize = existing.block.len;
switch (zig_progress_action) {
.add => count += 1,
.delete => count -= 1,
@@ -618,7 +614,7 @@ pub fn createBlock(existing: Environ, arena: Allocator, options: CreateBlockOpti
i += 1;
}
- while (existing_block[existing_index]) |line| : (existing_index += 1) {
+ while (existing.block[existing_index]) |line| : (existing_index += 1) {
if (mem.eql(u8, mem.sliceTo(line, '='), "ZIG_PROGRESS")) switch (zig_progress_action) {
.add => unreachable,
.delete => continue,
diff --git a/lib/std/start.zig b/lib/std/start.zig
@@ -559,7 +559,7 @@ fn posixCallMainAndExit(argc_argv_ptr: [*]usize) callconv(.c) noreturn {
const envp_optional: [*:null]?[*:0]u8 = @ptrCast(@alignCast(argv + argc + 1));
var envp_count: usize = 0;
while (envp_optional[envp_count]) |_| : (envp_count += 1) {}
- const envp = @as([*][*:0]u8, @ptrCast(envp_optional))[0..envp_count];
+ const envp = envp_optional[0..envp_count :null];
// Find the beginning of the auxiliary vector
const auxv: [*]elf.Auxv = @ptrCast(@alignCast(envp.ptr + envp_count + 1));
@@ -668,7 +668,7 @@ fn expandStackSize(phdrs: []elf.Phdr) void {
}
}
-inline fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
+inline fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [:null]?[*:0]u8) u8 {
if (std.Options.debug_threaded_io) |t| {
if (@sizeOf(std.Io.Threaded.Argv0) != 0) t.argv0.value = argv[0];
t.environ = .{ .block = envp };
@@ -680,7 +680,7 @@ inline fn callMainWithArgs(argc: usize, argv: [*][*:0]u8, envp: [][*:0]u8) u8 {
fn main(c_argc: c_int, c_argv: [*][*:0]c_char, c_envp: [*:null]?[*:0]c_char) callconv(.c) c_int {
var env_count: usize = 0;
while (c_envp[env_count] != null) : (env_count += 1) {}
- const envp = @as([*][*:0]u8, @ptrCast(c_envp))[0..env_count];
+ const envp = c_envp[0..env_count :null];
if (builtin.os.tag == .linux) {
const at_phdr = std.c.getauxval(elf.AT_PHDR);