From 18f27ad97997b33d745be8e231de1ea26167c0fc Mon Sep 17 00:00:00 2001 From: Zelalem Habtamu Date: Fri, 20 Nov 2026 14:40:15 +0300 Subject: [PATCH] os: fix abort on negative zero pid or priority Fixes: https://github.com/nodejs/node/issues/67648 Assisted-by: Claude Code Signed-off-by: Zelalem Habtamu --- lib/os.js | 8 ++++++-- test/parallel/test-os-negative-zero.js | 23 +++++++++++++++++++++++ 2 files changed, 29 insertions(+), 2 deletions(-) create mode 100644 test/parallel/test-os-negative-zero.js diff --git a/lib/os.js b/lib/os.js index 34b228c4edb1..612e7171f7af 100644 --- a/lib/os.js +++ b/lib/os.js @@ -260,7 +260,9 @@ function setPriority(pid, priority) { const ctx = {}; - if (_setPriority(pid, priority, ctx) !== 0) + // V8 does not report -0 as an int32, so coerce it to 0 before it reaches + // the binding. + if (_setPriority(pid | 0, priority | 0, ctx) !== 0) throw new ERR_SYSTEM_ERROR(ctx); } @@ -275,7 +277,9 @@ function getPriority(pid) { validateInt32(pid, 'pid'); const ctx = {}; - const priority = _getPriority(pid, ctx); + // V8 does not report -0 as an int32, so coerce it to 0 before it reaches + // the binding. + const priority = _getPriority(pid | 0, ctx); if (priority === undefined) throw new ERR_SYSTEM_ERROR(ctx); diff --git a/test/parallel/test-os-negative-zero.js b/test/parallel/test-os-negative-zero.js new file mode 100644 index 000000000000..7d355bb4c864 --- /dev/null +++ b/test/parallel/test-os-negative-zero.js @@ -0,0 +1,23 @@ +'use strict'; + +require('../common'); + +const assert = require('assert'); +const os = require('os'); + +// -0 passes the int32 validation, so it must be accepted like 0 instead of +// aborting the process in the binding. +assert.strictEqual(os.getPriority(-0), os.getPriority(0)); + +os.setPriority(-0, os.getPriority()); + +// Setting the priority to 0 may need more privileges than the process has, +// which is reported as a system error rather than an abort. +for (const args of [[0, -0], [-0, -0], [-0]]) { + try { + os.setPriority(...args); + } catch (err) { + assert.strictEqual(err.code, 'ERR_SYSTEM_ERROR'); + assert.strictEqual(err.info.code, 'EACCES'); + } +}