Commit 7208e1ff87

Adrian Cole <adrian@tetrate.io>
2023-01-19 06:50:23
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 parent c70a3d9
Changed files (1)
lib
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);