|
10 | 10 | #ifndef _LIBCPP_STACKTRACE_BASIC
|
11 | 11 | #define _LIBCPP_STACKTRACE_BASIC
|
12 | 12 |
|
| 13 | +#include "__assert" |
13 | 14 | #include <__config>
|
14 | 15 |
|
15 | 16 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
|
@@ -81,20 +82,27 @@ class _LIBCPP_EXPORTED_FROM_ABI basic_stacktrace : private __stacktrace::base {
|
81 | 82 | // (19.6.4.2)
|
82 | 83 | // Creation and assignment [stacktrace.basic.cons]
|
83 | 84 |
|
| 85 | + // Should be generous, but not so large that it would easily lead to an overflow |
| 86 | + // when added to a given skip amount. |
| 87 | + constexpr static size_type __default_max_depth = 1024; |
| 88 | + |
84 | 89 | _LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE _LIBCPP_EXPORTED_FROM_ABI static basic_stacktrace
|
85 | 90 | current(const allocator_type& __caller_alloc = allocator_type()) noexcept(__kNoThrowAlloc) {
|
86 |
| - return current(1, /* no __max_depth */ ~0, __caller_alloc); |
| 91 | + return current(1, __default_max_depth, __caller_alloc); |
87 | 92 | }
|
88 | 93 |
|
89 | 94 | _LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE _LIBCPP_EXPORTED_FROM_ABI static basic_stacktrace
|
90 | 95 | current(size_type __skip, const allocator_type& __caller_alloc = allocator_type()) noexcept(__kNoThrowAlloc) {
|
91 |
| - return current(__skip + 1, /* no __max_depth */ ~0, __caller_alloc); |
| 96 | + return current(__skip + 1, __default_max_depth, __caller_alloc); |
92 | 97 | }
|
93 | 98 |
|
94 | 99 | _LIBCPP_NO_TAIL_CALLS _LIBCPP_NOINLINE _LIBCPP_EXPORTED_FROM_ABI static basic_stacktrace
|
95 | 100 | current(size_type __skip,
|
96 | 101 | size_type __max_depth,
|
97 | 102 | const allocator_type& __caller_alloc = allocator_type()) noexcept(__kNoThrowAlloc) {
|
| 103 | + _LIBCPP_ASSERT_VALID_ELEMENT_ACCESS( |
| 104 | + __skip <= __skip + __max_depth, "sum of skip and max_depth too large; overflows size_type"); |
| 105 | + |
98 | 106 | __stacktrace::base __builder(__caller_alloc);
|
99 | 107 | __builder.build_stacktrace(__skip + 1, __max_depth);
|
100 | 108 | basic_stacktrace<_Allocator> __ret{__caller_alloc};
|
|
0 commit comments