summaryrefslogtreecommitdiff
path: root/src/host_io.zig
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/host_io.zig
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/host_io.zig')
-rw-r--r--src/host_io.zig16
1 files changed, 8 insertions, 8 deletions
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);