Skip to content

Commit 2995506

Browse files
joyeecheunggibfahn
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 <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 973488b commit 2995506

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

953953
if (name_idx >= arraysize(name_v)) {
954-
fn->Call(env->context(), names, name_idx, name_v)
955-
.ToLocalChecked();
954+
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx,
955+
name_v);
956+
if (ret.IsEmpty()) {
957+
return;
958+
}
956959
name_idx = 0;
957960
}
958961
}
959962

960963
if (name_idx > 0) {
961-
fn->Call(env->context(), names, name_idx, name_v).ToLocalChecked();
964+
MaybeLocal<Value> ret = fn->Call(env->context(), names, name_idx, name_v);
965+
if (ret.IsEmpty()) {
966+
return;
967+
}
962968
}
963969

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