diff --git a/lib/std/posix.zig b/lib/std/posix.zig index f920784ca542..f8731504efaf 100644 --- a/lib/std/posix.zig +++ b/lib/std/posix.zig @@ -1997,14 +1997,18 @@ pub fn getenv(key: []const u8) ?[:0]const u8 { if (native_os == .windows) { @compileError("std.posix.getenv is unavailable for Windows because environment strings are in WTF-16 format. See std.process.getEnvVarOwned for a cross-platform API or std.process.getenvW for a Windows-specific API."); } + if (mem.indexOfScalar(u8, key, '=') != null) { + return null; + } if (builtin.link_libc) { var ptr = std.c.environ; while (ptr[0]) |line| : (ptr += 1) { var line_i: usize = 0; - while (line[line_i] != 0 and line[line_i] != '=') : (line_i += 1) {} - const this_key = line[0..line_i]; - - if (!mem.eql(u8, this_key, key)) continue; + while (line[line_i] != 0) : (line_i += 1) { + if (line_i == key.len) break; + if (line[line_i] != key[line_i]) break; + } + if ((line_i != key.len) or (line[line_i] != '=')) continue; return mem.sliceTo(line + line_i + 1, 0); } @@ -2018,9 +2022,11 @@ pub fn getenv(key: []const u8) ?[:0]const u8 { // TODO see https://github.com/ziglang/zig/issues/4524 for (std.os.environ) |ptr| { var line_i: usize = 0; - while (ptr[line_i] != 0 and ptr[line_i] != '=') : (line_i += 1) {} - const this_key = ptr[0..line_i]; - if (!mem.eql(u8, key, this_key)) continue; + while (ptr[line_i] != 0) : (line_i += 1) { + if (line_i == key.len) break; + if (ptr[line_i] != key[line_i]) break; + } + if ((line_i != key.len) or (ptr[line_i] != '=')) continue; return mem.sliceTo(ptr + line_i + 1, 0); } diff --git a/lib/std/posix/test.zig b/lib/std/posix/test.zig index 9678bfb261ce..1eee0151715a 100644 --- a/lib/std/posix/test.zig +++ b/lib/std/posix/test.zig @@ -725,6 +725,11 @@ test "getenv" { if (native_os == .windows) { try expect(std.process.getenvW(&[_:0]u16{ 'B', 'O', 'G', 'U', 'S', 0x11, 0x22, 0x33, 0x44, 0x55 }) == null); } else { + try expect(posix.getenv("") == null); + try expect(posix.getenv("BOGUSDOESNOTEXISTENVVAR") == null); + if (builtin.link_libc) { + try testing.expectEqualStrings(posix.getenv("USER") orelse "", mem.span(std.c.getenv("USER") orelse "")); + } try expect(posix.getenvZ("BOGUSDOESNOTEXISTENVVAR") == null); } }