Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

std.posix.getenv: early-return comparison #23265

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions lib/std/posix.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can label the outer loop and continue :outer; instead which allows us to skip the line_i != key.len check below.

}
if ((line_i != key.len) or (line[line_i] != '=')) continue;

return mem.sliceTo(line + line_i + 1, 0);
}
Expand All @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise

}
if ((line_i != key.len) or (ptr[line_i] != '=')) continue;

return mem.sliceTo(ptr + line_i + 1, 0);
}
Expand Down
5 changes: 5 additions & 0 deletions lib/std/posix/test.zig
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
Loading