From d9c791a508fb70004f9e7d3cc1564e4c17345dd7 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 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 100118e7b4dbb5..f348ef6d2d612f 100644 --- a/lib/internal/vm.js +++ b/lib/internal/vm.js @@ -16,9 +16,9 @@ const { validateObject, validateString, validateStringArray, - validateUint32, kValidateObjectAllowArray, kValidateObjectAllowNullable, + validateInt32, } = require('internal/validators'); const { ERR_INVALID_ARG_TYPE, @@ -48,8 +48,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/, + } +);