Skip to content

Commit af49c9b

Browse files
committed
Enhance index_mapper with access control and add ASAN support in tests
1 parent fdb27ad commit af49c9b

4 files changed

Lines changed: 55 additions & 7 deletions

File tree

include/xtensor/views/index_mapper.hpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ namespace xt
193193
* @throws Assertion failure if `i != 0` for integral slices.
194194
* @throws Assertion failure if `i >= slice.size()` for non-integral slices.
195195
*/
196-
template <size_t I, std::integral Index>
196+
template <size_t I, access_t ACCESS, std::integral Index>
197197
size_t map_ith_index(const view_type& view, const Index i) const;
198198

199199
/**
@@ -490,16 +490,16 @@ namespace xt
490490
{
491491
if constexpr (ACCESS == access_t::SAFE)
492492
{
493-
return container.at(map_ith_index<Is>(view, indices[Is])...);
493+
return container.at(map_ith_index<Is, ACCESS>(view, indices[Is])...);
494494
}
495495
else
496496
{
497-
return container(map_ith_index<Is>(view, indices[Is])...);
497+
return container(map_ith_index<Is, ACCESS>(view, indices[Is])...);
498498
}
499499
}
500500

501501
template <class UnderlyingContainer, class... Slices>
502-
template <size_t I, std::integral Index>
502+
template <size_t I, access_t ACCESS, std::integral Index>
503503
auto
504504
index_mapper<xt::xview<UnderlyingContainer, Slices...>>::map_ith_index(const view_type& view, const Index i) const
505505
-> size_t
@@ -515,14 +515,51 @@ namespace xt
515515

516516
if constexpr (std::is_integral_v<current_slice>)
517517
{
518-
assert(i == 0);
518+
if constexpr (ACCESS == access_t::SAFE)
519+
{
520+
if (i != 0)
521+
{
522+
XTENSOR_THROW(std::out_of_range, "Index out of range in index_mapper access");
523+
}
524+
}
525+
else
526+
{
527+
assert(i == 0);
528+
}
519529
return size_t(slice);
520530
}
531+
else if constexpr (xt::detail::is_xall_slice<std::decay_t<current_slice>>::value)
532+
{
533+
return size_t(i);
534+
}
521535
else
522536
{
523537
using slice_size_type = typename current_slice::size_type;
524-
assert(i < slice.size());
525-
return size_t(slice(static_cast<slice_size_type>(i)));
538+
const auto slice_index = static_cast<slice_size_type>(i);
539+
540+
if constexpr (ACCESS == access_t::SAFE)
541+
{
542+
if constexpr (std::is_signed_v<slice_size_type>)
543+
{
544+
if (slice_index < 0 || slice_index >= slice.size())
545+
{
546+
XTENSOR_THROW(std::out_of_range, "Index out of range in index_mapper access");
547+
}
548+
}
549+
else if (slice_index >= slice.size())
550+
{
551+
XTENSOR_THROW(std::out_of_range, "Index out of range in index_mapper access");
552+
}
553+
}
554+
else
555+
{
556+
if constexpr (std::is_signed_v<slice_size_type>)
557+
{
558+
assert(slice_index >= 0);
559+
}
560+
assert(slice_index < slice.size());
561+
}
562+
return size_t(slice(slice_index));
526563
}
527564
}
528565
else

test/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,6 +261,8 @@ foreach(filename IN LISTS COMMON_BASE XTENSOR_TESTS)
261261
endif()
262262
target_include_directories(${targetname} PRIVATE ${XTENSOR_INCLUDE_DIR})
263263
target_link_libraries(${targetname} PRIVATE xtensor doctest::doctest ${CMAKE_THREAD_LIBS_INIT})
264+
target_compile_options(${targetname} PRIVATE $<$<BOOL:USE_SANITIZER>:${SANITIZER_COMPILE_OPTIONS}>)
265+
target_link_options(${targetname} PRIVATE $<$<BOOL:USE_SANITIZER>:${SANITIZER_LINK_OPTIONS}>)
264266
add_custom_target(
265267
x${targetname}
266268
COMMAND ${targetname}
@@ -285,6 +287,9 @@ if(XTENSOR_USE_OPENMP)
285287
target_compile_definitions(test_xtensor_lib PRIVATE XTENSOR_USE_OPENMP)
286288
endif()
287289

290+
target_compile_options(test_xtensor_lib PRIVATE $<$<BOOL:USE_SANITIZER>:${SANITIZER_COMPILE_OPTIONS}>)
291+
target_link_options(test_xtensor_lib PRIVATE $<$<BOOL:USE_SANITIZER>:${SANITIZER_LINK_OPTIONS}>)
292+
288293
target_include_directories(test_xtensor_lib PRIVATE ${XTENSOR_INCLUDE_DIR})
289294
target_link_libraries(test_xtensor_lib PRIVATE xtensor doctest::doctest ${CMAKE_THREAD_LIBS_INIT})
290295

test/test_xadapt.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,8 @@ namespace xt
132132
a1(1, 0) = static_cast<int>(i);
133133
EXPECT_EQ(i, data[i * size + st]);
134134
}
135+
136+
delete[] data;
135137
}
136138

137139
TEST(xarray_adaptor, pointer_acquire_ownership)
@@ -300,6 +302,8 @@ namespace xt
300302
a1(1, 0) = static_cast<int>(i);
301303
EXPECT_EQ(i, data[i * size + st]);
302304
}
305+
306+
delete[] data;
303307
}
304308

305309
TEST(xtensor_adaptor, pointer_const_no_ownership)

test/test_xbuffer_adaptor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,8 @@ namespace xt
201201
size_t size2 = 50;
202202
XT_EXPECT_THROW(adapt.resize(size2), std::runtime_error);
203203
EXPECT_EQ(adapt.size(), size1);
204+
205+
delete[] data1;
204206
}
205207

206208
TEST(xbuffer_adaptor, no_owner_iterating)

0 commit comments

Comments
 (0)