Skip to content

Commit fc28b70

Browse files
zelalem61aduh95
authored andcommitted
os: fix abort on negative zero pid or priority
Fixes: #67648 Assisted-by: Claude Code Signed-off-by: Zelalem Habtamu <zelalemhab19@gmail.com> PR-URL: #67651 Reviewed-By: Ethan Arrowood <ethan@arrowood.dev> Reviewed-By: Luigi Pinca <luigipinca@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Stefan Stojanovic <stefan.stojanovic@janeasystems.com>
1 parent 01470de commit fc28b70

2 files changed

Lines changed: 29 additions & 2 deletions

File tree

‎lib/os.js‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,9 @@ function setPriority(pid, priority) {
260260

261261
const ctx = {};
262262

263-
if (_setPriority(pid, priority, ctx) !== 0)
263+
// V8 does not report -0 as an int32, so coerce it to 0 before it reaches
264+
// the binding.
265+
if (_setPriority(pid | 0, priority | 0, ctx) !== 0)
264266
throw new ERR_SYSTEM_ERROR(ctx);
265267
}
266268

@@ -275,7 +277,9 @@ function getPriority(pid) {
275277
validateInt32(pid, 'pid');
276278

277279
const ctx = {};
278-
const priority = _getPriority(pid, ctx);
280+
// V8 does not report -0 as an int32, so coerce it to 0 before it reaches
281+
// the binding.
282+
const priority = _getPriority(pid | 0, ctx);
279283

280284
if (priority === undefined)
281285
throw new ERR_SYSTEM_ERROR(ctx);
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
'use strict';
2+
3+
require('../common');
4+
5+
const assert = require('assert');
6+
const os = require('os');
7+
8+
// -0 passes the int32 validation, so it must be accepted like 0 instead of
9+
// aborting the process in the binding.
10+
assert.strictEqual(os.getPriority(-0), os.getPriority(0));
11+
12+
os.setPriority(-0, os.getPriority());
13+
14+
// Setting the priority to 0 may need more privileges than the process has,
15+
// which is reported as a system error rather than an abort.
16+
for (const args of [[0, -0], [-0, -0], [-0]]) {
17+
try {
18+
os.setPriority(...args);
19+
} catch (err) {
20+
assert.strictEqual(err.code, 'ERR_SYSTEM_ERROR');
21+
assert.strictEqual(err.info.code, 'EACCES');
22+
}
23+
}

0 commit comments

Comments
 (0)