repos / zmx

session persistence for terminal processes
git clone https://github.com/neurosnap/zmx.git

commit
e5a0447
parent
d3a13c3
author
Eric Bower
date
2025-11-26 19:58:30 -0500 EST
fix: properly allocate NUL-terminated copies of each argument

the child process will exec (replacing memory) or exit, so no cleanup needed.
1 files changed,  +6, -9
M src/main.zig
+6, -9
 1@@ -445,7 +445,6 @@ fn clientLoop(_: *Cfg, client_sock_fd: i32) !void {
 2         }
 3 
 4         _ = posix.poll(poll_fds.items, -1) catch |err| {
 5-            if (err == error.SystemResources) continue;
 6             return err;
 7         };
 8 
 9@@ -568,10 +567,6 @@ fn daemonLoop(daemon: *Daemon, server_sock_fd: i32, pty_fd: i32) !void {
10         }
11 
12         _ = posix.poll(poll_fds.items, -1) catch |err| {
13-            if (err == error.SystemResources) {
14-                // Interrupted by signal (EINTR) - check flags (e.g. child exit) and continue
15-                continue;
16-            }
17             return err;
18         };
19 
20@@ -766,15 +761,17 @@ fn spawnPty(daemon: *Daemon) !c_int {
21         _ = c.putenv(@ptrCast(session_env.ptr));
22 
23         if (daemon.command) |cmd_args| {
24-            const cmd = cmd_args[0];
25+            const alloc = std.heap.c_allocator;
26             var argv_buf: [64:null]?[*:0]const u8 = undefined;
27             for (cmd_args, 0..) |arg, i| {
28-                argv_buf[i] = @ptrCast(arg.ptr);
29+                argv_buf[i] = alloc.dupeZ(u8, arg) catch {
30+                    std.posix.exit(1);
31+                };
32             }
33             argv_buf[cmd_args.len] = null;
34             const argv: [*:null]const ?[*:0]const u8 = &argv_buf;
35-            const err = std.posix.execvpeZ(@ptrCast(cmd.ptr), argv, std.c.environ);
36-            std.log.err("execvpe failed: cmd={s} err={s}", .{ cmd, @errorName(err) });
37+            const err = std.posix.execvpeZ(argv_buf[0].?, argv, std.c.environ);
38+            std.log.err("execvpe failed: cmd={s} err={s}", .{ cmd_args[0], @errorName(err) });
39             std.posix.exit(1);
40         } else {
41             const shell = std.posix.getenv("SHELL") orelse "/bin/sh";