

























@@ -1678,6 +1678,9 @@ async function samplePosixProcessWithDescendants(pid, run) {
16781678timeoutMs: 5000,
16791679});
16801680const rows = parsePosixProcessRows(stdout);
1681+if (!rows) {
1682+return null;
1683+}
16811684const selected = rows.find((row) => row.processId === safePid);
16821685if (!selected) {
16831686return null;
@@ -1698,6 +1701,9 @@ async function samplePosixProcessTree(pid, run, commandLineNeedles) {
16981701timeoutMs: 5000,
16991702});
17001703const rows = parsePosixProcessRows(stdout);
1704+if (!rows) {
1705+return null;
1706+}
17011707const descendants = collectPosixProcessTree(rows, safePid).filter(
17021708(row) => row.processId !== safePid,
17031709);
@@ -1729,34 +1735,36 @@ async function samplePosixProcessTree(pid, run, commandLineNeedles) {
17291735}
1730173617311737function parsePosixProcessRows(stdout) {
1732-return stdout
1733-.split(/\r?\n/u)
1734-.map((line) => {
1735-const match = line.match(/^\s*(\d+)\s+(\d+)\s+(\d+)\s+([0-9.]+)\s+(.*)$/u);
1736-if (!match) {
1737-return null;
1738-}
1739-const [, pidRaw, ppidRaw, rssKbRaw, cpuRaw, command] = match;
1740-const processId = Number.parseInt(pidRaw, 10);
1741-const parentProcessId = Number.parseInt(ppidRaw, 10);
1742-const rssKb = Number.parseInt(rssKbRaw, 10);
1743-const cpuPercent = parseStrictNonNegativeDecimal(cpuRaw);
1744-if (
1745-!Number.isInteger(processId) ||
1746-!Number.isInteger(parentProcessId) ||
1747-!Number.isFinite(rssKb)
1748-) {
1749-return null;
1750-}
1751-return {
1752- processId,
1753- parentProcessId,
1754- rssKb,
1755- cpuPercent,
1756-command: command ?? "",
1757-};
1758-})
1759-.filter(Boolean);
1738+const rows = [];
1739+for (const line of stdout.split(/\r?\n/u)) {
1740+const match = line.match(/^\s*(\S+)\s+(\S+)\s+(\S+)\s+(\S+)\s+(.*)$/u);
1741+if (!match) {
1742+continue;
1743+}
1744+const [, pidRaw, ppidRaw, rssKbRaw, cpuRaw, command] = match;
1745+if (!/^\d/u.test(pidRaw) && !/^\d/u.test(ppidRaw)) {
1746+continue;
1747+}
1748+const processId = parseStrictPositiveInteger(pidRaw);
1749+const parentProcessId = parseStrictUnsignedInteger(ppidRaw);
1750+const rssKb = parseStrictPositiveInteger(rssKbRaw);
1751+const cpuPercent = parseStrictNonNegativeDecimal(cpuRaw);
1752+if (
1753+!Number.isInteger(processId) ||
1754+!Number.isInteger(parentProcessId) ||
1755+!Number.isInteger(rssKb)
1756+) {
1757+return null;
1758+}
1759+rows.push({
1760+ processId,
1761+ parentProcessId,
1762+ rssKb,
1763+ cpuPercent,
1764+command: command ?? "",
1765+});
1766+}
1767+return rows;
17601768}
1761176917621770function parseStrictNonNegativeDecimal(raw) {
@@ -1777,6 +1785,11 @@ function parseStrictUnsignedInteger(raw) {
17771785return Number.isSafeInteger(parsed) ? parsed : null;
17781786}
177917871788+function parseStrictPositiveInteger(raw) {
1789+const parsed = parseStrictUnsignedInteger(raw);
1790+return parsed && parsed > 0 ? parsed : null;
1791+}
1792+17801793function parseTasklistMemoryKiB(raw) {
17811794const text = String(raw ?? "").trim();
17821795const match = text.match(/^((?:0|[1-9]\d*)|(?:[1-9]\d{0,2}(?:,\d{3})+))\s*K$/iu);
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。