authorgravatar for gereeter+code@gmail.comJonathan S <gereeter+code@gmail.com> 2020-03-27 14:28:48-05:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-05-29 18:23:06-04:00
log631633b25253201968b97cf129d986def37eed4a
treea09677ad9b9e71568c4ad384ac48a2a71710f628
parent4c8b937fb016a54b09d7447ca634b7cf1af78fce

Document and reduce usage of MAX_PATH_BYTES, lifting arbitrary buffer size requirements


4 files changed, 36 insertions(+), 11 deletions(-)

lib/std/debug.zig+3
......@@ -1283,6 +1283,9 @@ pub const DebugInfo = struct {
12831283 const elf_path = if (ctx.name.len > 0)
12841284 ctx.name
12851285 else blk: {
1286 // Use of MAX_PATH_BYTES here is valid as the resulting path is immediately
1287 // opened with no modification. TODO: Use openSelfExe instead to avoid path
1288 // length limitations
12861289 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
12871290 break :blk try fs.selfExePath(&buf);
12881291 };
lib/std/fs.zig+28-10
......@@ -33,8 +33,11 @@ pub const GetAppDataDirError = @import("fs/get_app_data_dir.zig").GetAppDataDirE
3333
3434pub const Watch = @import("fs/watch.zig").Watch;
3535
36/// This represents the maximum size of a UTF-8 encoded file path.
37/// All file system operations which return a path are guaranteed to
36/// This represents the maximum size of a UTF-8 encoded file path that the
37/// operating system will accept. Paths, including those returned from file
38/// system operations, may be longer than this length, but such paths cannot
39/// be successfully passed back in other file system operations. However,
40/// all path components returned by file system operations are assumed to
3841/// fit into a UTF-8 encoded array of this length.
3942/// The byte count includes room for a null sentinel byte.
4043pub const MAX_PATH_BYTES = switch (builtin.os.tag) {
......@@ -1194,7 +1197,7 @@ pub const Dir = struct {
11941197 /// Read value of a symbolic link.
11951198 /// The return value is a slice of `buffer`, from index `0`.
11961199 /// Asserts that the path parameter has no null bytes.
1197 pub fn readLink(self: Dir, sub_path: []const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
1200 pub fn readLink(self: Dir, sub_path: []const u8, buffer: []u8) ![]u8 {
11981201 const sub_path_c = try os.toPosixPath(sub_path);
11991202 return self.readLinkZ(&sub_path_c, buffer);
12001203 }
......@@ -1202,7 +1205,7 @@ pub const Dir = struct {
12021205 pub const readLinkC = @compileError("deprecated: renamed to readLinkZ");
12031206
12041207 /// Same as `readLink`, except the `pathname` parameter is null-terminated.
1205 pub fn readLinkZ(self: Dir, sub_path_c: [*:0]const u8, buffer: *[MAX_PATH_BYTES]u8) ![]u8 {
1208 pub fn readLinkZ(self: Dir, sub_path_c: [*:0]const u8, buffer: []u8) ![]u8 {
12061209 return os.readlinkatZ(self.fd, sub_path_c, buffer);
12071210 }
12081211
......@@ -1320,6 +1323,9 @@ pub const Dir = struct {
13201323 var cleanup_dir = true;
13211324 defer if (cleanup_dir) dir.close();
13221325
1326 // Likely valid use of MAX_PATH_BYTES, as dir_name_buf will only
1327 // ever store a single path component that was returned from the
1328 // filesystem.
13231329 var dir_name_buf: [MAX_PATH_BYTES]u8 = undefined;
13241330 var dir_name: []const u8 = sub_path;
13251331
......@@ -1781,6 +1787,8 @@ pub fn openSelfExe() OpenSelfExeError!File {
17811787 const prefixed_path_w = try os.windows.wToPrefixedFileW(wide_slice);
17821788 return cwd().openFileW(prefixed_path_w.span(), .{});
17831789 }
1790 // Use of MAX_PATH_BYTES here is valid as the resulting path is immediately
1791 // opened with no modification.
17841792 var buf: [MAX_PATH_BYTES]u8 = undefined;
17851793 const self_exe_path = try selfExePath(&buf);
17861794 buf[self_exe_path.len] = 0;
......@@ -1792,6 +1800,8 @@ pub const SelfExePathError = os.ReadLinkError || os.SysCtlError;
17921800/// `selfExePath` except allocates the result on the heap.
17931801/// Caller owns returned memory.
17941802pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 {
1803 // TODO(#4812): Consider looping with larger and larger buffers to handle
1804 // overlong paths.
17951805 var buf: [MAX_PATH_BYTES]u8 = undefined;
17961806 return mem.dupe(allocator, u8, try selfExePath(&buf));
17971807}
......@@ -1806,10 +1816,10 @@ pub fn selfExePathAlloc(allocator: *Allocator) ![]u8 {
18061816/// On Linux, depends on procfs being mounted. If the currently executing binary has
18071817/// been deleted, the file path looks something like `/a/b/c/exe (deleted)`.
18081818/// TODO make the return type of this a null terminated pointer
1809pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
1819pub fn selfExePath(out_buffer: []u8) SelfExePathError![]u8 {
18101820 if (is_darwin) {
1811 var u32_len: u32 = out_buffer.len;
1812 const rc = std.c._NSGetExecutablePath(out_buffer, &u32_len);
1821 var u32_len: u32 = @intCast(u32, math.min(out_buffer.len, math.maxInt(u32)));
1822 const rc = std.c._NSGetExecutablePath(out_buffer.ptr, &u32_len);
18131823 if (rc != 0) return error.NameTooLong;
18141824 return mem.spanZ(@ptrCast([*:0]u8, out_buffer));
18151825 }
......@@ -1818,14 +1828,14 @@ pub fn selfExePath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]u8 {
18181828 .freebsd, .dragonfly => {
18191829 var mib = [4]c_int{ os.CTL_KERN, os.KERN_PROC, os.KERN_PROC_PATHNAME, -1 };
18201830 var out_len: usize = out_buffer.len;
1821 try os.sysctl(&mib, out_buffer, &out_len, null, 0);
1831 try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);
18221832 // TODO could this slice from 0 to out_len instead?
18231833 return mem.spanZ(@ptrCast([*:0]u8, out_buffer));
18241834 },
18251835 .netbsd => {
18261836 var mib = [4]c_int{ os.CTL_KERN, os.KERN_PROC_ARGS, -1, os.KERN_PROC_PATHNAME };
18271837 var out_len: usize = out_buffer.len;
1828 try os.sysctl(&mib, out_buffer, &out_len, null, 0);
1838 try os.sysctl(&mib, out_buffer.ptr, &out_len, null, 0);
18291839 // TODO could this slice from 0 to out_len instead?
18301840 return mem.spanZ(@ptrCast([*:0]u8, out_buffer));
18311841 },
......@@ -1848,13 +1858,15 @@ pub fn selfExePathW() [:0]const u16 {
18481858/// `selfExeDirPath` except allocates the result on the heap.
18491859/// Caller owns returned memory.
18501860pub fn selfExeDirPathAlloc(allocator: *Allocator) ![]u8 {
1861 // TODO(#4812): Consider looping with larger and larger buffers to handle
1862 // overlong paths.
18511863 var buf: [MAX_PATH_BYTES]u8 = undefined;
18521864 return mem.dupe(allocator, u8, try selfExeDirPath(&buf));
18531865}
18541866
18551867/// Get the directory path that contains the current executable.
18561868/// Returned value is a slice of out_buffer.
1857pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const u8 {
1869pub fn selfExeDirPath(out_buffer: []u8) SelfExePathError![]const u8 {
18581870 const self_exe_path = try selfExePath(out_buffer);
18591871 // Assume that the OS APIs return absolute paths, and therefore dirname
18601872 // will not return null.
......@@ -1864,6 +1876,12 @@ pub fn selfExeDirPath(out_buffer: *[MAX_PATH_BYTES]u8) SelfExePathError![]const
18641876/// `realpath`, except caller must free the returned memory.
18651877/// TODO integrate with `Dir`
18661878pub fn realpathAlloc(allocator: *Allocator, pathname: []const u8) ![]u8 {
1879 // Use of MAX_PATH_BYTES here is valid as the realpath function does not
1880 // have a variant that takes an arbitrary-size buffer.
1881 // TODO(#4812): Consider reimplementing realpath or using the POSIX.1-2008
1882 // NULL out parameter (GNU's canonicalize_file_name) to handle overelong
1883 // paths. musl supports passing NULL but restricts the output to PATH_MAX
1884 // anyway.
18671885 var buf: [MAX_PATH_BYTES]u8 = undefined;
18681886 return mem.dupe(allocator, u8, try os.realpath(pathname, &buf));
18691887}
lib/std/os.zig+2
......@@ -1236,6 +1236,8 @@ pub fn execvpeZ_expandArg0(
12361236 if (mem.indexOfScalar(u8, file_slice, '/') != null) return execveZ(file, child_argv, envp);
12371237
12381238 const PATH = getenvZ("PATH") orelse "/usr/local/bin:/bin/:/usr/bin";
1239 // Use of MAX_PATH_BYTES here is valid as the path_buf will be passed
1240 // directly to the operating system in execveZ.
12391241 var path_buf: [MAX_PATH_BYTES]u8 = undefined;
12401242 var it = mem.tokenize(PATH, ":");
12411243 var seen_eacces = false;
lib/std/process.zig+3-1
......@@ -15,12 +15,14 @@ pub const changeCurDir = os.chdir;
1515pub const changeCurDirC = os.chdirC;
1616
1717/// The result is a slice of `out_buffer`, from index `0`.
18pub fn getCwd(out_buffer: *[fs.MAX_PATH_BYTES]u8) ![]u8 {
18pub fn getCwd(out_buffer: []u8) ![]u8 {
1919 return os.getcwd(out_buffer);
2020}
2121
2222/// Caller must free the returned memory.
2323pub fn getCwdAlloc(allocator: *Allocator) ![]u8 {
24 // TODO(#4812): Consider looping with larger and larger buffers to handle
25 // overlong paths.
2426 var buf: [fs.MAX_PATH_BYTES]u8 = undefined;
2527 return mem.dupe(allocator, u8, try os.getcwd(&buf));
2628}