Repository navigation
fix: checked pid conversion so debug and release agree (#179) - #180
Conversation
A u32 pid was turned into a signed id with `as i32`. u32::MAX became -1, and rustix's Pid::from_raw asserts against negative values in debug builds, so the proc test panicked in debug and passed in release. Every conversion now goes through one helper, proc::checked_pid, which returns None for a value that does not fit in i32 or is zero or negative. Callers take the path they already take for a process that does not exist: no signal is sent and has_exited reports true. Sites changed (poc/reverse-attach-linux/src): - proc.rs has_exited: Pid::from_raw(child.id() as i32) -> checked_pid(child.id()).and_then(Pid::from_raw) - proc.rs Child::stop: Pid::from_raw(child.id() as i32) -> checked_pid(child.id()).and_then(Pid::from_raw) - proc.rs kill_pid_checked: Pid::from_raw(pid as i32) -> checked_pid(pid).and_then(Pid::from_raw) - jobs/mod.rs signal and supervise_once: kill(-(pid as i32), sig) -> kill only when checked_pid(pid) is Some - tools/bash.rs: child.id() as i32 -> checked_pid; the kill(-pid) runs only when Some, and the reported pid is null when None - sweep_tests.rs: Pid::from_raw(helper as i32) -> checked_pid(helper).and_then(Pid::from_raw) Tests (proc_tests.rs): - a_pid_above_i32_max_is_treated_as_no_such_process (new): 2147483648, u32::MAX, u32::MAX - 1 - a_pid_that_does_not_exist_signals_nothing_and_does_not_panic (existing, u32::MAX and 0), now passes in debug Commands run on a Linux arm64 host, in a fresh clone of the pushed branch: - cargo test - cargo test --release - cargo fmt --check - cargo clippy -- -D warnings
|
Important CHANGES REQUESTED What This PR DoesRefs #179. Bare How It Works
Findings
Finding Details🟡 F1:
|
Refs #179
Problem
u32pids were cast toi32withas i32at several call sites. A value abovei32::MAXwraps to a negative number. In debug builds, rustix'sPid::from_rawassertsraw >= 0and panics. In release builds the assertion is compiled out, so the wrapped value reacheskill(). Debug and release therefore behaved differently.Change
Every conversion now goes through one helper,
proc::checked_pid. It returnsNoneif the value does not fit ini32, or if it is zero or negative. Each call site reuses its existing no-such-process path forNone.has_exitedPid::from_raw(self.child.id() as i32)checked_pid(self.child.id()).and_then(Pid::from_raw).Nonereturnstrue.Child::stopPid::from_raw(self.child.id() as i32)checked_pid(self.child.id()).and_then(Pid::from_raw).Noneskips the group signal.kill_pid_checkedPid::from_raw(pid as i32)checked_pid(pid).and_then(Pid::from_raw).Nonereturnsfalse.signalkill(-(self.pid as i32), sig)checked_pid(self.pid)isSome.supervise_oncekill(-(self.pid as i32), SIGKILL)checked_pid(self.pid)isSome.let pid = child.id() as i32;checked_pid(child.id()).kill(-pid)runs only onSome. The reported pid isnullonNone.Pid::from_raw(helper as i32)checked_pid(helper).and_then(Pid::from_raw)Valid pids follow the same code path as before. Before this change, pid 0 signalled the caller's own process group from
jobs/mod.rs, andu32::MAXsignalled init (pid 1); both are now skipped. Otheras i32casts in the tree (seat.rs,screen.rs,legacy.rs) convert values that are not pids, so they are unchanged.Tests
proc::tests::a_pid_above_i32_max_is_treated_as_no_such_process(inproc_tests.rs). It asserts thatkill_pid_checkedreturnsfalsefor 2147483648,u32::MAX, andu32::MAX - 1.a_pid_that_does_not_exist_signals_nothing_and_does_not_panicalso coversu32::MAX.Before the fix, with the new test kept and the four source files restored to the branch base,
cargo test(debug) gave 598 passed and 2 failed. Both failures were the two pid tests above, withassertion failed: raw >= 0from rustixpid.rs:44. The release build was not run before the fix.Results on the final head (b4b6130)
Run on a Linux aarch64 build host, each command in a fresh clone of the branch.
cargo testcargo test --releasecargo fmt --checkcargo clippy -- -D warningsBoth new
proctests pass in the debug and release runs.