authorgravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-13 12:54:31+01:00
committergravatar for mlugg@mlugg.co.ukMatthew Lugg <mlugg@mlugg.co.uk> 2025-09-30 13:44:54+01:00
logf5c8d80e08fac9dcf2eb88941d68b44640a4c9b4
tree8ea4aea6321454af813cea9929265cb582601772
parentd28966785652822b7b763044ce4cb2421247a8f4
signaturelock-open Commit is signed but in an unrecognized format.

windows_bat_args: fix path handling

The input path could be cwd-relative, in which case it must be modified before it is written into the batch script. Also, remove usage of deprecated `GeneralPurposeAllocator` alias, rename `allocator` to `gpa`, use unmanaged `ArrayList`.

2 files changed, 117 insertions(+), 107 deletions(-)

test/standalone/windows_bat_args/fuzz.zig+40-35
......@@ -3,14 +3,14 @@ const builtin = @import("builtin");
33const Allocator = std.mem.Allocator;
44
55pub fn main() anyerror!void {
6 var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
7 defer if (gpa.deinit() == .leak) @panic("found memory leaks");
8 const allocator = gpa.allocator();
6 var debug_alloc_inst: std.heap.DebugAllocator(.{}) = .init;
7 defer std.debug.assert(debug_alloc_inst.deinit() == .ok);
8 const gpa = debug_alloc_inst.allocator();
99
10 var it = try std.process.argsWithAllocator(allocator);
10 var it = try std.process.argsWithAllocator(gpa);
1111 defer it.deinit();
1212 _ = it.next() orelse unreachable; // skip binary name
13 const child_exe_path = it.next() orelse unreachable;
13 const child_exe_path_orig = it.next() orelse unreachable;
1414
1515 const iterations: u64 = iterations: {
1616 const arg = it.next() orelse "0";
......@@ -42,58 +42,63 @@ pub fn main() anyerror!void {
4242 try tmp.dir.setAsCwd();
4343 defer tmp.parent_dir.setAsCwd() catch {};
4444
45 var buf = try std.array_list.Managed(u8).initCapacity(allocator, 128);
46 defer buf.deinit();
47 try buf.appendSlice("@echo off\n");
48 try buf.append('"');
49 try buf.appendSlice(child_exe_path);
50 try buf.append('"');
45 // `child_exe_path_orig` might be relative; make it relative to our new cwd.
46 const child_exe_path = try std.fs.path.resolve(gpa, &.{ "..\\..\\..", child_exe_path_orig });
47 defer gpa.free(child_exe_path);
48
49 var buf: std.ArrayList(u8) = .empty;
50 defer buf.deinit(gpa);
51 try buf.print(gpa,
52 \\@echo off
53 \\"{s}"
54 , .{child_exe_path});
55 // Trailing newline intentionally omitted above so we can add args.
5156 const preamble_len = buf.items.len;
5257
53 try buf.appendSlice(" %*");
58 try buf.appendSlice(gpa, " %*");
5459 try tmp.dir.writeFile(.{ .sub_path = "args1.bat", .data = buf.items });
5560 buf.shrinkRetainingCapacity(preamble_len);
5661
57 try buf.appendSlice(" %1 %2 %3 %4 %5 %6 %7 %8 %9");
62 try buf.appendSlice(gpa, " %1 %2 %3 %4 %5 %6 %7 %8 %9");
5863 try tmp.dir.writeFile(.{ .sub_path = "args2.bat", .data = buf.items });
5964 buf.shrinkRetainingCapacity(preamble_len);
6065
61 try buf.appendSlice(" \"%~1\" \"%~2\" \"%~3\" \"%~4\" \"%~5\" \"%~6\" \"%~7\" \"%~8\" \"%~9\"");
66 try buf.appendSlice(gpa, " \"%~1\" \"%~2\" \"%~3\" \"%~4\" \"%~5\" \"%~6\" \"%~7\" \"%~8\" \"%~9\"");
6267 try tmp.dir.writeFile(.{ .sub_path = "args3.bat", .data = buf.items });
6368 buf.shrinkRetainingCapacity(preamble_len);
6469
6570 var i: u64 = 0;
6671 while (iterations == 0 or i < iterations) {
67 const rand_arg = try randomArg(allocator, rand);
68 defer allocator.free(rand_arg);
72 const rand_arg = try randomArg(gpa, rand);
73 defer gpa.free(rand_arg);
6974
70 try testExec(allocator, &.{rand_arg}, null);
75 try testExec(gpa, &.{rand_arg}, null);
7176
7277 i += 1;
7378 }
7479}
7580
76fn testExec(allocator: std.mem.Allocator, args: []const []const u8, env: ?*std.process.EnvMap) !void {
77 try testExecBat(allocator, "args1.bat", args, env);
78 try testExecBat(allocator, "args2.bat", args, env);
79 try testExecBat(allocator, "args3.bat", args, env);
81fn testExec(gpa: std.mem.Allocator, args: []const []const u8, env: ?*std.process.EnvMap) !void {
82 try testExecBat(gpa, "args1.bat", args, env);
83 try testExecBat(gpa, "args2.bat", args, env);
84 try testExecBat(gpa, "args3.bat", args, env);
8085}
8186
82fn testExecBat(allocator: std.mem.Allocator, bat: []const u8, args: []const []const u8, env: ?*std.process.EnvMap) !void {
83 var argv = try std.array_list.Managed([]const u8).initCapacity(allocator, 1 + args.len);
84 defer argv.deinit();
85 argv.appendAssumeCapacity(bat);
86 argv.appendSliceAssumeCapacity(args);
87fn testExecBat(gpa: std.mem.Allocator, bat: []const u8, args: []const []const u8, env: ?*std.process.EnvMap) !void {
88 const argv = try gpa.alloc([]const u8, 1 + args.len);
89 defer gpa.free(argv);
90 argv[0] = bat;
91 @memcpy(argv[1..], args);
8792
8893 const can_have_trailing_empty_args = std.mem.eql(u8, bat, "args3.bat");
8994
9095 const result = try std.process.Child.run(.{
91 .allocator = allocator,
96 .allocator = gpa,
9297 .env_map = env,
93 .argv = argv.items,
98 .argv = argv,
9499 });
95 defer allocator.free(result.stdout);
96 defer allocator.free(result.stderr);
100 defer gpa.free(result.stdout);
101 defer gpa.free(result.stderr);
97102
98103 try std.testing.expectEqualStrings("", result.stderr);
99104 var it = std.mem.splitScalar(u8, result.stdout, '\x00');
......@@ -109,7 +114,7 @@ fn testExecBat(allocator: std.mem.Allocator, bat: []const u8, args: []const []co
109114 }
110115}
111116
112fn randomArg(allocator: Allocator, rand: std.Random) ![]const u8 {
117fn randomArg(gpa: Allocator, rand: std.Random) ![]const u8 {
113118 const Choice = enum {
114119 backslash,
115120 quote,
......@@ -121,8 +126,8 @@ fn randomArg(allocator: Allocator, rand: std.Random) ![]const u8 {
121126 };
122127
123128 const choices = rand.uintAtMostBiased(u16, 256);
124 var buf = try std.array_list.Managed(u8).initCapacity(allocator, choices);
125 errdefer buf.deinit();
129 var buf: std.ArrayList(u8) = try .initCapacity(gpa, choices);
130 errdefer buf.deinit(gpa);
126131
127132 var last_codepoint: u21 = 0;
128133 for (0..choices) |_| {
......@@ -149,12 +154,12 @@ fn randomArg(allocator: Allocator, rand: std.Random) ![]const u8 {
149154 continue;
150155 }
151156 }
152 try buf.ensureUnusedCapacity(4);
157 try buf.ensureUnusedCapacity(gpa, 4);
153158 const unused_slice = buf.unusedCapacitySlice();
154159 const len = std.unicode.wtf8Encode(codepoint, unused_slice) catch unreachable;
155160 buf.items.len += len;
156161 last_codepoint = codepoint;
157162 }
158163
159 return buf.toOwnedSlice();
164 return buf.toOwnedSlice(gpa);
160165}
test/standalone/windows_bat_args/test.zig+77-72
......@@ -1,14 +1,14 @@
11const std = @import("std");
22
33pub fn main() anyerror!void {
4 var gpa: std.heap.GeneralPurposeAllocator(.{}) = .init;
5 defer if (gpa.deinit() == .leak) @panic("found memory leaks");
6 const allocator = gpa.allocator();
4 var debug_alloc_inst: std.heap.DebugAllocator(.{}) = .init;
5 defer std.debug.assert(debug_alloc_inst.deinit() == .ok);
6 const gpa = debug_alloc_inst.allocator();
77
8 var it = try std.process.argsWithAllocator(allocator);
8 var it = try std.process.argsWithAllocator(gpa);
99 defer it.deinit();
1010 _ = it.next() orelse unreachable; // skip binary name
11 const child_exe_path = it.next() orelse unreachable;
11 const child_exe_path_orig = it.next() orelse unreachable;
1212
1313 var tmp = std.testing.tmpDir(.{});
1414 defer tmp.cleanup();
......@@ -16,62 +16,67 @@ pub fn main() anyerror!void {
1616 try tmp.dir.setAsCwd();
1717 defer tmp.parent_dir.setAsCwd() catch {};
1818
19 var buf = try std.array_list.Managed(u8).initCapacity(allocator, 128);
20 defer buf.deinit();
21 try buf.appendSlice("@echo off\n");
22 try buf.append('"');
23 try buf.appendSlice(child_exe_path);
24 try buf.append('"');
19 // `child_exe_path_orig` might be relative; make it relative to our new cwd.
20 const child_exe_path = try std.fs.path.resolve(gpa, &.{ "..\\..\\..", child_exe_path_orig });
21 defer gpa.free(child_exe_path);
22
23 var buf: std.ArrayList(u8) = .empty;
24 defer buf.deinit(gpa);
25 try buf.print(gpa,
26 \\@echo off
27 \\"{s}"
28 , .{child_exe_path});
29 // Trailing newline intentionally omitted above so we can add args.
2530 const preamble_len = buf.items.len;
2631
27 try buf.appendSlice(" %*");
32 try buf.appendSlice(gpa, " %*");
2833 try tmp.dir.writeFile(.{ .sub_path = "args1.bat", .data = buf.items });
2934 buf.shrinkRetainingCapacity(preamble_len);
3035
31 try buf.appendSlice(" %1 %2 %3 %4 %5 %6 %7 %8 %9");
36 try buf.appendSlice(gpa, " %1 %2 %3 %4 %5 %6 %7 %8 %9");
3237 try tmp.dir.writeFile(.{ .sub_path = "args2.bat", .data = buf.items });
3338 buf.shrinkRetainingCapacity(preamble_len);
3439
35 try buf.appendSlice(" \"%~1\" \"%~2\" \"%~3\" \"%~4\" \"%~5\" \"%~6\" \"%~7\" \"%~8\" \"%~9\"");
40 try buf.appendSlice(gpa, " \"%~1\" \"%~2\" \"%~3\" \"%~4\" \"%~5\" \"%~6\" \"%~7\" \"%~8\" \"%~9\"");
3641 try tmp.dir.writeFile(.{ .sub_path = "args3.bat", .data = buf.items });
3742 buf.shrinkRetainingCapacity(preamble_len);
3843
3944 // Test cases are from https://github.com/rust-lang/rust/blob/master/tests/ui/std/windows-bat-args.rs
40 try testExecError(error.InvalidBatchScriptArg, allocator, &.{"\x00"});
41 try testExecError(error.InvalidBatchScriptArg, allocator, &.{"\n"});
42 try testExecError(error.InvalidBatchScriptArg, allocator, &.{"\r"});
43 try testExec(allocator, &.{ "a", "b" }, null);
44 try testExec(allocator, &.{ "c is for cat", "d is for dog" }, null);
45 try testExec(allocator, &.{ "\"", " \"" }, null);
46 try testExec(allocator, &.{ "\\", "\\" }, null);
47 try testExec(allocator, &.{">file.txt"}, null);
48 try testExec(allocator, &.{"whoami.exe"}, null);
49 try testExec(allocator, &.{"&a.exe"}, null);
50 try testExec(allocator, &.{"&echo hello "}, null);
51 try testExec(allocator, &.{ "&echo hello", "&whoami", ">file.txt" }, null);
52 try testExec(allocator, &.{"!TMP!"}, null);
53 try testExec(allocator, &.{"key=value"}, null);
54 try testExec(allocator, &.{"\"key=value\""}, null);
55 try testExec(allocator, &.{"key = value"}, null);
56 try testExec(allocator, &.{"key=[\"value\"]"}, null);
57 try testExec(allocator, &.{ "", "a=b" }, null);
58 try testExec(allocator, &.{"key=\"foo bar\""}, null);
59 try testExec(allocator, &.{"key=[\"my_value]"}, null);
60 try testExec(allocator, &.{"key=[\"my_value\",\"other-value\"]"}, null);
61 try testExec(allocator, &.{"key\\=value"}, null);
62 try testExec(allocator, &.{"key=\"&whoami\""}, null);
63 try testExec(allocator, &.{"key=\"value\"=5"}, null);
64 try testExec(allocator, &.{"key=[\">file.txt\"]"}, null);
65 try testExec(allocator, &.{"%hello"}, null);
66 try testExec(allocator, &.{"%PATH%"}, null);
67 try testExec(allocator, &.{"%%cd:~,%"}, null);
68 try testExec(allocator, &.{"%PATH%PATH%"}, null);
69 try testExec(allocator, &.{"\">file.txt"}, null);
70 try testExec(allocator, &.{"abc\"&echo hello"}, null);
71 try testExec(allocator, &.{"123\">file.txt"}, null);
72 try testExec(allocator, &.{"\"&echo hello&whoami.exe"}, null);
73 try testExec(allocator, &.{ "\"hello^\"world\"", "hello &echo oh no >file.txt" }, null);
74 try testExec(allocator, &.{"&whoami.exe"}, null);
45 try testExecError(error.InvalidBatchScriptArg, gpa, &.{"\x00"});
46 try testExecError(error.InvalidBatchScriptArg, gpa, &.{"\n"});
47 try testExecError(error.InvalidBatchScriptArg, gpa, &.{"\r"});
48 try testExec(gpa, &.{ "a", "b" }, null);
49 try testExec(gpa, &.{ "c is for cat", "d is for dog" }, null);
50 try testExec(gpa, &.{ "\"", " \"" }, null);
51 try testExec(gpa, &.{ "\\", "\\" }, null);
52 try testExec(gpa, &.{">file.txt"}, null);
53 try testExec(gpa, &.{"whoami.exe"}, null);
54 try testExec(gpa, &.{"&a.exe"}, null);
55 try testExec(gpa, &.{"&echo hello "}, null);
56 try testExec(gpa, &.{ "&echo hello", "&whoami", ">file.txt" }, null);
57 try testExec(gpa, &.{"!TMP!"}, null);
58 try testExec(gpa, &.{"key=value"}, null);
59 try testExec(gpa, &.{"\"key=value\""}, null);
60 try testExec(gpa, &.{"key = value"}, null);
61 try testExec(gpa, &.{"key=[\"value\"]"}, null);
62 try testExec(gpa, &.{ "", "a=b" }, null);
63 try testExec(gpa, &.{"key=\"foo bar\""}, null);
64 try testExec(gpa, &.{"key=[\"my_value]"}, null);
65 try testExec(gpa, &.{"key=[\"my_value\",\"other-value\"]"}, null);
66 try testExec(gpa, &.{"key\\=value"}, null);
67 try testExec(gpa, &.{"key=\"&whoami\""}, null);
68 try testExec(gpa, &.{"key=\"value\"=5"}, null);
69 try testExec(gpa, &.{"key=[\">file.txt\"]"}, null);
70 try testExec(gpa, &.{"%hello"}, null);
71 try testExec(gpa, &.{"%PATH%"}, null);
72 try testExec(gpa, &.{"%%cd:~,%"}, null);
73 try testExec(gpa, &.{"%PATH%PATH%"}, null);
74 try testExec(gpa, &.{"\">file.txt"}, null);
75 try testExec(gpa, &.{"abc\"&echo hello"}, null);
76 try testExec(gpa, &.{"123\">file.txt"}, null);
77 try testExec(gpa, &.{"\"&echo hello&whoami.exe"}, null);
78 try testExec(gpa, &.{ "\"hello^\"world\"", "hello &echo oh no >file.txt" }, null);
79 try testExec(gpa, &.{"&whoami.exe"}, null);
7580
7681 // Ensure that trailing space and . characters can't lead to unexpected bat/cmd script execution.
7782 // In many Windows APIs (including CreateProcess), trailing space and . characters are stripped
......@@ -89,17 +94,17 @@ pub fn main() anyerror!void {
8994 // > "args1.bat .. "
9095 // '"args1.bat .. "' is not recognized as an internal or external command,
9196 // operable program or batch file.
92 try std.testing.expectError(error.FileNotFound, testExecBat(allocator, "args1.bat .. ", &.{"abc"}, null));
97 try std.testing.expectError(error.FileNotFound, testExecBat(gpa, "args1.bat .. ", &.{"abc"}, null));
9398 const absolute_with_trailing = blk: {
94 const absolute_path = try std.fs.realpathAlloc(allocator, "args1.bat");
95 defer allocator.free(absolute_path);
96 break :blk try std.mem.concat(allocator, u8, &.{ absolute_path, " .. " });
99 const absolute_path = try std.fs.realpathAlloc(gpa, "args1.bat");
100 defer gpa.free(absolute_path);
101 break :blk try std.mem.concat(gpa, u8, &.{ absolute_path, " .. " });
97102 };
98 defer allocator.free(absolute_with_trailing);
99 try std.testing.expectError(error.FileNotFound, testExecBat(allocator, absolute_with_trailing, &.{"abc"}, null));
103 defer gpa.free(absolute_with_trailing);
104 try std.testing.expectError(error.FileNotFound, testExecBat(gpa, absolute_with_trailing, &.{"abc"}, null));
100105
101106 var env = env: {
102 var env = try std.process.getEnvMap(allocator);
107 var env = try std.process.getEnvMap(gpa);
103108 errdefer env.deinit();
104109 // No escaping
105110 try env.put("FOO", "123");
......@@ -110,37 +115,37 @@ pub fn main() anyerror!void {
110115 break :env env;
111116 };
112117 defer env.deinit();
113 try testExec(allocator, &.{"%FOO%"}, &env);
118 try testExec(gpa, &.{"%FOO%"}, &env);
114119
115120 // Ensure that none of the `>file.txt`s have caused file.txt to be created
116121 try std.testing.expectError(error.FileNotFound, tmp.dir.access("file.txt", .{}));
117122}
118123
119fn testExecError(err: anyerror, allocator: std.mem.Allocator, args: []const []const u8) !void {
120 return std.testing.expectError(err, testExec(allocator, args, null));
124fn testExecError(err: anyerror, gpa: std.mem.Allocator, args: []const []const u8) !void {
125 return std.testing.expectError(err, testExec(gpa, args, null));
121126}
122127
123fn testExec(allocator: std.mem.Allocator, args: []const []const u8, env: ?*std.process.EnvMap) !void {
124 try testExecBat(allocator, "args1.bat", args, env);
125 try testExecBat(allocator, "args2.bat", args, env);
126 try testExecBat(allocator, "args3.bat", args, env);
128fn testExec(gpa: std.mem.Allocator, args: []const []const u8, env: ?*std.process.EnvMap) !void {
129 try testExecBat(gpa, "args1.bat", args, env);
130 try testExecBat(gpa, "args2.bat", args, env);
131 try testExecBat(gpa, "args3.bat", args, env);
127132}
128133
129fn testExecBat(allocator: std.mem.Allocator, bat: []const u8, args: []const []const u8, env: ?*std.process.EnvMap) !void {
130 var argv = try std.array_list.Managed([]const u8).initCapacity(allocator, 1 + args.len);
131 defer argv.deinit();
132 argv.appendAssumeCapacity(bat);
133 argv.appendSliceAssumeCapacity(args);
134fn testExecBat(gpa: std.mem.Allocator, bat: []const u8, args: []const []const u8, env: ?*std.process.EnvMap) !void {
135 const argv = try gpa.alloc([]const u8, 1 + args.len);
136 defer gpa.free(argv);
137 argv[0] = bat;
138 @memcpy(argv[1..], args);
134139
135140 const can_have_trailing_empty_args = std.mem.eql(u8, bat, "args3.bat");
136141
137142 const result = try std.process.Child.run(.{
138 .allocator = allocator,
143 .allocator = gpa,
139144 .env_map = env,
140 .argv = argv.items,
145 .argv = argv,
141146 });
142 defer allocator.free(result.stdout);
143 defer allocator.free(result.stderr);
147 defer gpa.free(result.stdout);
148 defer gpa.free(result.stderr);
144149
145150 try std.testing.expectEqualStrings("", result.stderr);
146151 var it = std.mem.splitScalar(u8, result.stdout, '\x00');