66
77#include < algorithm>
88#include < cassert>
9+ #include < cstdint>
910#include < execinfo.h>
1011
1112#include " common/sanitizer.h"
@@ -43,9 +44,13 @@ int fast_backtrace(void** buffer, int size) {
4344}
4445
4546int fast_backtrace_by_bp (void * bp, void * stack_end_, void ** buffer, int size) {
46- stack_frame* current_bp = static_cast <stack_frame*>(bp);
47+ if (stack_end_ == nullptr ) {
48+ return 0 ;
49+ }
50+ const auto last_frame_addr = reinterpret_cast <std::uintptr_t >(stack_end_) - sizeof (stack_frame);
51+ auto * current_bp = static_cast <stack_frame*>(bp);
4752 int i = 0 ;
48- while (i < size && reinterpret_cast <char * >(current_bp) <= stack_end_ && !(reinterpret_cast <long >(current_bp) & (sizeof (long ) - 1 ))) {
53+ while (i < size && reinterpret_cast <std:: uintptr_t >(current_bp) <= last_frame_addr && !(reinterpret_cast <long >(current_bp) & (sizeof (long ) - 1 ))) {
4954 void * ip = current_bp->ip ;
5055 buffer[i++] = ip;
5156 stack_frame* p = current_bp->bp ;
@@ -67,9 +72,13 @@ int fast_backtrace_without_recursions(void** buffer, int size) noexcept {
6772}
6873
6974int fast_backtrace_without_recursions_by_bp (void * bp, void * stack_end_, void ** buffer, int size) noexcept {
70- stack_frame* current_bp = static_cast <stack_frame*>(bp);
75+ if (stack_end_ == nullptr ) {
76+ return 0 ;
77+ }
78+ const auto last_frame_addr = reinterpret_cast <std::uintptr_t >(stack_end_) - sizeof (stack_frame);
79+ auto * current_bp = static_cast <stack_frame*>(bp);
7180 int i = 0 ;
72- while (i < size && reinterpret_cast <char * >(current_bp) <= stack_end_ && !(reinterpret_cast <long >(current_bp) & (sizeof (long ) - 1 ))) {
81+ while (i < size && reinterpret_cast <std:: uintptr_t >(current_bp) <= last_frame_addr && !(reinterpret_cast <long >(current_bp) & (sizeof (long ) - 1 ))) {
7382 buffer[i++] = current_bp->ip ;
7483 for (int possible_recursion_len = 1 ; i >= 2 * possible_recursion_len; ++possible_recursion_len) {
7584 if (std::equal (buffer + i - possible_recursion_len, buffer + i, buffer + i - possible_recursion_len * 2 )) {
0 commit comments