From 5c9d7d80265820a9e90b2e89542a4cc51a548226 Mon Sep 17 00:00:00 2001 From: Linus Groh Date: Sun, 8 Nov 2020 12:58:49 +0000 Subject: [PATCH] js: Limit number of consecutive error trace entries being printed > function f(){f()}f() Uncaught exception: [RuntimeError]: Call stack size limit exceeded -> f 1234 more calls -> (global execution context) > function a(x){if(x>0){a(x-1)}else{throw Error()}}function b(x){if(x>0){b(x-1)}else{a(5)}}function c(){b(2)}c() Uncaught exception: [Error] -> a 5 more calls -> b -> b -> b -> c -> (global execution context) --- Userland/js.cpp | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/Userland/js.cpp b/Userland/js.cpp index 316160e0d7..9170c7526e 100644 --- a/Userland/js.cpp +++ b/Userland/js.cpp @@ -363,8 +363,25 @@ static bool parse_and_run(JS::Interpreter& interpreter, const StringView& source print(vm->exception()->value()); auto trace = vm->exception()->trace(); if (trace.size() > 1) { - for (auto& function_name : trace) - printf(" -> %s\n", function_name.characters()); + unsigned repetitions = 0; + for (size_t i = 0; i < trace.size(); ++i) { + auto& function_name = trace[i]; + if (i + 1 < trace.size() && trace[i + 1] == function_name) { + repetitions++; + continue; + } + if (repetitions > 4) { + // If more than 5 (1 + >4) consecutive function calls with the same name, print + // the name only once and show the number of repetitions instead. This prevents + // printing ridiculously large call stacks of recursive functions. + outln(" -> {}", function_name); + outln(" {} more calls", repetitions); + } else { + for (size_t j = 0; j < repetitions + 1; ++j) + outln(" -> {}", function_name); + } + repetitions = 0; + } } vm->clear_exception(); return false;