Skip to content

Commit 0a70455

Browse files
ianprime0509andrewrk
authored andcommitted
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.
1 parent dffc8c4 commit 0a70455

File tree

3 files changed

+13
-6
lines changed

3 files changed

+13
-6
lines changed

lib/std/debug/Dwarf.zig

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2275,9 +2275,11 @@ pub const ElfModule = struct {
22752275
break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;
22762276
}
22772277
if (std.posix.getenv("XDG_CACHE_HOME")) |cache_path| {
2278-
const path = std.fs.path.join(gpa, &[_][]const u8{ cache_path, "debuginfod_client" }) catch break :blk;
2279-
defer gpa.free(path);
2280-
break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;
2278+
if (cache_path.len > 0) {
2279+
const path = std.fs.path.join(gpa, &[_][]const u8{ cache_path, "debuginfod_client" }) catch break :blk;
2280+
defer gpa.free(path);
2281+
break :dir std.fs.openDirAbsolute(path, .{}) catch break :blk;
2282+
}
22812283
}
22822284
if (std.posix.getenv("HOME")) |home_path| {
22832285
const path = std.fs.path.join(gpa, &[_][]const u8{ home_path, ".cache", "debuginfod_client" }) catch break :blk;

lib/std/fs/get_app_data_dir.zig

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ pub fn getAppDataDir(allocator: mem.Allocator, appname: []const u8) GetAppDataDi
3232
},
3333
.linux, .freebsd, .netbsd, .dragonfly, .openbsd, .solaris, .illumos => {
3434
if (posix.getenv("XDG_DATA_HOME")) |xdg| {
35-
return fs.path.join(allocator, &[_][]const u8{ xdg, appname });
35+
if (xdg.len > 0) {
36+
return fs.path.join(allocator, &[_][]const u8{ xdg, appname });
37+
}
3638
}
3739

3840
const home_dir = posix.getenv("HOME") orelse {

src/introspect.zig

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,11 @@ pub fn resolveGlobalCacheDir(allocator: mem.Allocator) ![]u8 {
9090

9191
if (builtin.os.tag != .windows) {
9292
if (std.zig.EnvVar.XDG_CACHE_HOME.getPosix()) |cache_root| {
93-
return fs.path.join(allocator, &[_][]const u8{ cache_root, appname });
94-
} else if (std.zig.EnvVar.HOME.getPosix()) |home| {
93+
if (cache_root.len > 0) {
94+
return fs.path.join(allocator, &[_][]const u8{ cache_root, appname });
95+
}
96+
}
97+
if (std.zig.EnvVar.HOME.getPosix()) |home| {
9598
return fs.path.join(allocator, &[_][]const u8{ home, ".cache", appname });
9699
}
97100
}

0 commit comments

Comments
 (0)