util: fix isInsideNodeModules inside error

When isInsideNodeModules gets called while already processing
another stack trace, V8 will not call prepareStackTrace again.
This used to cause Node.js to just crash — fix it by checking
for expected return type of the stack (Array).

PR-URL: https://github.com/nodejs/node/pull/20266
Fixes: https://github.com/nodejs/node/issues/20258
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
This commit is contained in:
Anatoli Papirovski 2018-04-25 00:56:35 +02:00
parent 99d56a4749
commit 16aee380a5
No known key found for this signature in database
GPG Key ID: 614E2E1ABEB4B2C0
2 changed files with 27 additions and 9 deletions

View File

@ -356,16 +356,17 @@ function isInsideNodeModules() {
// Iterate over all stack frames and look for the first one not coming
// from inside Node.js itself:
for (const frame of stack) {
const filename = frame.getFileName();
// If a filename does not start with / or contain \,
// it's likely from Node.js core.
if (!/^\/|\\/.test(filename))
continue;
return kNodeModulesRE.test(filename);
if (Array.isArray(stack)) {
for (const frame of stack) {
const filename = frame.getFileName();
// If a filename does not start with / or contain \,
// it's likely from Node.js core.
if (!/^\/|\\/.test(filename))
continue;
return kNodeModulesRE.test(filename);
}
}
return false; // This should be unreachable.
return false;
}

View File

@ -0,0 +1,17 @@
'use strict';
const common = require('../common');
const bufferWarning = 'Buffer() is deprecated due to security and usability ' +
'issues. Please use the Buffer.alloc(), ' +
'Buffer.allocUnsafe(), or Buffer.from() methods instead.';
common.expectWarning('DeprecationWarning', bufferWarning, 'DEP0005');
// This is used to make sure that a warning is only emitted once even though
// `new Buffer()` is called twice.
process.on('warning', common.mustCall());
Error.prepareStackTrace = (err, trace) => new Buffer(10);
new Error().stack;