authorgravatar for carter.sande@duodecima.technologyCarter Sande <carter.sande@duodecima.technology> 2020-06-22 02:45:23-07:00
committergravatar for carter.sande@duodecima.technologyCarter Sande <carter.sande@duodecima.technology> 2020-06-22 03:03:20-07:00
log8faa85ac19e1bbbe962d69b283c94526e35f5338
tree3c9e49c40f3a36de733ce55a64dd3b85e1cd5d49
parentd907f574e02aadf8196e616bcc2fb2813cf2c82c

ArgIteratorWindows: don't treat unclosed quotes like they're escaped


1 files changed, 3 insertions(+), 28 deletions(-)

lib/std/process.zig+3-28
......@@ -282,7 +282,6 @@ pub const ArgIteratorWindows = struct {
282282 index: usize,
283283 cmd_line: [*]const u8,
284284 in_quote: bool,
285 quote_count: usize,
286285 seen_quote_count: usize,
287286
288287 pub const NextError = error{OutOfMemory};
......@@ -296,7 +295,6 @@ pub const ArgIteratorWindows = struct {
296295 .index = 0,
297296 .cmd_line = cmd_line,
298297 .in_quote = false,
299 .quote_count = countQuotes(cmd_line),
300298 .seen_quote_count = 0,
301299 };
302300 }
......@@ -342,7 +340,7 @@ pub const ArgIteratorWindows = struct {
342340 backslash_count += 1;
343341 },
344342 ' ', '\t' => {
345 if (self.seen_quote_count % 2 == 0 or self.seen_quote_count == self.quote_count) {
343 if (self.seen_quote_count % 2 == 0) {
346344 return true;
347345 }
348346 backslash_count = 0;
......@@ -371,9 +369,6 @@ pub const ArgIteratorWindows = struct {
371369
372370 if (quote_is_real) {
373371 self.seen_quote_count += 1;
374 if (self.seen_quote_count == self.quote_count and self.seen_quote_count % 2 == 1) {
375 try buf.append('"');
376 }
377372 } else {
378373 try buf.append('"');
379374 }
......@@ -384,7 +379,7 @@ pub const ArgIteratorWindows = struct {
384379 ' ', '\t' => {
385380 try self.emitBackslashes(&buf, backslash_count);
386381 backslash_count = 0;
387 if (self.seen_quote_count % 2 == 1 and self.seen_quote_count != self.quote_count) {
382 if (self.seen_quote_count % 2 == 1) {
388383 try buf.append(byte);
389384 } else {
390385 return buf.toOwnedSlice();
......@@ -405,26 +400,6 @@ pub const ArgIteratorWindows = struct {
405400 try buf.append('\\');
406401 }
407402 }
408
409 fn countQuotes(cmd_line: [*]const u8) usize {
410 var result: usize = 0;
411 var backslash_count: usize = 0;
412 var index: usize = 0;
413 while (true) : (index += 1) {
414 const byte = cmd_line[index];
415 switch (byte) {
416 0 => return result,
417 '\\' => backslash_count += 1,
418 '"' => {
419 result += 1 - (backslash_count % 2);
420 backslash_count = 0;
421 },
422 else => {
423 backslash_count = 0;
424 },
425 }
426 }
427 }
428403};
429404
430405pub const ArgIterator = struct {
......@@ -578,7 +553,7 @@ test "windows arg parsing" {
578553 testWindowsCmdLine("a\\\\\\b d\"e f\"g h", &[_][]const u8{ "a\\\\\\b", "de fg", "h" });
579554 testWindowsCmdLine("a\\\\\\\"b c d", &[_][]const u8{ "a\\\"b", "c", "d" });
580555 testWindowsCmdLine("a\\\\\\\\\"b c\" d e", &[_][]const u8{ "a\\\\b c", "d", "e" });
581 testWindowsCmdLine("a b\tc \"d f", &[_][]const u8{ "a", "b", "c", "\"d", "f" });
556 testWindowsCmdLine("a b\tc \"d f", &[_][]const u8{ "a", "b", "c", "d f" });
582557
583558 testWindowsCmdLine("\".\\..\\zig-cache\\build\" \"bin\\zig.exe\" \".\\..\" \".\\..\\zig-cache\" \"--help\"", &[_][]const u8{
584559 ".\\..\\zig-cache\\build",