From 26e9c7e42a6f9e20e4cb93e2de6ac7c865fb645a Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Sat, 5 Oct 2024 21:36:59 +0200 Subject: [PATCH] Add a bounds check and negative slice support to strided view fill_args --- include/xtensor/views/xstrided_view_base.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/include/xtensor/views/xstrided_view_base.hpp b/include/xtensor/views/xstrided_view_base.hpp index be67ee191..e5fa63e99 100644 --- a/include/xtensor/views/xstrided_view_base.hpp +++ b/include/xtensor/views/xstrided_view_base.hpp @@ -903,6 +903,14 @@ namespace xt if (ptr != nullptr) { auto slice0 = static_cast(*ptr); + if (slice0 < 0) + { + slice0 += shape[i_ax]; + } + if (slice0 < 0 || slice0 >= shape[i_ax]) + { + XTENSOR_THROW(std::runtime_error, "Slice index out of range."); + } new_offset += static_cast(slice0 * old_strides[i_ax]); } else if (std::get_if(&slices[i]) != nullptr)