Skip to content

Commit c5c9515

Browse files
joyeecheungBridgeAR
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 df2f4ad commit c5c9515

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
@@ -1132,14 +1132,20 @@ static void ReadDir(const FunctionCallbackInfo<Value>& args) {
11321132
name_v[name_idx++] = filename.ToLocalChecked();
11331133

11341134
if (name_idx >= arraysize(name_v)) {
1135-
fn->Call(env->context(), names, name_idx, name_v)
1136-
.ToLocalChecked();
1135+
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx,
1136+
name_v);
1137+
if (ret.IsEmpty()) {
1138+
return;
1139+
}
11371140
name_idx = 0;
11381141
}
11391142
}
11401143

11411144
if (name_idx > 0) {
1142-
fn->Call(env->context(), names, name_idx, name_v).ToLocalChecked();
1145+
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, name_v);
1146+
if (ret.IsEmpty()) {
1147+
return;
1148+
}
11431149
}
11441150

11451151
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)