From 95d69fcf7496499ad9316ee57b44fc7f2110f92f Mon Sep 17 00:00:00 2001 From: MacDue Date: Sun, 28 May 2023 12:40:49 +0100 Subject: [PATCH] LibJS: Fix crash when trying to get source range Previously, source_range() could crash attempting to read from a null unrealized->source_code pointer. It looks like the previous behaviour here was to return a dummy source range, so this commit restores that. With this loading https://github.com/SerenityOS/serenity works again. --- Userland/Libraries/LibJS/Runtime/Error.cpp | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibJS/Runtime/Error.cpp b/Userland/Libraries/LibJS/Runtime/Error.cpp index bee4dce057..7ce2823f7e 100644 --- a/Userland/Libraries/LibJS/Runtime/Error.cpp +++ b/Userland/Libraries/LibJS/Runtime/Error.cpp @@ -17,9 +17,12 @@ namespace JS { SourceRange const& TracebackFrame::source_range() const { - if (auto* unrealized = source_range_storage.get_pointer()) { + if (auto* unrealized = source_range_storage.get_pointer(); unrealized && unrealized->source_code) { auto source_range = unrealized->source_code->range_from_offsets(unrealized->start_offset, unrealized->end_offset); source_range_storage = move(source_range); + } else { + static auto dummy_source_range = SourceRange { .code = SourceCode::create(String {}, String {}), .start = {}, .end = {} }; + return dummy_source_range; } return source_range_storage.get(); } @@ -69,8 +72,6 @@ ThrowCompletionOr Error::install_error_cause(Value options) void Error::populate_stack() { - static auto dummy_source_range = SourceRange { .code = SourceCode::create(String {}, String {}), .start = {}, .end = {} }; - auto& vm = this->vm(); m_traceback.ensure_capacity(vm.execution_context_stack().size()); for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; i--) {