From 29faa41394e3cec75c22343a0b85bf72e20ae9c8 Mon Sep 17 00:00:00 2001 From: Joyee Cheung Date: Fri, 9 Feb 2018 01:22:37 +0800 Subject: [PATCH] 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. --- src/node_file.cc | 12 +++++++++--- .../parallel/test-fs-readdir-stack-overflow.js | 18 ++++++++++++++++++ 2 files changed, 27 insertions(+), 3 deletions(-) create mode 100644 test/parallel/test-fs-readdir-stack-overflow.js diff --git a/src/node_file.cc b/src/node_file.cc index 62dd5fe80cf8b2..e3a1da473111c1 100644 --- a/src/node_file.cc +++ b/src/node_file.cc @@ -1132,14 +1132,20 @@ static void ReadDir(const FunctionCallbackInfo& args) { name_v[name_idx++] = filename.ToLocalChecked(); if (name_idx >= arraysize(name_v)) { - fn->Call(env->context(), names, name_idx, name_v) - .ToLocalChecked(); + MaybeLocal ret = fn->Call(env->context(), names, name_idx, + name_v); + if (ret.IsEmpty()) { + return; + } name_idx = 0; } } if (name_idx > 0) { - fn->Call(env->context(), names, name_idx, name_v).ToLocalChecked(); + MaybeLocal ret = fn->Call(env->context(), names, name_idx, name_v); + if (ret.IsEmpty()) { + return; + } } args.GetReturnValue().Set(names); diff --git a/test/parallel/test-fs-readdir-stack-overflow.js b/test/parallel/test-fs-readdir-stack-overflow.js new file mode 100644 index 00000000000000..b7dea52cc37ec5 --- /dev/null +++ b/test/parallel/test-fs-readdir-stack-overflow.js @@ -0,0 +1,18 @@ +'use strict'; + +const common = require('../common'); + +const fs = require('fs'); + +function recurse() { + fs.readdirSync('.'); + recurse(); +} + +common.expectsError( + () => recurse(), + { + type: RangeError, + message: 'Maximum call stack size exceeded' + } +);