Skip to content

Commit 2d0ff97

Browse files
committed
Add a smoke test to Span in debug builds to recover from non-empty nullptr Span.
1 parent d9cd011 commit 2d0ff97

File tree

2 files changed

+15
-6
lines changed

2 files changed

+15
-6
lines changed

core/templates/span.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,17 @@ class Span {
5151
std::is_same<T, wchar_t>>;
5252

5353
_FORCE_INLINE_ constexpr Span() = default;
54-
_FORCE_INLINE_ constexpr Span(const T *p_ptr, uint64_t p_len) :
55-
_ptr(p_ptr), _len(p_len) {}
54+
55+
_FORCE_INLINE_ Span(const T *p_ptr, uint64_t p_len) :
56+
_ptr(p_ptr), _len(p_len) {
57+
#ifdef DEBUG_ENABLED
58+
// TODO In c++20, make this check run only in non-consteval, and make this constructor constexpr.
59+
if (_ptr == nullptr && _len > 0) {
60+
ERR_PRINT("Internal bug, please report: Span was created from nullptr with size > 0. Recovering by using size = 0.");
61+
_len = 0;
62+
}
63+
#endif
64+
}
5665

5766
// Allows creating Span directly from C arrays and string literals.
5867
template <size_t N>

tests/core/templates/test_span.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,10 @@ TEST_CASE("[Span] Constexpr Validators") {
4343
static_assert(span_empty.is_empty());
4444

4545
constexpr static uint16_t value = 5;
46-
constexpr Span<uint16_t> span_value(&value, 1);
47-
static_assert(span_value.ptr() == &value);
48-
static_assert(span_value.size() == 1);
49-
static_assert(!span_value.is_empty());
46+
Span<uint16_t> span_value(&value, 1);
47+
CHECK(span_value.ptr() == &value);
48+
CHECK(span_value.size() == 1);
49+
CHECK(!span_value.is_empty());
5050

5151
static constexpr int ints[] = { 0, 1, 2, 3, 4, 5 };
5252
constexpr Span<int> span_array = ints;

0 commit comments

Comments
 (0)