assert: cap input size in myersDiff to avoid Int32Array overflow

PR-URL: https://github.com/nodejs/node/pull/59578
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
This commit is contained in:
Haram Jeong 2025-09-09 09:36:14 +09:00 committed by GitHub
parent 670d7ab7f2
commit 402a02fe2b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 40 additions and 1 deletions

View File

@ -6,6 +6,12 @@ const {
StringPrototypeEndsWith,
} = primordials;
const {
codes: {
ERR_OUT_OF_RANGE,
},
} = require('internal/errors');
const colors = require('internal/util/colors');
const kNopLinesToCollapse = 5;
@ -29,7 +35,15 @@ function myersDiff(actual, expected, checkCommaDisparity = false) {
const actualLength = actual.length;
const expectedLength = expected.length;
const max = actualLength + expectedLength;
// TODO(BridgeAR): Cap the input in case the values go beyond the limit of 2^31 - 1.
if (max > 2 ** 31 - 1) {
throw new ERR_OUT_OF_RANGE(
'myersDiff input size',
'< 2^31',
max,
);
}
const v = new Int32Array(2 * max + 1);
const trace = [];

View File

@ -0,0 +1,25 @@
// Flags: --expose-internals
'use strict';
const common = require('../common');
const assert = require('assert');
const { myersDiff } = require('internal/assert/myers_diff');
{
const arr1 = { length: 2 ** 31 - 1 };
const arr2 = { length: 2 };
const max = arr1.length + arr2.length;
assert.throws(
() => {
myersDiff(arr1, arr2);
},
common.expectsError({
code: 'ERR_OUT_OF_RANGE',
name: 'RangeError',
message: 'The value of "myersDiff input size" ' +
'is out of range. It must be < 2^31. ' +
`Received ${max}`
})
);
}