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 21, 2024
1 parent 4877acf commit 0accddc
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions include/experimental/__p0009_bits/mdspan.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ class mdspan
{
size_t r = 0;
for (const auto& index : {indices...}) {
if (index >= __mapping_ref().extents().extent(r)) {
if (index >= __mapping_ref().extents().extent(r) || index < 0) {
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 +254,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 (indices[r] >= __mapping_ref().extents().extent(r) || indices[r] < 0) {
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 +274,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 (indices[r] >= __mapping_ref().extents().extent(r) || indices[r] < 0) {
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

0 comments on commit 0accddc

Please sign in to comment.