From fdcfcce9e762644477205cd8a5b7f66d4a6a9965 Mon Sep 17 00:00:00 2001 From: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com> Date: Wed, 31 May 2023 23:15:21 -0700 Subject: [PATCH] Clean-up some option parsing in Bun.spawn --- src/bun.js/api/bun/subprocess.zig | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/bun.js/api/bun/subprocess.zig b/src/bun.js/api/bun/subprocess.zig index 0fb5a98bed..a996f863b0 100644 --- a/src/bun.js/api/bun/subprocess.zig +++ b/src/bun.js/api/bun/subprocess.zig @@ -1114,7 +1114,14 @@ pub const Subprocess = struct { } while (cmds_array.next()) |value| { - argv.appendAssumeCapacity(value.getZigString(globalThis).toOwnedSliceZ(allocator) catch { + const arg = value.getZigString(globalThis); + + // if the string is empty, ignore it, don't add it to the argv + if (arg.len == 0) { + continue; + } + + argv.appendAssumeCapacity(arg.toOwnedSliceZ(allocator) catch { globalThis.throw("out of memory", .{}); return .zero; }); @@ -1128,11 +1135,15 @@ pub const Subprocess = struct { if (args != .zero and args.isObject()) { if (args.get(globalThis, "cwd")) |cwd_| { + // ignore definitely invalid cwd if (!cwd_.isEmptyOrUndefinedOrNull()) { - cwd = cwd_.getZigString(globalThis).toOwnedSliceZ(allocator) catch { - globalThis.throw("out of memory", .{}); - return .zero; - }; + const cwd_str = cwd_.getZigString(globalThis); + if (cwd_str.len > 0) { + cwd = cwd_str.toOwnedSliceZ(allocator) catch { + globalThis.throw("out of memory", .{}); + return .zero; + }; + } } }