Skip to content

Commit f19bd9e

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 5ce0066 commit f19bd9e

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/node_file.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -913,14 +913,20 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
913913
name_v[name_idx++] = filename.ToLocalChecked();
914914

915915
if (name_idx >= arraysize(name_v)) {
916-
fn->Call(env->context(), names, name_idx, name_v)
917-
.ToLocalChecked();
916+
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx,
917+
name_v);
918+
if (ret.IsEmpty()) {
919+
return;
920+
}
918921
name_idx = 0;
919922
}
920923
}
921924

922925
if (name_idx > 0) {
923-
fn->Call(env->context(), names, name_idx, name_v).ToLocalChecked();
926+
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, name_v);
927+
if (ret.IsEmpty()) {
928+
return;
929+
}
924930
}
925931

926932
args.GetReturnValue().Set(names);
Lines changed: 18 additions & 0 deletions
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)