Skip to content

Commit 3962c73

Browse files
apapirovskiMylesBorins
authored andcommitted
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: #20266 Fixes: #20258 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Trivikram Kamat <[email protected]>
1 parent 29bc735 commit 3962c73

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

lib/internal/util.js

+10-9
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,17 @@ function isInsideNodeModules() {
356356

357357
// Iterate over all stack frames and look for the first one not coming
358358
// from inside Node.js itself:
359-
for (const frame of stack) {
360-
const filename = frame.getFileName();
361-
// If a filename does not start with / or contain \,
362-
// it's likely from Node.js core.
363-
if (!/^\/|\\/.test(filename))
364-
continue;
365-
return kNodeModulesRE.test(filename);
359+
if (Array.isArray(stack)) {
360+
for (const frame of stack) {
361+
const filename = frame.getFileName();
362+
// If a filename does not start with / or contain \,
363+
// it's likely from Node.js core.
364+
if (!/^\/|\\/.test(filename))
365+
continue;
366+
return kNodeModulesRE.test(filename);
367+
}
366368
}
367-
368-
return false; // This should be unreachable.
369+
return false;
369370
}
370371

371372

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const bufferWarning = 'Buffer() is deprecated due to security and usability ' +
6+
'issues. Please use the Buffer.alloc(), ' +
7+
'Buffer.allocUnsafe(), or Buffer.from() methods instead.';
8+
9+
common.expectWarning('DeprecationWarning', bufferWarning, 'DEP0005');
10+
11+
// This is used to make sure that a warning is only emitted once even though
12+
// `new Buffer()` is called twice.
13+
process.on('warning', common.mustCall());
14+
15+
Error.prepareStackTrace = (err, trace) => new Buffer(10);
16+
17+
new Error().stack;

0 commit comments

Comments
 (0)