mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-09 10:07:41 +00:00
refactor: centralize strict numeric parsing
This commit is contained in:
@@ -122,6 +122,19 @@ describe("launchd runtime parsing", () => {
|
||||
expect(info.pid).toBeUndefined();
|
||||
expect(info.state).toBe("waiting");
|
||||
});
|
||||
|
||||
it("rejects pid and exit status values with junk suffixes", () => {
|
||||
const output = [
|
||||
"state = waiting",
|
||||
"pid = 123abc",
|
||||
"last exit status = 7ms",
|
||||
"last exit reason = exited",
|
||||
].join("\n");
|
||||
expect(parseLaunchctlPrint(output)).toEqual({
|
||||
state: "waiting",
|
||||
lastExitReason: "exited",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("launchctl list detection", () => {
|
||||
|
||||
@@ -1,5 +1,6 @@
|
||||
import fs from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
import { parseStrictInteger, parseStrictPositiveInteger } from "../infra/parse-finite-number.js";
|
||||
import {
|
||||
GATEWAY_LAUNCH_AGENT_LABEL,
|
||||
resolveGatewayServiceDescription,
|
||||
@@ -127,15 +128,15 @@ export function parseLaunchctlPrint(output: string): LaunchctlPrintInfo {
|
||||
}
|
||||
const pidValue = entries.pid;
|
||||
if (pidValue) {
|
||||
const pid = Number.parseInt(pidValue, 10);
|
||||
if (Number.isFinite(pid) && pid > 0) {
|
||||
const pid = parseStrictPositiveInteger(pidValue);
|
||||
if (pid !== undefined) {
|
||||
info.pid = pid;
|
||||
}
|
||||
}
|
||||
const exitStatusValue = entries["last exit status"];
|
||||
if (exitStatusValue) {
|
||||
const status = Number.parseInt(exitStatusValue, 10);
|
||||
if (Number.isFinite(status)) {
|
||||
const status = parseStrictInteger(exitStatusValue);
|
||||
if (status !== undefined) {
|
||||
info.lastExitStatus = status;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -351,6 +351,21 @@ describe("systemd runtime parsing", () => {
|
||||
execMainCode: "exited",
|
||||
});
|
||||
});
|
||||
|
||||
it("rejects pid and exit status values with junk suffixes", () => {
|
||||
const output = [
|
||||
"ActiveState=inactive",
|
||||
"SubState=dead",
|
||||
"MainPID=42abc",
|
||||
"ExecMainStatus=2ms",
|
||||
"ExecMainCode=exited",
|
||||
].join("\n");
|
||||
expect(parseSystemdShow(output)).toEqual({
|
||||
activeState: "inactive",
|
||||
subState: "dead",
|
||||
execMainCode: "exited",
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
describe("resolveSystemdUserUnitPath", () => {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import fs from "node:fs/promises";
|
||||
import os from "node:os";
|
||||
import path from "node:path";
|
||||
import { parseStrictInteger, parseStrictPositiveInteger } from "../infra/parse-finite-number.js";
|
||||
import { splitArgsPreservingQuotes } from "./arg-split.js";
|
||||
import {
|
||||
LEGACY_GATEWAY_SYSTEMD_SERVICE_NAMES,
|
||||
@@ -231,15 +232,15 @@ export function parseSystemdShow(output: string): SystemdServiceInfo {
|
||||
}
|
||||
const mainPidValue = entries.mainpid;
|
||||
if (mainPidValue) {
|
||||
const pid = Number.parseInt(mainPidValue, 10);
|
||||
if (Number.isFinite(pid) && pid > 0) {
|
||||
const pid = parseStrictPositiveInteger(mainPidValue);
|
||||
if (pid !== undefined) {
|
||||
info.mainPid = pid;
|
||||
}
|
||||
}
|
||||
const execMainStatusValue = entries.execmainstatus;
|
||||
if (execMainStatusValue) {
|
||||
const status = Number.parseInt(execMainStatusValue, 10);
|
||||
if (Number.isFinite(status)) {
|
||||
const status = parseStrictInteger(execMainStatusValue);
|
||||
if (status !== undefined) {
|
||||
info.execMainStatus = status;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user