diff --git a/deps/ox/src/ox/std/array.hpp b/deps/ox/src/ox/std/array.hpp index 32281dec..7a212f01 100644 --- a/deps/ox/src/ox/std/array.hpp +++ b/deps/ox/src/ox/std/array.hpp @@ -181,13 +181,13 @@ constexpr Array &Array::operator=(Array &&other) noe template constexpr T &Array::operator[](std::size_t i) noexcept { - ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Array access overflow"); + boundsCheck(__FILE__, __LINE__, i, size(), "Array access overflow"); return m_items[i]; } template constexpr const T &Array::operator[](std::size_t i) const noexcept { - ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Array access overflow"); + boundsCheck(__FILE__, __LINE__, i, size(), "Array access overflow"); return m_items[i]; } diff --git a/deps/ox/src/ox/std/defines.hpp b/deps/ox/src/ox/std/defines.hpp index 0c274a76..63d75b03 100644 --- a/deps/ox/src/ox/std/defines.hpp +++ b/deps/ox/src/ox/std/defines.hpp @@ -41,6 +41,12 @@ constexpr auto Debug = true; constexpr auto Debug = false; #endif +#if defined(OX_CHECK_BOUNDS) +constexpr auto CheckBounds = true; +#else +constexpr auto CheckBounds = Debug; +#endif + #if defined(NDEBUG) constexpr auto NDebug = true; #else diff --git a/deps/ox/src/ox/std/error.hpp b/deps/ox/src/ox/std/error.hpp index 8161e101..6030a524 100644 --- a/deps/ox/src/ox/std/error.hpp +++ b/deps/ox/src/ox/std/error.hpp @@ -330,4 +330,17 @@ constexpr void primitiveAssert(char const*file, int line, bool pass, char const* } } +constexpr void boundsCheck( + char const*file, + int const line, + size_t const i, + size_t const sz, + char const*msg) noexcept { + if constexpr(defines::CheckBounds) { + if (i >= sz) [[unlikely]] { + panic(file, line, msg, ox::Error{1}); + } + } +} + } diff --git a/deps/ox/src/ox/std/iterator.hpp b/deps/ox/src/ox/std/iterator.hpp index 1f062979..381a8549 100644 --- a/deps/ox/src/ox/std/iterator.hpp +++ b/deps/ox/src/ox/std/iterator.hpp @@ -133,17 +133,17 @@ struct SpanIterator { } constexpr PtrType operator->() const noexcept { - ox::primitiveAssert(__FILE__, __LINE__, m_offset < m_max, "SpanIterator access overflow"); + boundsCheck(__FILE__, __LINE__, m_offset, m_max, "SpanIterator access overflow"); return &m_t[m_offset]; } constexpr RefType operator*() const noexcept { - ox::primitiveAssert(__FILE__, __LINE__, m_offset < m_max, "SpanIterator access overflow"); + boundsCheck(__FILE__, __LINE__, m_offset, m_max, "SpanIterator access overflow"); return m_t[m_offset]; } constexpr RefType operator[](std::size_t s) const noexcept { - ox::primitiveAssert(__FILE__, __LINE__, s < m_max, "SpanIterator access overflow"); + boundsCheck(__FILE__, __LINE__, s, m_max, "SpanIterator access overflow"); return m_t[s]; } diff --git a/deps/ox/src/ox/std/span.hpp b/deps/ox/src/ox/std/span.hpp index 3114aa0f..a0b7f993 100644 --- a/deps/ox/src/ox/std/span.hpp +++ b/deps/ox/src/ox/std/span.hpp @@ -129,22 +129,22 @@ class Span { } constexpr T &operator[](std::size_t i) noexcept { - ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span access overflow"); + boundsCheck(__FILE__, __LINE__, i, size(), "Span access overflow"); return m_items[i]; } constexpr T const&operator[](std::size_t i) const noexcept { - ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span access overflow"); + boundsCheck(__FILE__, __LINE__, i, size(), "Span access overflow"); return m_items[i]; } constexpr Span operator+(size_t i) const noexcept { - ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span access overflow"); + boundsCheck(__FILE__, __LINE__, i, size(), "Span access overflow"); return {m_items + i, m_size - i}; } constexpr Span operator+=(size_t i) noexcept { - ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Span access overflow"); + boundsCheck(__FILE__, __LINE__, i, size(), "Span access overflow"); m_items += i; m_size -= i; return *this; diff --git a/deps/ox/src/ox/std/vector.hpp b/deps/ox/src/ox/std/vector.hpp index b5b4f446..7f10ff5b 100644 --- a/deps/ox/src/ox/std/vector.hpp +++ b/deps/ox/src/ox/std/vector.hpp @@ -427,13 +427,13 @@ constexpr Vector &Vector constexpr T &Vector::operator[](std::size_t i) noexcept { - ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Vector access overflow"); + boundsCheck(__FILE__, __LINE__, i, size(), "Vector access overflow"); return m_items[i]; } template constexpr const T &Vector::operator[](std::size_t i) const noexcept { - ox::primitiveAssert(__FILE__, __LINE__, i < size(), "Vector access overflow"); + boundsCheck(__FILE__, __LINE__, i, size(), "Vector access overflow"); return m_items[i]; }