Browse Source

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.
MacDue 2 years ago
parent
commit
95d69fcf74
1 changed files with 4 additions and 3 deletions
  1. 4 3
      Userland/Libraries/LibJS/Runtime/Error.cpp

+ 4 - 3
Userland/Libraries/LibJS/Runtime/Error.cpp

@@ -17,9 +17,12 @@ namespace JS {
 
 
 SourceRange const& TracebackFrame::source_range() const
 SourceRange const& TracebackFrame::source_range() const
 {
 {
-    if (auto* unrealized = source_range_storage.get_pointer<UnrealizedSourceRange>()) {
+    if (auto* unrealized = source_range_storage.get_pointer<UnrealizedSourceRange>(); unrealized && unrealized->source_code) {
         auto source_range = unrealized->source_code->range_from_offsets(unrealized->start_offset, unrealized->end_offset);
         auto source_range = unrealized->source_code->range_from_offsets(unrealized->start_offset, unrealized->end_offset);
         source_range_storage = move(source_range);
         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<SourceRange>();
     return source_range_storage.get<SourceRange>();
 }
 }
@@ -69,8 +72,6 @@ ThrowCompletionOr<void> Error::install_error_cause(Value options)
 
 
 void Error::populate_stack()
 void Error::populate_stack()
 {
 {
-    static auto dummy_source_range = SourceRange { .code = SourceCode::create(String {}, String {}), .start = {}, .end = {} };
-
     auto& vm = this->vm();
     auto& vm = this->vm();
     m_traceback.ensure_capacity(vm.execution_context_stack().size());
     m_traceback.ensure_capacity(vm.execution_context_stack().size());
     for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; i--) {
     for (ssize_t i = vm.execution_context_stack().size() - 1; i >= 0; i--) {