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>
This commit is contained in:
Adrian Cole 2023-01-19 13:50:23 +08:00 committed by Veikka Tuominen
parent c70a3d9022
commit 7208e1ff87

View File

@ -293,6 +293,10 @@ pub fn getEnvMap(allocator: Allocator) !EnvMap {
return os.unexpectedErrno(environ_sizes_get_ret); return os.unexpectedErrno(environ_sizes_get_ret);
} }
if (environ_count == 0) {
return result;
}
var environ = try allocator.alloc([*:0]u8, environ_count); var environ = try allocator.alloc([*:0]u8, environ_count);
defer allocator.free(environ); defer allocator.free(environ);
var environ_buf = try allocator.alloc(u8, environ_buf_size); var environ_buf = try allocator.alloc(u8, environ_buf_size);
@ -468,6 +472,10 @@ pub const ArgIteratorWasi = struct {
else => |err| return os.unexpectedErrno(err), else => |err| return os.unexpectedErrno(err),
} }
if (count == 0) {
return &[_][:0]u8{};
}
var argv = try allocator.alloc([*:0]u8, count); var argv = try allocator.alloc([*:0]u8, count);
defer allocator.free(argv); defer allocator.free(argv);