From 86be2d8cb4a58efa0bfdabeb99d41417e66d2da0 Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Sat, 5 Oct 2024 21:36:59 +0200 Subject: [PATCH 1/2] Add a bounds check and negative slice support to strided view fill_args --- include/xtensor/xstrided_view_base.hpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/xtensor/xstrided_view_base.hpp b/include/xtensor/xstrided_view_base.hpp index 9f3f8ef4c..98dbed93e 100644 --- a/include/xtensor/xstrided_view_base.hpp +++ b/include/xtensor/xstrided_view_base.hpp @@ -903,6 +903,11 @@ 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 (xtl::get_if(&slices[i]) != nullptr) From ea204f41fadd4abfc271288cf08aa1acdb2aa9ae Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Sun, 6 Oct 2024 00:17:13 +0200 Subject: [PATCH 2/2] Update xstrided_view_base.hpp --- include/xtensor/xstrided_view_base.hpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/include/xtensor/xstrided_view_base.hpp b/include/xtensor/xstrided_view_base.hpp index 98dbed93e..b4b26e01c 100644 --- a/include/xtensor/xstrided_view_base.hpp +++ b/include/xtensor/xstrided_view_base.hpp @@ -903,8 +903,11 @@ namespace xt if (ptr != nullptr) { auto slice0 = static_cast(*ptr); - if (slice0 < 0) slice0 += shape[i_ax]; - if (slice0 < 0 || slice0 >= shape[i_ax]) + if (slice0 < 0) + { + slice0 += shape[i_ax]; + } + if (slice0 < 0 || slice0 >= shape[i_ax]) { XTENSOR_THROW(std::runtime_error, "Slice index out of range."); }