fix: reject launchd pid sentinel values

Landed from contributor PR #39281 by @mvanhorn.

Co-authored-by: Matt Van Horn <mvanhorn@gmail.com>
This commit is contained in:
Peter Steinberger
2026-03-08 02:42:34 +00:00
parent 244aabb0cb
commit 7f44bc5e94
3 changed files with 22 additions and 1 deletions

View File

@@ -102,6 +102,26 @@ describe("launchd runtime parsing", () => {
lastExitReason: "exited",
});
});
it("does not set pid when pid = 0", () => {
const output = ["state = running", "pid = 0"].join("\n");
const info = parseLaunchctlPrint(output);
expect(info.pid).toBeUndefined();
expect(info.state).toBe("running");
});
it("sets pid for positive values", () => {
const output = ["state = running", "pid = 1234"].join("\n");
const info = parseLaunchctlPrint(output);
expect(info.pid).toBe(1234);
});
it("does not set pid for negative values", () => {
const output = ["state = waiting", "pid = -1"].join("\n");
const info = parseLaunchctlPrint(output);
expect(info.pid).toBeUndefined();
expect(info.state).toBe("waiting");
});
});
describe("launchctl list detection", () => {

View File

@@ -128,7 +128,7 @@ export function parseLaunchctlPrint(output: string): LaunchctlPrintInfo {
const pidValue = entries.pid;
if (pidValue) {
const pid = Number.parseInt(pidValue, 10);
if (Number.isFinite(pid)) {
if (Number.isFinite(pid) && pid > 0) {
info.pid = pid;
}
}