Skip to content

Commit

Permalink
lib: use primordials when calling methods of Error
Browse files Browse the repository at this point in the history
This is to unsure that code using those methods won't crash if the
methods are deleted in userland.

PR-URL: #35837
Reviewed-By: Michaël Zasso <targos@protonmail.com>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
  • Loading branch information
aduh95 authored and BethGriggs committed Dec 15, 2020
1 parent 3f3d2d7 commit 41d997c
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 37 deletions.
6 changes: 3 additions & 3 deletions lib/assert.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@

const {
Error,
ErrorCaptureStackTrace,
ObjectAssign,
ObjectIs,
ObjectKeys,
Expand Down Expand Up @@ -269,8 +270,7 @@ function getErrMessage(message, fn) {
// We only need the stack trace. To minimize the overhead use an object
// instead of an error.
const err = {};
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(err, fn);
ErrorCaptureStackTrace(err, fn);
Error.stackTraceLimit = tmpLimit;

overrideStackTrace.set(err, (_, stack) => stack);
Expand Down Expand Up @@ -789,7 +789,7 @@ function hasMatchingError(actual, expected) {
if (expected.prototype !== undefined && actual instanceof expected) {
return true;
}
if (Error.isPrototypeOf(expected)) {
if (ObjectPrototypeIsPrototypeOf(Error, expected)) {
return false;
}
return expected.call({}, actual) === true;
Expand Down
4 changes: 2 additions & 2 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
const {
Boolean,
Error,
ErrorCaptureStackTrace,
MathMin,
NumberIsNaN,
ObjectCreate,
Expand Down Expand Up @@ -279,8 +280,7 @@ EventEmitter.prototype.emit = function emit(type, ...args) {
if (er instanceof Error) {
try {
const capture = {};
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(capture, EventEmitter.prototype.emit);
ErrorCaptureStackTrace(capture, EventEmitter.prototype.emit);
ObjectDefineProperty(er, kEnhanceStackBeforeInspector, {
value: enhanceStackTrace.bind(this, er, capture),
configurable: true
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/assert/assertion_error.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

const {
Error,
ErrorCaptureStackTrace,
MathMax,
ObjectCreate,
ObjectDefineProperty,
Expand Down Expand Up @@ -444,8 +445,7 @@ class AssertionError extends Error {
this.expected = expected;
this.operator = operator;
}
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(this, stackStartFn || stackStartFunction);
ErrorCaptureStackTrace(this, stackStartFn || stackStartFunction);
// Create error message including the error code in the name.
this.stack;
// Reset the name.
Expand Down
5 changes: 2 additions & 3 deletions lib/internal/async_hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

const {
ArrayPrototypeUnshift,
Error,
ErrorCaptureStackTrace,
FunctionPrototypeBind,
ObjectPrototypeHasOwnProperty,
ObjectDefineProperty,
Expand Down Expand Up @@ -159,8 +159,7 @@ function fatalError(e) {
process._rawDebug(e.stack);
} else {
const o = { message: e };
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(o, fatalError);
ErrorCaptureStackTrace(o, fatalError);
process._rawDebug(o.stack);
}

Expand Down
5 changes: 2 additions & 3 deletions lib/internal/console/constructor.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const {
ArrayFrom,
ArrayIsArray,
Boolean,
Error,
ErrorCaptureStackTrace,
Map,
MathFloor,
Number,
Expand Down Expand Up @@ -395,8 +395,7 @@ const consoleMethods = {
name: 'Trace',
message: this[kFormatForStderr](args)
};
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(err, trace);
ErrorCaptureStackTrace(err, trace);
this.error(err.stack);
},

Expand Down
24 changes: 7 additions & 17 deletions lib/internal/errors.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
const {
ArrayIsArray,
Error,
ErrorCaptureStackTrace,
ErrorPrototypeToString,
JSONStringify,
Map,
Expand Down Expand Up @@ -302,8 +303,7 @@ function hideStackFrames(fn) {
function addCodeToName(err, name, code) {
// Set the stack
if (excludedStackFn !== undefined) {
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(err, excludedStackFn);
ErrorCaptureStackTrace(err, excludedStackFn);
}
// Add the error code to the name to include it in the stack trace.
err.name = `${name} [${code}]`;
Expand Down Expand Up @@ -439,9 +439,7 @@ function uvException(ctx) {
if (dest) {
err.dest = dest;
}

// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(err, excludedStackFn || uvException);
ErrorCaptureStackTrace(err, excludedStackFn || uvException);
return err;
}

Expand Down Expand Up @@ -482,9 +480,7 @@ function uvExceptionWithHostPort(err, syscall, address, port) {
if (port) {
ex.port = port;
}

// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(ex, excludedStackFn || uvExceptionWithHostPort);
ErrorCaptureStackTrace(ex, excludedStackFn || uvExceptionWithHostPort);
return ex;
}

Expand All @@ -511,9 +507,7 @@ function errnoException(err, syscall, original) {
ex.errno = err;
ex.code = code;
ex.syscall = syscall;

// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(ex, excludedStackFn || errnoException);
ErrorCaptureStackTrace(ex, excludedStackFn || errnoException);
return ex;
}

Expand Down Expand Up @@ -560,9 +554,7 @@ function exceptionWithHostPort(err, syscall, address, port, additional) {
if (port) {
ex.port = port;
}

// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(ex, excludedStackFn || exceptionWithHostPort);
ErrorCaptureStackTrace(ex, excludedStackFn || exceptionWithHostPort);
return ex;
}

Expand Down Expand Up @@ -606,9 +598,7 @@ function dnsException(code, syscall, hostname) {
if (hostname) {
ex.hostname = hostname;
}

// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(ex, excludedStackFn || dnsException);
ErrorCaptureStackTrace(ex, excludedStackFn || dnsException);
return ex;
}

Expand Down
8 changes: 3 additions & 5 deletions lib/internal/fs/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const {
ArrayIsArray,
BigInt,
DateNow,
Error,
ErrorCaptureStackTrace,
ObjectPrototypeHasOwnProperty,
Number,
NumberIsFinite,
Expand Down Expand Up @@ -303,16 +303,14 @@ function getOptions(options, defaultOptions) {
function handleErrorFromBinding(ctx) {
if (ctx.errno !== undefined) { // libuv error numbers
const err = uvException(ctx);
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(err, handleErrorFromBinding);
ErrorCaptureStackTrace(err, handleErrorFromBinding);
throw err;
}
if (ctx.error !== undefined) { // Errors created in C++ land.
// TODO(joyeecheung): currently, ctx.error are encoding errors
// usually caused by memory problems. We need to figure out proper error
// code(s) for this.
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(ctx.error, handleErrorFromBinding);
ErrorCaptureStackTrace(ctx.error, handleErrorFromBinding);
throw ctx.error;
}
}
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/process/warning.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
const {
ArrayIsArray,
Error,
ErrorCaptureStackTrace,
String,
} = primordials;

Expand Down Expand Up @@ -157,8 +158,7 @@ function createWarningObject(warning, type, code, ctor, detail) {
warning.name = String(type || 'Warning');
if (code !== undefined) warning.code = code;
if (detail !== undefined) warning.detail = detail;
// eslint-disable-next-line no-restricted-syntax
Error.captureStackTrace(warning, ctor || process.emitWarning);
ErrorCaptureStackTrace(warning, ctor || process.emitWarning);
return warning;
}

Expand Down

0 comments on commit 41d997c

Please sign in to comment.