Skip to content

Commit 34f4934

Browse files
joyeecheungMylesBorins
authored andcommitted
fs: fix stack overflow in fs.readdirSync
Previously, fs.readdirSync calls the function returned by env->push_values_to_array_function() in batch and check the returned Maybe right away in C++, which can lead to assertions if the call stack already reaches the maximum size. This patch fixes that by returning early the call fails so the stack overflow error will be properly thrown into JS land. PR-URL: #18647 Fixes: #18645 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent a7419d0 commit 34f4934

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/node_file.cc

+9-3
Original file line numberDiff line numberDiff line change
@@ -909,14 +909,20 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
909909
name_v[name_idx++] = filename.ToLocalChecked();
910910

911911
if (name_idx >= arraysize(name_v)) {
912-
fn->Call(env->context(), names, name_idx, name_v)
913-
.ToLocalChecked();
912+
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx,
913+
name_v);
914+
if (ret.IsEmpty()) {
915+
return;
916+
}
914917
name_idx = 0;
915918
}
916919
}
917920

918921
if (name_idx > 0) {
919-
fn->Call(env->context(), names, name_idx, name_v).ToLocalChecked();
922+
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, name_v);
923+
if (ret.IsEmpty()) {
924+
return;
925+
}
920926
}
921927

922928
args.GetReturnValue().Set(names);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const fs = require('fs');
6+
7+
function recurse() {
8+
fs.readdirSync('.');
9+
recurse();
10+
}
11+
12+
common.expectsError(
13+
() => recurse(),
14+
{
15+
type: RangeError,
16+
message: 'Maximum call stack size exceeded'
17+
}
18+
);

0 commit comments

Comments
 (0)