From 5b5e5192f724c35a805e52d9f1255860c60d0a24 Mon Sep 17 00:00:00 2001 From: Jithil P Ponnan Date: Sat, 7 Oct 2023 01:33:46 +1100 Subject: [PATCH] lib: fix compileFunction throws range error for negative numbers PR-URL: https://github.com/nodejs/node/pull/49855 Backport-PR-URL: https://github.com/nodejs/node/pull/51004 Fixes: https://github.com/nodejs/node/issues/49848 Reviewed-By: Yagiz Nizipli Reviewed-By: Benjamin Gruenbaum --- lib/internal/vm.js | 6 ++-- .../test-vm-compile-function-lineoffset.js | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+), 3 deletions(-) create mode 100644 test/es-module/test-vm-compile-function-lineoffset.js diff --git a/lib/internal/vm.js b/lib/internal/vm.js index b14ba13e7e4cfb..d2063c78e6315d 100644 --- a/lib/internal/vm.js +++ b/lib/internal/vm.js @@ -16,7 +16,7 @@ const { validateObject, validateString, validateStringArray, - validateUint32, + validateInt32, } = require('internal/validators'); const { ERR_INVALID_ARG_TYPE, @@ -46,8 +46,8 @@ function internalCompileFunction(code, params, options) { } = options; validateString(filename, 'options.filename'); - validateUint32(columnOffset, 'options.columnOffset'); - validateUint32(lineOffset, 'options.lineOffset'); + validateInt32(columnOffset, 'options.columnOffset'); + validateInt32(lineOffset, 'options.lineOffset'); if (cachedData !== undefined) validateBuffer(cachedData, 'options.cachedData'); validateBoolean(produceCachedData, 'options.produceCachedData'); diff --git a/test/es-module/test-vm-compile-function-lineoffset.js b/test/es-module/test-vm-compile-function-lineoffset.js new file mode 100644 index 00000000000000..47846a3aa6eb8b --- /dev/null +++ b/test/es-module/test-vm-compile-function-lineoffset.js @@ -0,0 +1,34 @@ +'use strict'; + +require('../common'); + +const assert = require('assert'); +const { compileFunction } = require('node:vm'); + +const min = -2147483648; +const max = 2147483647; + +compileFunction('', [], { lineOffset: min, columnOffset: min }); +compileFunction('', [], { lineOffset: max, columnOffset: max }); + +assert.throws( + () => { + compileFunction('', [], { lineOffset: min - 1, columnOffset: max }); + }, + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: /The value of "options\.lineOffset" is out of range/, + } +); + +assert.throws( + () => { + compileFunction('', [], { lineOffset: min, columnOffset: min - 1 }); + }, + { + code: 'ERR_OUT_OF_RANGE', + name: 'RangeError', + message: /The value of "options\.columnOffset" is out of range/, + } +);