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: #20266Fixes: #20258 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent 29bc735 commit 3962c73

2 files changed

Lines changed: 27 additions & 9 deletions

File tree

‎lib/internal/util.js‎

Lines changed: 10 additions & 9 deletions
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(constframeofstack){
360-
constfilename=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-
returnkNodeModulesRE.test(filename);
359+
if(Array.isArray(stack)){
360+
for(constframeofstack){
361+
constfilename=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+
returnkNodeModulesRE.test(filename);
367+
}
366368
}
367-
368-
returnfalse;// This should be unreachable.
369+
returnfalse;
369370
}
370371

371372

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'use strict';
2+
3+
constcommon=require('../common');
4+
5+
constbufferWarning='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)=>newBuffer(10);
16+
17+
newError().stack;

0 commit comments

Comments
 (0)