From 8faa85ac19e1bbbe962d69b283c94526e35f5338 Mon Sep 17 00:00:00 2001 From: Carter Sande Date: Mon, 22 Jun 2020 02:45:23 -0700 Subject: [PATCH] ArgIteratorWindows: don't treat unclosed quotes like they're escaped --- lib/std/process.zig | 31 +++---------------------------- 1 file changed, 3 insertions(+), 28 deletions(-) diff --git a/lib/std/process.zig b/lib/std/process.zig index a65f6da3af..57b7888e19 100644 --- a/lib/std/process.zig +++ b/lib/std/process.zig @@ -282,7 +282,6 @@ pub const ArgIteratorWindows = struct { index: usize, cmd_line: [*]const u8, in_quote: bool, - quote_count: usize, seen_quote_count: usize, pub const NextError = error{OutOfMemory}; @@ -296,7 +295,6 @@ pub const ArgIteratorWindows = struct { .index = 0, .cmd_line = cmd_line, .in_quote = false, - .quote_count = countQuotes(cmd_line), .seen_quote_count = 0, }; } @@ -342,7 +340,7 @@ pub const ArgIteratorWindows = struct { backslash_count += 1; }, ' ', '\t' => { - if (self.seen_quote_count % 2 == 0 or self.seen_quote_count == self.quote_count) { + if (self.seen_quote_count % 2 == 0) { return true; } backslash_count = 0; @@ -371,9 +369,6 @@ pub const ArgIteratorWindows = struct { if (quote_is_real) { self.seen_quote_count += 1; - if (self.seen_quote_count == self.quote_count and self.seen_quote_count % 2 == 1) { - try buf.append('"'); - } } else { try buf.append('"'); } @@ -384,7 +379,7 @@ pub const ArgIteratorWindows = struct { ' ', '\t' => { try self.emitBackslashes(&buf, backslash_count); backslash_count = 0; - if (self.seen_quote_count % 2 == 1 and self.seen_quote_count != self.quote_count) { + if (self.seen_quote_count % 2 == 1) { try buf.append(byte); } else { return buf.toOwnedSlice(); @@ -405,26 +400,6 @@ pub const ArgIteratorWindows = struct { try buf.append('\\'); } } - - fn countQuotes(cmd_line: [*]const u8) usize { - var result: usize = 0; - var backslash_count: usize = 0; - var index: usize = 0; - while (true) : (index += 1) { - const byte = cmd_line[index]; - switch (byte) { - 0 => return result, - '\\' => backslash_count += 1, - '"' => { - result += 1 - (backslash_count % 2); - backslash_count = 0; - }, - else => { - backslash_count = 0; - }, - } - } - } }; pub const ArgIterator = struct { @@ -578,7 +553,7 @@ test "windows arg parsing" { testWindowsCmdLine("a\\\\\\b d\"e f\"g h", &[_][]const u8{ "a\\\\\\b", "de fg", "h" }); testWindowsCmdLine("a\\\\\\\"b c d", &[_][]const u8{ "a\\\"b", "c", "d" }); testWindowsCmdLine("a\\\\\\\\\"b c\" d e", &[_][]const u8{ "a\\\\b c", "d", "e" }); - testWindowsCmdLine("a b\tc \"d f", &[_][]const u8{ "a", "b", "c", "\"d", "f" }); + testWindowsCmdLine("a b\tc \"d f", &[_][]const u8{ "a", "b", "c", "d f" }); testWindowsCmdLine("\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\"", &[_][]const u8{ ".\\..\\zig-cache\\build",