From 0a70455095a19a4c18497e6786ca6139c1cc2892 Mon Sep 17 00:00:00 2001 From: Ian Johnson Date: Mon, 19 Aug 2024 23:11:32 -0400 Subject: [PATCH] Fix handling of empty XDG environment variables Closes #21132 According to the XDG Base Directory specification (https://specifications.freedesktop.org/basedir-spec/latest/#variables), empty values for these environment variables should be treated the same as if they are unset. Specifically, for the instances changed in this commit, > $XDG_DATA_HOME defines the base directory relative to which > user-specific data files should be stored. If $XDG_DATA_HOME is either > not set **or empty**, a default equal to $HOME/.local/share should be > used. and > $XDG_CACHE_HOME defines the base directory relative to which > user-specific non-essential data files should be stored. If > $XDG_CACHE_HOME is either not set **or empty**, a default equal to > $HOME/.cache should be used. (emphasis mine) In addition to the case mentioned in the linked issue, all other uses of XDG environment variables were corrected. --- lib/std/debug/Dwarf.zig | 8 +++++--- lib/std/fs/get_app_data_dir.zig | 4 +++- src/introspect.zig | 7 +++++-- 3 files changed, 13 insertions(+), 6 deletions(-) diff --git a/lib/std/debug/Dwarf.zig b/lib/std/debug/Dwarf.zig index fb72316675d035a93ef9e7448fde9dfbb55dcdc8..7cce30df384e34acb7f6f45b8ded957aae4f1c89 100644 --- a/lib/std/debug/Dwarf.zig +++ b/lib/std/debug/Dwarf.zig @@ -2275,9 +2275,11 @@ pub const ElfModule = struct { break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk; } if (std.posix.getenv("XDG_CACHE_HOME")) |cache_path| { - const path = std.fs.path.join(gpa, &[_][]const u8{ cache_path, "debuginfod_client" }) catch break :blk; - defer gpa.free(path); - break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk; + if (cache_path.len > 0) { + const path = std.fs.path.join(gpa, &[_][]const u8{ cache_path, "debuginfod_client" }) catch break :blk; + defer gpa.free(path); + break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk; + } } if (std.posix.getenv("HOME")) |home_path| { const path = std.fs.path.join(gpa, &[_][]const u8{ home_path, ".cache", "debuginfod_client" }) catch break :blk; diff --git a/lib/std/fs/get_app_data_dir.zig b/lib/std/fs/get_app_data_dir.zig index a05a4316486a677c95ad1556bccc446569c78d65..cce99ea8ccc87163b7c866376dd50ed25b4b96c3 100644 --- a/lib/std/fs/get_app_data_dir.zig +++ b/lib/std/fs/get_app_data_dir.zig @@ -32,7 +32,9 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi }, .linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => { if (posix.getenv("XDG_DATA_HOME")) |xdg| { - return fs.path.join(allocator, &[_][]const u8{ xdg, appname }); + if (xdg.len > 0) { + return fs.path.join(allocator, &[_][]const u8{ xdg, appname }); + } } const home_dir = posix.getenv("HOME") orelse { diff --git a/src/introspect.zig b/src/introspect.zig index 341b1cddeb82ad426198f2965b24fc01c2843aa9..4193440461b4bbbd9914ba6b74a63c7121413ad3 100644 --- a/src/introspect.zig +++ b/src/introspect.zig @@ -90,8 +90,11 @@ pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 { if (builtin.os.tag != .windows) { if (std.zig.EnvVar.XDG_CACHE_HOME.getPosix()) |cache_root| { - return fs.path.join(allocator, &[_][]const u8{ cache_root, appname }); - } else if (std.zig.EnvVar.HOME.getPosix()) |home| { + if (cache_root.len > 0) { + return fs.path.join(allocator, &[_][]const u8{ cache_root, appname }); + } + } + if (std.zig.EnvVar.HOME.getPosix()) |home| { return fs.path.join(allocator, &[_][]const u8{ home, ".cache", appname }); } } -- 2.54.0