From 7208e1ff8715856f4ec7b5148450d160442d5a6e Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Thu, 19 Jan 2023 13:50:23 +0800 Subject: [PATCH] wasm: avoids allocating zero length buffers for args or env I was testing this with wazero, which defaults to not propagate any env variables. This ensures we don't try to allocate zero length buffers when there are no results from either function. Signed-off-by: Adrian Cole --- lib/std/process.zig | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/lib/std/process.zig b/lib/std/process.zig index 138f5feeb8..b901a9f0fa 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -293,6 +293,10 @@ pub fn getEnvMap(allocator: Allocator) !EnvMap { return os.unexpectedErrno(environ_sizes_get_ret); } + if (environ_count == 0) { + return result; + } + var environ = try allocator.alloc([*:0]u8, environ_count); defer allocator.free(environ); var environ_buf = try allocator.alloc(u8, environ_buf_size); @@ -468,6 +472,10 @@ pub const ArgIteratorWasi = struct { else => |err| return os.unexpectedErrno(err), } + if (count == 0) { + return &[_][:0]u8{}; + } + var argv = try allocator.alloc([*:0]u8, count); defer allocator.free(argv);