Skip to content

Commit

Permalink
Add boundary check in accessor subscriptor operator
Browse files Browse the repository at this point in the history
  • Loading branch information
facuMH committed Apr 25, 2023
1 parent 0685d94 commit 8345698
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions include/accessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -256,11 +256,40 @@ class accessor<DataT, Dims, Mode, target::device> : public detail::accessor_base

template <access_mode M = Mode>
inline std::enable_if_t<detail::access::mode_traits::is_producer(M), DataT&> operator[](const id<Dims>& index) const {
#if defined(CELERITY_DETAIL_ENABLE_DEBUG)
auto actual_index = index - m_index_offset;
if(actual_index >= m_buffer_range != detail::id_cast<Dims>(id<3>(false, false, false))) {
if(Dims == 1) {
printf("Accessor indexed out of bounds -- %d: %zu -- producer -- buff_range: %zu - offset: %zu\n", Dims, index.get(0), m_buffer_range.size(),
m_index_offset.get(0));
} else if(Dims == 2) {
printf("Accessor indexed out of bounds -- %d: %zu %zu -- producer -\n", Dims, index.get(0), index.get(1));
} else if(Dims == 3) {
printf("Accessor indexed out of bounds -- %d: %zu %zu %zu -- producer -\n", Dims, index.get(0), index.get(1), index.get(2));
} else {
printf("Accessor indexed out of bounds -- NO DIM -- producer -\n");
}
}
#endif
return m_device_ptr[get_linear_offset(index)];
}

template <access_mode M = Mode>
inline std::enable_if_t<detail::access::mode_traits::is_pure_consumer(M), const DataT&> operator[](const id<Dims>& index) const {
#if defined(CELERITY_DETAIL_ENABLE_DEBUG)
auto actual_index = index - m_index_offset;
if(actual_index >= m_buffer_range != detail::id_cast<Dims>(id<3>(false, false, false))) {
if(Dims == 1) {
printf("Accessor indexed out of bounds -- %d: %zu -- consumer -\n", Dims, index.get(0));
} else if(Dims == 2) {
printf("Accessor indexed out of bounds -- %d: %zu %zu -- consumer -\n", Dims, index.get(0), index.get(1));
} else if(Dims == 3) {
printf("Accessor indexed out of bounds -- %d: %zu %zu %zu -- consumer -\n", Dims, index.get(0), index.get(1), index.get(2));
} else {
printf("Accessor indexed out of bounds -- DIM -- consumer -\n");
}
}
#endif
return m_device_ptr[get_linear_offset(index)];
}

Expand Down

0 comments on commit 8345698

Please sign in to comment.