Skip to content

assert: refactor to avoid unsafe array iteration #36754

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,12 @@

const {
ArrayPrototypeIndexOf,
ArrayPrototypeForEach,
ArrayPrototypeJoin,
ArrayPrototypePush,
ArrayPrototypeShift,
ArrayPrototypeSlice,
ArrayPrototypeSome,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
Expand Down Expand Up @@ -374,7 +376,7 @@ function getErrMessage(message, fn) {
}
}

function innerOk(fn, argLen, value, message) {
function innerOk(fn, { length: argLen, 0: value, 1: message }) {
if (!value) {
let generatedMessage = false;

Expand Down Expand Up @@ -403,7 +405,7 @@ function innerOk(fn, argLen, value, message) {
// Pure assertion tests whether a value is truthy, as determined
// by !!value.
function ok(...args) {
innerOk(ok, args.length, ...args);
innerOk(ok, args);
}
assert.ok = ok;

Expand Down Expand Up @@ -543,7 +545,7 @@ assert.notStrictEqual = function notStrictEqual(actual, expected, message) {

class Comparison {
constructor(obj, keys, actual) {
for (const key of keys) {
ArrayPrototypeForEach(keys, (key) => {
if (key in obj) {
if (actual !== undefined &&
typeof actual[key] === 'string' &&
Expand All @@ -554,7 +556,7 @@ class Comparison {
this[key] = obj[key];
}
}
}
});
}
}

Expand Down Expand Up @@ -626,14 +628,13 @@ function expectedException(actual, expected, message, fn) {
expected, 'may not be an empty object');
}
if (isDeepEqual === undefined) lazyLoadComparison();
for (const key of keys) {
if (typeof actual[key] === 'string' &&
isRegExp(expected[key]) &&
RegExpPrototypeTest(expected[key], actual[key])) {
continue;
ArrayPrototypeForEach(keys, (key) => {
if (typeof actual[key] !== 'string' ||
!isRegExp(expected[key]) ||
!RegExpPrototypeTest(expected[key], actual[key])) {
compareExceptionKey(actual, expected, key, message, keys, fn);
}
compareExceptionKey(actual, expected, key, message, keys, fn);
}
});
return;
}
// Guard instanceof against arrow functions as they don't have a prototype.
Expand Down Expand Up @@ -739,9 +740,9 @@ async function waitForActual(promiseFn) {
return NO_EXCEPTION_SENTINEL;
}

function expectsError(stackStartFn, actual, error, message) {
function expectsError(stackStartFn, actual, { 0: error, 1: message, length }) {
if (typeof error === 'string') {
if (arguments.length === 4) {
if (length === 2) {
throw new ERR_INVALID_ARG_TYPE('error',
['Object', 'Error', 'Function', 'RegExp'],
error);
Expand Down Expand Up @@ -811,7 +812,7 @@ function hasMatchingError(actual, expected) {
return ReflectApply(expected, {}, [actual]) === true;
}

function expectsNoError(stackStartFn, actual, error, message) {
function expectsNoError(stackStartFn, actual, { 0: error, 1: message }) {
if (actual === NO_EXCEPTION_SENTINEL)
return;

Expand All @@ -837,19 +838,19 @@ function expectsNoError(stackStartFn, actual, error, message) {
}

assert.throws = function throws(promiseFn, ...args) {
expectsError(throws, getActual(promiseFn), ...args);
expectsError(throws, getActual(promiseFn), args);
};

assert.rejects = async function rejects(promiseFn, ...args) {
expectsError(rejects, await waitForActual(promiseFn), ...args);
expectsError(rejects, await waitForActual(promiseFn), args);
};

assert.doesNotThrow = function doesNotThrow(fn, ...args) {
expectsNoError(doesNotThrow, getActual(fn), ...args);
expectsNoError(doesNotThrow, getActual(fn), args);
};

assert.doesNotReject = async function doesNotReject(fn, ...args) {
expectsNoError(doesNotReject, await waitForActual(fn), ...args);
expectsNoError(doesNotReject, await waitForActual(fn), args);
};

assert.ifError = function ifError(err) {
Expand Down Expand Up @@ -884,15 +885,15 @@ assert.ifError = function ifError(err) {
ArrayPrototypeShift(tmp2);
// Filter all frames existing in err.stack.
let tmp1 = StringPrototypeSplit(newErr.stack, '\n');
for (const errFrame of tmp2) {
ArrayPrototypeSome(tmp2, (errFrame) => {
// Find the first occurrence of the frame.
const pos = ArrayPrototypeIndexOf(tmp1, errFrame);
if (pos !== -1) {
// Only keep new frames.
tmp1 = ArrayPrototypeSlice(tmp1, 0, pos);
break;
return true;
}
}
});
newErr.stack =
`${ArrayPrototypeJoin(tmp1, '\n')}\n${ArrayPrototypeJoin(tmp2, '\n')}`;
}
Expand Down Expand Up @@ -948,7 +949,7 @@ assert.CallTracker = CallTracker;

// Expose a strict only variant of assert
function strict(...args) {
innerOk(strict, args.length, ...args);
innerOk(strict, args);
}
assert.strict = ObjectAssign(strict, assert, {
equal: assert.strictEqual,
Expand Down