Skip to content

Commit

Permalink
Check negative indicies in at()
Browse files Browse the repository at this point in the history
Signed-off-by: Stephan Lachnit <[email protected]>
  • Loading branch information
stephanlachnit committed Aug 22, 2024
1 parent ab57448 commit 4cff9ce
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions include/experimental/__p0009_bits/mdspan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <stdexcept>
#include <string>
#include <type_traits>

namespace MDSPAN_IMPL_STANDARD_NAMESPACE {
template <
Expand Down Expand Up @@ -234,7 +235,7 @@ class mdspan
{
size_t r = 0;
for (const auto& index : {indices...}) {
if (index >= __mapping_ref().extents().extent(r)) {
if (__is_index_oor(index, __mapping_ref().extents().extent(r))) {
throw std::out_of_range(
"mdspan::at(...," + std::to_string(index) + ",...) out-of-range at rank index " + std::to_string(r) +
" for mdspan with extent {...," + std::to_string(__mapping_ref().extents().extent(r)) + ",...}");
Expand All @@ -254,7 +255,7 @@ class mdspan
constexpr reference at(const std::array<SizeType, rank()>& indices) const
{
for (size_t r = 0; r < indices.size(); ++r) {
if (indices[r] >= __mapping_ref().extents().extent(r)) {
if (__is_index_oor(indices[r], __mapping_ref().extents().extent(r))) {
throw std::out_of_range(
"mdspan::at({...," + std::to_string(indices[r]) + ",...}) out-of-range at rank index " + std::to_string(r) +
" for mdspan with extent {...," + std::to_string(__mapping_ref().extents().extent(r)) + ",...}");
Expand All @@ -274,7 +275,7 @@ class mdspan
constexpr reference at(std::span<SizeType, rank()> indices) const
{
for (size_t r = 0; r < indices.size(); ++r) {
if (indices[r] >= __mapping_ref().extents().extent(r)) {
if (__is_index_oor(indices[r], __mapping_ref().extents().extent(r))) {
throw std::out_of_range(
"mdspan::at({...," + std::to_string(indices[r]) + ",...}) out-of-range at rank index " + std::to_string(r) +
" for mdspan with extent {...," + std::to_string(__mapping_ref().extents().extent(r)) + ",...}");
Expand Down Expand Up @@ -441,6 +442,23 @@ class mdspan
MDSPAN_FORCE_INLINE_FUNCTION constexpr mapping_type const& __mapping_ref() const noexcept { return __members.__second().__first(); }
MDSPAN_FORCE_INLINE_FUNCTION _MDSPAN_CONSTEXPR_14 accessor_type& __accessor_ref() noexcept { return __members.__second().__second(); }
MDSPAN_FORCE_INLINE_FUNCTION constexpr accessor_type const& __accessor_ref() const noexcept { return __members.__second().__second(); }

MDSPAN_TEMPLATE_REQUIRES(
class SizeType,
/* requires */ (
_MDSPAN_TRAIT(std::is_convertible, const SizeType&, index_type) &&
_MDSPAN_TRAIT(std::is_nothrow_constructible, index_type, const SizeType&)
)
)
MDSPAN_FORCE_INLINE_FUNCTION constexpr bool __is_index_oor(SizeType index, index_type extent) const noexcept {
// Check for negative indices
if constexpr(std::is_signed_v<SizeType>) {
if(index < 0) {
return true;
}
}
return static_cast<index_type>(index) >= extent;
}

template <class, class, class, class>
friend class mdspan;
Expand Down

0 comments on commit 4cff9ce

Please sign in to comment.