diff --git a/absl/debugging/internal/stacktrace_emscripten-inl.inc b/absl/debugging/internal/stacktrace_emscripten-inl.inc index 9f870060..3c125e2f 100644 --- a/absl/debugging/internal/stacktrace_emscripten-inl.inc +++ b/absl/debugging/internal/stacktrace_emscripten-inl.inc @@ -74,10 +74,19 @@ static int UnwindImpl(void **result, uintptr_t *frames, int *sizes, size = static_cast(emscripten_stack_unwind_buffer(pc, stack, kStackLength)); - int result_count = size - skip_count; - if (result_count < 0) result_count = 0; - if (result_count > max_depth) result_count = max_depth; - for (int i = 0; i < result_count; i++) result[i] = stack[i + skip_count]; + int num_frames = size - skip_count; + if (num_frames < 0) num_frames = 0; + if (num_frames > max_depth) num_frames = max_depth; + + int result_count = 0; + for (int i = 0; i < num_frames; i++) { + int stack_index = i + skip_count; + // Follow x86 and stop if the return address is null (end of stack). + if (stack[stack_index] == nullptr) { + break; + } + result[result_count++] = stack[stack_index]; + } if (IS_STACK_FRAMES) { // No implementation for finding out the stack frames yet. diff --git a/absl/debugging/internal/stacktrace_riscv-inl.inc b/absl/debugging/internal/stacktrace_riscv-inl.inc index 7ae7fefa..a4fb3834 100644 --- a/absl/debugging/internal/stacktrace_riscv-inl.inc +++ b/absl/debugging/internal/stacktrace_riscv-inl.inc @@ -142,6 +142,10 @@ static int UnwindImpl(void **result, uintptr_t *frames, int *sizes, void *return_address = nullptr; while (frame_pointer && n < max_depth) { return_address = frame_pointer[-1]; + // Follow x86 and stop if the return address is null (end of stack). + if (return_address == nullptr) { + break; + } // The absl::GetStackFrames routine is called when we are in some // informational context (the failure signal handler for example). Use the diff --git a/absl/debugging/stacktrace_test.cc b/absl/debugging/stacktrace_test.cc index 5b54fe0d..a1108b3b 100644 --- a/absl/debugging/stacktrace_test.cc +++ b/absl/debugging/stacktrace_test.cc @@ -378,4 +378,14 @@ TEST(StackTrace, NestedSignal) { } #endif +TEST(StackTrace, NoNullptrInPopulatedRange) { + constexpr int kMaxDepth = 1024; + void* results[kMaxDepth]; + int depth = absl::GetStackTrace(results, kMaxDepth, 0); + for (int i = 0; i < depth; ++i) { + EXPECT_NE(results[i], nullptr) << "Unexpected nullptr found at index " << i; + } +} + + } // namespace