Skip to content

Commit

Permalink
lib: fix compileFunction throws range error for negative numbers
Browse files Browse the repository at this point in the history
PR-URL: nodejs#49855
Fixes: nodejs#49848
Reviewed-By: Yagiz Nizipli <yagiz@nizipli.com>
Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
  • Loading branch information
MrJithil authored and joyeecheung committed Dec 1, 2023
1 parent 86d8b58 commit c8ed93a
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
6 changes: 3 additions & 3 deletions lib/internal/vm.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const {
validateObject,
validateString,
validateStringArray,
validateUint32,
validateInt32,
} = require('internal/validators');
const {
ERR_INVALID_ARG_TYPE,
Expand Down Expand Up @@ -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');
Expand Down
34 changes: 34 additions & 0 deletions test/es-module/test-vm-compile-function-lineoffset.js
Original file line number Diff line number Diff line change
@@ -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/,
}
);

0 comments on commit c8ed93a

Please sign in to comment.