From cf901695338a7b39964c8eda465fd31ac69aa7a5 Mon Sep 17 00:00:00 2001 From: Rohit Goswami Date: Fri, 20 Nov 2026 12:58:15 +0000 Subject: [PATCH] fix: compress a long log name the reference compressor allows A run directory's log can carry a name close to a filesystem's own 255-byte limit on one path component. It can still sit well inside what the reference compressor at its highest setting allows. Its own overhead is just the three bytes of the `.gz` suffix it adds. `gzipInPlace` writes its compressed bytes to a temporary name first. That way a crash or a short write cannot leave a partial archive under the final name a later run would skip. The temporary name carried the original name in full, plus a pid and a random suffix guarding it from a concurrent or a retried run. Nothing checked whether the result still fit in one path component once that suffix was added on. A name as short as 232 bytes, well under the reference tool's own limit, could report NameTooLong. The pid and the random suffix alone are already enough to keep two temporary files from colliding in the same directory. The original name is left out of the temporary one when adding it on would overflow a path component. That way a compression the final name alone would have gone on to complete does not fail. A name under the reference compressor's own 252-byte limit compresses here too, regardless of the temporary name's own overhead. One past even that limit still reports NameTooLong, exactly as the reference tool itself would refuse it. A 240-byte name is picked so the prior temporary name overflows whatever the running process's own pid happens to be. It reports NameTooLong on the parent commit, and compresses here. A full `zig build test` run reports every other case still passing. --- .../+gzip-long-name-tmp-overflow.bugfix.md | 7 +++ src/logs.zig | 54 ++++++++++++++++++- 2 files changed, 60 insertions(+), 1 deletion(-) create mode 100644 changelog.d/+gzip-long-name-tmp-overflow.bugfix.md diff --git a/changelog.d/+gzip-long-name-tmp-overflow.bugfix.md b/changelog.d/+gzip-long-name-tmp-overflow.bugfix.md new file mode 100644 index 0000000..b1e1427 --- /dev/null +++ b/changelog.d/+gzip-long-name-tmp-overflow.bugfix.md @@ -0,0 +1,7 @@ +`gzipInPlace` refused to compress a log whose name, well within the +reference compressor's own limit, no longer fit a filesystem's 255-byte +limit on one path component once its temporary file's pid and random +suffix were added on. That suffix alone already makes the temporary +name unique, so the original name is left off a temporary name that +would otherwise overflow, and a name that reference tool would have +compressed compresses here too. diff --git a/src/logs.zig b/src/logs.zig index cd00ea4..4183275 100644 --- a/src/logs.zig +++ b/src/logs.zig @@ -13,6 +13,10 @@ const flate = std.compress.flate; /// path per line. `renameInList` gives its text after files are renamed. pub const licensed_list = ".licensed-logs"; +/// The longest a single path component may be on a Linux filesystem +/// holding log files: ext4, xfs, btrfs and tmpfs all share this limit. +const name_component_max = 255; + /// The errors of `gzipInPlace`: the `sys.Error` of a failed call, /// `error.OutOfMemory`, or `error.WriteFailed` from writing the /// compressed stream. @@ -42,7 +46,21 @@ pub fn gzipInPlace(gpa: Allocator, io: Io, dir: fsx.fd_t, name: []const u8) Gzip _ = try sys.call(linux.getrandom, .{ &rnd, rnd.len, 0 }); const gz_parent = std.fs.path.dirname(gz_name) orelse "."; const gz_base = std.fs.path.basename(gz_name); - const tmp_name = try gpa.print("{s}/.{s}.{d}.{x:0>8}.tmp", .{ gz_parent, gz_base, linux.getpid(), std.mem.readInt(u32, &rnd, .little) }); + const pid = linux.getpid(); + const suffix = std.mem.readInt(u32, &rnd, .little); + // A name this program's own `name.gz` already fits in one path + // component can still leave the temporary file holding both that name + // and the pid and random suffix guarding it from a concurrent or a + // retried run over that component's own limit. The suffix alone is + // already enough to keep two temporary files from colliding in `dir`, + // so a name that would overflow is left out of the temporary one + // rather than failing a compression this program's own final name + // would otherwise have gone on to complete. + const tagged_len = std.fmt.count(".{s}.{d}.{x:0>8}.tmp", .{ gz_base, pid, suffix }); + const tmp_name = if (tagged_len <= name_component_max) + try gpa.print("{s}/.{s}.{d}.{x:0>8}.tmp", .{ gz_parent, gz_base, pid, suffix }) + else + try gpa.print("{s}/.{d}.{x:0>8}.tmp", .{ gz_parent, pid, suffix }); defer gpa.free(tmp_name); const dst = try sys.openat(dir, tmp_name, .{ .ACCMODE = .WRONLY, @@ -386,3 +404,37 @@ test "gzipInPlace handles nested paths like gzip -9 sub/file" { try std.testing.expectError(error.NotFound, fsx.statAt(dir, "sub/nested.log", false)); try std.testing.expect((fsx.statAt(dir, "sub/nested.log.gz", false) catch null) != null); } + +test "gzipInPlace compresses a name its own temporary file alone would overflow" { + const gpa = std.testing.allocator; + var cwd_buf: [sys.path_max]u8 = undefined; + const here = try fsx.realpath(fsx.cwd, ".", &cwd_buf); + var pb: [sys.path_max]u8 = undefined; + const base = try std.mem.print(&pb, "{s}/.zig-cache/zig-common-logs-long-{d}", .{ here, linux.getpid() }); + var zb: [sys.path_max:0]u8 = undefined; + _ = linux.mkdirat(fsx.cwd, (try sys.z(&zb, base)).ptr, 0o700); + const dir = try fsx.openDir(fsx.cwd, base, false); + defer { + fsx.deleteTree(gpa, fsx.cwd, base) catch {}; + sys.close(dir); + } + + // 240 bytes: short enough that `gzip -9` on the real reference + // compressor takes it (its own overhead is the 3 bytes of `.gz`), long + // enough that adding a pid and a random suffix on top overflows one + // path component's 255-byte limit no matter how many digits the + // running process's own pid happens to have. + var name_buf: [240]u8 = undefined; + @memset(&name_buf, 'a'); + const name = &name_buf; + const src = try sys.openat(dir, name, .{ .ACCMODE = .WRONLY, .CREAT = true, .TRUNC = true }, 0o644); + try sys.writeAll(src, "a log with a long name\n"); + sys.close(src); + + const io = std.Io.Threaded.global_single_threaded.io(); + try gzipInPlace(gpa, io, dir, name); + try std.testing.expectError(error.NotFound, fsx.statAt(dir, name, false)); + var gz_buf: [244]u8 = undefined; + const gz_name = try std.fmt.bufPrint(&gz_buf, "{s}.gz", .{name}); + try std.testing.expect((fsx.statAt(dir, gz_name, false) catch null) != null); +}