authorgravatar for adrian@tetrate.ioAdrian Cole <adrian@tetrate.io> 2023-01-19 13:50:23+08:00
committergravatar for git@vexu.euVeikka Tuominen <git@vexu.eu> 2023-01-19 15:34:38+02:00
log7208e1ff8715856f4ec7b5148450d160442d5a6e
tree83a91978fa8c912be81c1a53715e88b2b34005c6
parentc70a3d9022b4cb3e848a5f092a5eab7f7217cca0

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 <adrian@tetrate.io>

1 files changed, 8 insertions(+), 0 deletions(-)

lib/std/process.zig+8
......@@ -293,6 +293,10 @@ pub fn getEnvMap(allocator: Allocator) !EnvMap {
293293 return os.unexpectedErrno(environ_sizes_get_ret);
294294 }
295295
296 if (environ_count == 0) {
297 return result;
298 }
299
296300 var environ = try allocator.alloc([*:0]u8, environ_count);
297301 defer allocator.free(environ);
298302 var environ_buf = try allocator.alloc(u8, environ_buf_size);
......@@ -468,6 +472,10 @@ pub const ArgIteratorWasi = struct {
468472 else => |err| return os.unexpectedErrno(err),
469473 }
470474
475 if (count == 0) {
476 return &[_][:0]u8{};
477 }
478
471479 var argv = try allocator.alloc([*:0]u8, count);
472480 defer allocator.free(argv);
473481