summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGabriel Schneider <[email protected]>2026-11-06 12:38:55 -0300
committerGabriel Schneider <[email protected]>2026-11-06 14:56:36 -0300
commitdde4979f59bec754b7909a81a40763850f3c29c0 (patch)
treec885f4ddc37f0564b4b20345c1a0f02672663252 /src
parent4c194047d210ad9cf571c6019731d00d1e3615a4 (diff)
downloadpardes-dde4979f59bec754b7909a81a40763850f3c29c0.tar.gz
pardes-dde4979f59bec754b7909a81a40763850f3c29c0.zip
idioms #5, more formats that cannot overflow: /proc paths of a pid, the daemon's pty, close and clients words, a pid or a serial for the environment, a capture's header and status line and a cache's .new name are catch unreachable with their reason, where a catch return, null or continue stood for an overflow no buffer here can have
Co-Authored-By: Claude Fable 5.1 <[email protected]> Claude-Session: https://claude.ai/code/session_016Y9MNAtZenFv4weoMD3cge
Diffstat (limited to 'src')
-rw-r--r--src/9p_io.zig2
-rw-r--r--src/Db.zig2
-rw-r--r--src/daemon.zig9
-rw-r--r--src/gui/gui.zig4
-rw-r--r--src/host_io.zig16
5 files changed, 17 insertions, 16 deletions
diff --git a/src/9p_io.zig b/src/9p_io.zig
index 10109bb8..bc122109 100644
--- a/src/9p_io.zig
+++ b/src/9p_io.zig
@@ -1737,7 +1737,7 @@ pub fn exportEnv(serial: u32, adopts: bool) void {
const socket: ?[]const u8 = if (own_socket_len > 0) own_socket[0..own_socket_len] else null;
const ChildEnv = @import("host_io.zig").ChildEnv;
var pid_buf: [16]u8 = undefined;
- ChildEnv.exportVar("PARDES_PID", if (adopts) std.fmt.bufPrint(&pid_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch null else null);
+ ChildEnv.exportVar("PARDES_PID", if (adopts) std.fmt.bufPrint(&pid_buf, "{d}", .{@as(u32, @intCast(libc.getpid()))}) catch unreachable else null); // a u32 fits 16
if (socket) |own| {
var buf: [16]u8 = undefined;
ChildEnv.exportVar("PARDES_9P", own);
diff --git a/src/Db.zig b/src/Db.zig
index 27557ac4..f0bfc3f5 100644
--- a/src/Db.zig
+++ b/src/Db.zig
@@ -1115,7 +1115,7 @@ pub const Cache = struct {
}{ .cap = c.cap, .went = &went }) catch return;
if (!went) return;
var new_buf: [4200]u8 = undefined;
- const new = std.fmt.bufPrint(&new_buf, "{s}.new", .{c.path()}) catch return;
+ const new = std.fmt.bufPrint(&new_buf, "{s}.new", .{c.path()}) catch unreachable; // a path of 4096 fits 4200
const cwd = std.Io.Dir.cwd();
const file = cwd.createFile(io, new, .{ .read = true, .truncate = true }) catch return;
var buffer: std.Io.Writer.Allocating = .init(std.heap.c_allocator);
diff --git a/src/daemon.zig b/src/daemon.zig
index a4e2c2d9..118fa58a 100644
--- a/src/daemon.zig
+++ b/src/daemon.zig
@@ -341,14 +341,14 @@ fn tell(words: []const u8, fds: []const c_int) void {
/// A shell forked on `master` (host_io.forkShell): the daemon keeps a dup.
pub fn pty(pid: libc.pid_t, master: c_int) void {
var buf: [32]u8 = undefined;
- tell(std.fmt.bufPrint(&buf, "pty {d}", .{pid}) catch return, &.{master});
+ tell(std.fmt.bufPrint(&buf, "pty {d}", .{pid}) catch unreachable, &.{master}); // a pid fits 32
}
/// The pty of `pid` closed here: the daemon lets its dup go too.
pub fn closed(pid: libc.pid_t) void {
if (pid <= 0) return;
var buf: [32]u8 = undefined;
- tell(std.fmt.bufPrint(&buf, "close {d}", .{pid}) catch return, &.{});
+ tell(std.fmt.bufPrint(&buf, "close {d}", .{pid}) catch unreachable, &.{}); // a pid fits 32
}
pub fn setState(detached: bool) void {
@@ -360,8 +360,9 @@ pub fn setState(detached: bool) void {
pub fn clients(pids: []const libc.pid_t) void {
var buf: [64]u8 = undefined;
var w: std.Io.Writer = .fixed(&buf);
- w.writeAll("clients") catch return;
- for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch return;
+ // "clients" and max_clients pids fit 64.
+ w.writeAll("clients") catch unreachable;
+ for (pids[0..@min(pids.len, max_clients)]) |pid| w.print(" {d}", .{pid}) catch unreachable;
tell(w.buffered(), &.{});
}
diff --git a/src/gui/gui.zig b/src/gui/gui.zig
index c149d913..d2e17508 100644
--- a/src/gui/gui.zig
+++ b/src/gui/gui.zig
@@ -7165,7 +7165,7 @@ fn writeTestStatus(g: *const Gui, core: *const pardes.Pardes, presented: bool) v
!g.glide.moving() and g.scroll_lag == 0 and g.scroll_delta == 0 and g.scroll_notch == 0;
var line_buf: [64]u8 = undefined;
// What keeps it busy, after: owes a frame, moves, waits, not shown.
- const line = std.fmt.bufPrint(&line_buf, "{d} {d} {d} {d}{d}{d}{d}\n", .{ test_status_step, g.captured, @intFromBool(idle), @intFromBool(core.needs_frame), @intFromBool(frontendMoving(g)), @intFromBool(core.nextWake() != null), @intFromBool(!presented) }) catch return;
+ const line = std.fmt.bufPrint(&line_buf, "{d} {d} {d} {d}{d}{d}{d}\n", .{ test_status_step, g.captured, @intFromBool(idle), @intFromBool(core.needs_frame), @intFromBool(frontendMoving(g)), @intFromBool(core.nextWake() != null), @intFromBool(!presented) }) catch unreachable; // seven numbers fit 64
var tmp_buf: [4096]u8 = undefined;
var final_buf: [4096]u8 = undefined;
const tmp_path = std.fmt.bufPrintSentinel(&tmp_buf, "{s}/status.tmp", .{g.capture_dir}, 0) catch return;
@@ -7199,7 +7199,7 @@ fn writeCapturePpm(g: *Gui, gpa: std.mem.Allocator, pixels: []const u8, width: u
defer _ = libc.close(fd);
var header: [64]u8 = undefined;
- const hdr = std.fmt.bufPrint(&header, "P6\n{d} {d}\n255\n", .{ width, height }) catch return error.CaptureWriteFailed;
+ const hdr = std.fmt.bufPrint(&header, "P6\n{d} {d}\n255\n", .{ width, height }) catch unreachable; // two u32s fit 64
_ = host_io.writeFd(fd, hdr);
const row_rgb = try gpa.alloc(u8, @as(usize, width) * 3);
diff --git a/src/host_io.zig b/src/host_io.zig
index 3fed6e1d..1b82e6bc 100644
--- a/src/host_io.zig
+++ b/src/host_io.zig
@@ -2136,7 +2136,7 @@ pub fn commandEnv(arena: std.mem.Allocator, serial: u32, adopts: bool) ![]const
fn exportWinid(serial: u32) void {
var buf: [16]u8 = undefined;
- ChildEnv.exportVar("winid", if (serial == 0) null else std.fmt.bufPrint(&buf, "{d}", .{serial}) catch null);
+ ChildEnv.exportVar("winid", if (serial == 0) null else std.fmt.bufPrint(&buf, "{d}", .{serial}) catch unreachable); // a u32 fits 16
}
/// The longest line a command pane runs (exec.zig refuses a longer one).
@@ -2404,7 +2404,7 @@ pub fn processExe(pid: libc.pid_t, buf: []u8) ?[]const u8 {
}
if (comptime builtin.os.tag != .linux) return null;
var pbuf: [64]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/exe", .{pid}, 0) catch return null;
+ const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/exe", .{pid}, 0) catch unreachable; // a pid fits 64
const n = libc.readlink(path, buf.ptr, buf.len);
if (n <= 0 or n >= buf.len) return null;
return buf[0..@intCast(n)];
@@ -2414,7 +2414,7 @@ pub fn shellCwd(pid: libc.pid_t, buf: []u8) ?[]const u8 {
switch (builtin.os.tag) {
.linux => {
var pbuf: [64]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/cwd", .{pid}, 0) catch return null;
+ const path = std.fmt.bufPrintSentinel(&pbuf, "/proc/{d}/cwd", .{pid}, 0) catch unreachable; // a pid fits 64
const n = libc.readlink(path, buf.ptr, buf.len);
if (n <= 0 or n >= buf.len) return null;
const link = buf[0..@intCast(n)];
@@ -3027,7 +3027,7 @@ fn signalSession(sid: libc.pid_t, sig: libc.SIG) void {
const name = std.mem.sliceTo(&ent.name, 0);
const pid = std.fmt.parseInt(libc.pid_t, name, 10) catch continue;
var path_buf: [64:0]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&path_buf, "/proc/{d}/stat", .{pid}, 0) catch continue;
+ const path = std.fmt.bufPrintSentinel(&path_buf, "/proc/{d}/stat", .{pid}, 0) catch unreachable; // a pid fits 64
const ids = parseGroupSession(readProc(path, &buf) orelse continue) orelse continue;
if (ids.session == sid and ids.pgrp != sid) _ = libc.kill(-ids.pgrp, sig);
}
@@ -3062,7 +3062,7 @@ pub fn foregroundName(shell_pid: libc.pid_t, master_fd: c_int, buf: []u8) ?[]con
if (fg <= 0 or fg == shell_pid) return null;
if (comptime builtin.os.tag == .freebsd) return std.Io.Dir.path.basename(processExe(fg, buf) orelse return null);
var name: [64:0]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/comm", .{@as(u32, @intCast(fg))}, 0) catch return null;
+ const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/comm", .{@as(u32, @intCast(fg))}, 0) catch unreachable; // a u32 fits 64
const comm = std.mem.trimEnd(u8, readProc(path, buf) orelse return null, "\n");
return if (comm.len > 0) comm else null;
}
@@ -3078,13 +3078,13 @@ fn readProc(path: [*:0]const u8, buf: []u8) ?[]const u8 {
fn procPgrp(pid: libc.pid_t, buf: *[4096]u8) ?libc.pid_t {
var name: [64:0]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/stat", .{@as(u32, @intCast(pid))}, 0) catch return null;
+ const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/stat", .{@as(u32, @intCast(pid))}, 0) catch unreachable; // a u32 fits 64
return (parseGroupSession(readProc(path, buf) orelse return null) orelse return null).pgrp;
}
fn offTty(pid: libc.pid_t, buf: *[4096]u8) bool {
var name: [64:0]u8 = undefined;
- const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/status", .{@as(u32, @intCast(pid))}, 0) catch return false;
+ const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/status", .{@as(u32, @intCast(pid))}, 0) catch unreachable; // a u32 fits 64
const status = readProc(path, buf) orelse return true;
return parseZombie(status);
}
@@ -3105,7 +3105,7 @@ fn pushChildren(probe: *TtyProbe, pending: *usize, pid: libc.pid_t, depth: u8) P
var name: [96:0]u8 = undefined;
const path = std.fmt.bufPrintSentinel(&name, "/proc/{d}/task/{d}/children", .{
@as(u32, @intCast(pid)), @as(u32, @intCast(pid)),
- }, 0) catch return .unreadable;
+ }, 0) catch unreachable; // two u32s fit 96
const fd = libc.open(path, .{ .ACCMODE = .RDONLY });
if (fd < 0) return .unreadable;
defer _ = libc.close(fd);