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
signature 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 {
706706 /// Call `File.close` to release the resource.
707707 /// Asserts that the path parameter has no null bytes.
708708 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);
710709 if (builtin.os.tag == .windows) {
711710 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
712711 return self.openFileW(&path_w, flags);
......@@ -756,7 +755,6 @@ pub const Dir = struct {
756755 /// Call `File.close` on the result when done.
757756 /// Asserts that the path parameter has no null bytes.
758757 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);
760758 if (builtin.os.tag == .windows) {
761759 const path_w = try os.windows.sliceToPrefixedFileW(sub_path);
762760 return self.createFileW(&path_w, flags);
......@@ -909,7 +907,6 @@ pub const Dir = struct {
909907 ///
910908 /// Asserts that the path parameter has no null bytes.
911909 pub fn openDirTraverse(self: Dir, sub_path: []const u8) OpenError!Dir {
912 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
913910 if (builtin.os.tag == .windows) {
914911 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
915912 return self.openDirTraverseW(&sub_path_w);
......@@ -927,7 +924,6 @@ pub const Dir = struct {
927924 ///
928925 /// Asserts that the path parameter has no null bytes.
929926 pub fn openDirList(self: Dir, sub_path: []const u8) OpenError!Dir {
930 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
931927 if (builtin.os.tag == .windows) {
932928 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
933929 return self.openDirListW(&sub_path_w);
......@@ -1091,7 +1087,6 @@ pub const Dir = struct {
10911087 /// To delete a directory recursively, see `deleteTree`.
10921088 /// Asserts that the path parameter has no null bytes.
10931089 pub fn deleteDir(self: Dir, sub_path: []const u8) DeleteDirError!void {
1094 if (std.debug.runtime_safety) for (sub_path) |byte| assert(byte != 0);
10951090 if (builtin.os.tag == .windows) {
10961091 const sub_path_w = try os.windows.sliceToPrefixedFileW(sub_path);
10971092 return self.deleteDirW(&sub_path_w);
......@@ -1121,7 +1116,6 @@ pub const Dir = struct {
11211116 /// The return value is a slice of `buffer`, from index `0`.
11221117 /// Asserts that the path parameter has no null bytes.
11231118 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);
11251119 const sub_path_c = try os.toPosixPath(sub_path);
11261120 return self.readLinkC(&sub_path_c, buffer);
11271121 }
lib/std/os.zig+1-1
......@@ -1355,7 +1355,6 @@ pub const UnlinkatError = UnlinkError || error{
13551355/// Delete a file name and possibly the file it refers to, based on an open directory handle.
13561356/// Asserts that the path parameter has no null bytes.
13571357pub 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);
13591358 if (builtin.os.tag == .windows) {
13601359 const file_path_w = try windows.sliceToPrefixedFileW(file_path);
13611360 return unlinkatW(dirfd, &file_path_w, flags);
......@@ -3241,6 +3240,7 @@ pub fn sched_getaffinity(pid: pid_t) SchedGetAffinityError!cpu_set_t {
32413240/// Used to convert a slice to a null terminated slice on the stack.
32423241/// TODO https://github.com/ziglang/zig/issues/287
32433242pub 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);
32443244 var path_with_null: [PATH_MAX - 1:0]u8 = undefined;
32453245 // >= rather than > to make room for the null byte
32463246 if (file_path.len >= PATH_MAX) return error.NameTooLong;