authorgravatar for quae@daurnimator.comdaurnimator <quae@daurnimator.com> 2020-01-15 18:17:41+10:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-03-03 13:25:43-05:00
loga19a30bb1771f0fc935cd8c17070158d8c379346
tree106f6d0ec09f1473e926f3895f688147bc3d60aa
parentd8f966a04b09ede06840b5fdd42b7d8e65b0cb25
signaturelock-open Commit is signed but in an unrecognized format.

std: move null byte check into toPosixPath

Note that windows NT paths *can* contain nulls

2 files changed, 1 insertions(+), 7 deletions(-)

lib/std/fs.zig-6
...@@ -706,7 +706,6 @@ pub const Dir = struct {...@@ -706,7 +706,6 @@ pub const Dir = struct {
706 /// Call `File.close` to release the resource.706 /// Call `File.close` to release the resource.
707 /// Asserts that the path parameter has no null bytes.707 /// Asserts that the path parameter has no null bytes.
708 pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {708 pub fn openFile(self: Dir, sub_path: []const u8, flags: File.OpenFlags) File.OpenError!File {
709 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
710 if (builtin.os.tag == .windows) {709 if (builtin.os.tag == .windows) {
711 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);710 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
712 return self.openFileW(&path_w, flags);711 return self.openFileW(&path_w, flags);
...@@ -756,7 +755,6 @@ pub const Dir = struct {...@@ -756,7 +755,6 @@ pub const Dir = struct {
756 /// Call `File.close` on the result when done.755 /// Call `File.close` on the result when done.
757 /// Asserts that the path parameter has no null bytes.756 /// Asserts that the path parameter has no null bytes.
758 pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {757 pub fn createFile(self: Dir, sub_path: []const u8, flags: File.CreateFlags) File.OpenError!File {
759 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
760 if (builtin.os.tag == .windows) {758 if (builtin.os.tag == .windows) {
761 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);759 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
762 return self.createFileW(&path_w, flags);760 return self.createFileW(&path_w, flags);
...@@ -909,7 +907,6 @@ pub const Dir = struct {...@@ -909,7 +907,6 @@ pub const Dir = struct {
909 ///907 ///
910 /// Asserts that the path parameter has no null bytes.908 /// Asserts that the path parameter has no null bytes.
911 pub fn openDirTraverse(self: Dir, sub_path: []const u8) OpenError!Dir {909 pub fn openDirTraverse(self: Dir, sub_path: []const u8) OpenError!Dir {
912 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
913 if (builtin.os.tag == .windows) {910 if (builtin.os.tag == .windows) {
914 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);911 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
915 return self.openDirTraverseW(&sub_path_w);912 return self.openDirTraverseW(&sub_path_w);
...@@ -927,7 +924,6 @@ pub const Dir = struct {...@@ -927,7 +924,6 @@ pub const Dir = struct {
927 ///924 ///
928 /// Asserts that the path parameter has no null bytes.925 /// Asserts that the path parameter has no null bytes.
929 pub fn openDirList(self: Dir, sub_path: []const u8) OpenError!Dir {926 pub fn openDirList(self: Dir, sub_path: []const u8) OpenError!Dir {
930 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
931 if (builtin.os.tag == .windows) {927 if (builtin.os.tag == .windows) {
932 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);928 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
933 return self.openDirListW(&sub_path_w);929 return self.openDirListW(&sub_path_w);
...@@ -1091,7 +1087,6 @@ pub const Dir = struct {...@@ -1091,7 +1087,6 @@ pub const Dir = struct {
1091 /// To delete a directory recursively, see `deleteTree`.1087 /// To delete a directory recursively, see `deleteTree`.
1092 /// Asserts that the path parameter has no null bytes.1088 /// Asserts that the path parameter has no null bytes.
1093 pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {1089 pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {
1094 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
1095 if (builtin.os.tag == .windows) {1090 if (builtin.os.tag == .windows) {
1096 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);1091 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
1097 return self.deleteDirW(&sub_path_w);1092 return self.deleteDirW(&sub_path_w);
...@@ -1121,7 +1116,6 @@ pub const Dir = struct {...@@ -1121,7 +1116,6 @@ pub const Dir = struct {
1121 /// The return value is a slice of `buffer`, from index `0`.1116 /// The return value is a slice of `buffer`, from index `0`.
1122 /// Asserts that the path parameter has no null bytes.1117 /// Asserts that the path parameter has no null bytes.
1123 pub fn readLink(self: Dir, sub_path: []const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {1118 pub fn readLink(self: Dir, sub_path: []const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
1124 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
1125 const sub_path_c = try os.toPosixPath(sub_path);1119 const sub_path_c = try os.toPosixPath(sub_path);
1126 return self.readLinkC(&sub_path_c, buffer);1120 return self.readLinkC(&sub_path_c, buffer);
1127 }1121 }
lib/std/os.zig+1-1
...@@ -1355,7 +1355,6 @@ pub const UnlinkatError = UnlinkError || error{...@@ -1355,7 +1355,6 @@ pub const UnlinkatError = UnlinkError || error{
1355/// Delete a file name and possibly the file it refers to, based on an open directory handle.1355/// Delete a file name and possibly the file it refers to, based on an open directory handle.
1356/// Asserts that the path parameter has no null bytes.1356/// Asserts that the path parameter has no null bytes.
1357pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {1357pub fn unlinkat(dirfd: fd_t, file_path: []const u8, flags: u32) UnlinkatError!void {
1358 if (std.debug.runtime_safety) for (file_path) |byte| assert(byte != 0);
1359 if (builtin.os.tag == .windows) {1358 if (builtin.os.tag == .windows) {
1360 const file_path_w = try windows.sliceToPrefixedFileW(file_path);1359 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
1361 return unlinkatW(dirfd, &file_path_w, flags);1360 return unlinkatW(dirfd, &file_path_w, flags);
...@@ -3241,6 +3240,7 @@ pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t {...@@ -3241,6 +3240,7 @@ pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t {
3241/// Used to convert a slice to a null terminated slice on the stack.3240/// Used to convert a slice to a null terminated slice on the stack.
3242/// TODO https://github.com/ziglang/zig/issues/2873241/// TODO https://github.com/ziglang/zig/issues/287
3243pub fn toPosixPath(file_path: []const u8) ![PATH_MAX - 1:0]u8 {3242pub fn toPosixPath(file_path: []const u8) ![PATH_MAX - 1:0]u8 {
3243 if (std.debug.runtime_safety) assert(std.mem.indexOfScalar(u8, file_path, 0) == null);
3244 var path_with_null: [PATH_MAX - 1:0]u8 = undefined;3244 var path_with_null: [PATH_MAX - 1:0]u8 = undefined;
3245 // >= rather than > to make room for the null byte3245 // >= rather than > to make room for the null byte
3246 if (file_path.len >= PATH_MAX) return error.NameTooLong;3246 if (file_path.len >= PATH_MAX) return error.NameTooLong;