Skip to content

Commit 5a9024b

Browse files
author
zxy.monado
committed
fix(container): validate inner-most pointer indices
1 parent f29968d commit 5a9024b

2 files changed

Lines changed: 38 additions & 12 deletions

File tree

source/source_base/module_container/ATen/core/tensor.h

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -390,25 +390,30 @@ class Tensor {
390390
*
391391
* @return The pointer to the specified row.
392392
*
393-
* @note This function assumes the tensor is treated as a matrix, where each row
394-
* is a contiguous block of memory.
395-
* If the row index is out of bounds, the behavior is undefined.
393+
* @note This function supports rank-1 and rank-2 tensors. For a rank-1 tensor,
394+
* the index selects an element. For a rank-2 tensor, the index selects a row.
395+
* An invalid rank or index throws std::invalid_argument.
396396
*/
397397
template <typename T>
398-
T* inner_most_ptr(const int &index) const {
399-
if (shape_.ndim() > 2) {
400-
throw std::invalid_argument("Invalid call, inner_most_ptr only support tensor rank <= 2!");
398+
T* inner_most_ptr(const int& index) const
399+
{
400+
const unsigned int rank = shape_.ndim();
401+
if (rank == 0 || rank > 2)
402+
{
403+
throw std::invalid_argument("Invalid call, inner_most_ptr only supports tensor ranks 1 and 2!");
401404
}
402-
if (index > shape_.dim_size(static_cast<int>(shape_.ndim() - 2))) {
403-
throw std::invalid_argument("Invalid index, index of the inner-most must less than the inner-most shape size!");
405+
const int64_t outer_size = shape_.dim_size(0);
406+
if (index < 0 || static_cast<int64_t>(index) >= outer_size)
407+
{
408+
throw std::invalid_argument("Invalid index, inner_most_ptr index is out of bounds!");
404409
}
405-
if (shape_.ndim() == 1) {
410+
if (rank == 1)
411+
{
406412
return data<T>() + index;
407413
}
408-
return data<T>() + index * shape_.dim_size(static_cast<int>(shape_.ndim()) - 1);
414+
return data<T>() + index * shape_.dim_size(1);
409415
}
410416

411-
412417
/**
413418
* @brief Equality comparison operator for tensors.
414419
*

source/source_base/module_container/test/tensor_test.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,27 @@ TEST(Tensor, GetValueAndInnerMostPtr) {
218218
EXPECT_EQ(row_ptr[3], 12);
219219
}
220220

221+
TEST(Tensor, InnerMostPtrBounds)
222+
{
223+
container::Tensor vector(container::DataType::DT_INT, container::DeviceType::CpuDevice, {4});
224+
std::vector<int> values = {1, 2, 3, 4};
225+
memcpy(vector.data<int>(), values.data(), sizeof(int) * values.size());
226+
227+
auto element_ptr = vector.inner_most_ptr<int>(2);
228+
EXPECT_EQ(*element_ptr, 3);
229+
EXPECT_THROW(vector.inner_most_ptr<int>(-1), std::invalid_argument);
230+
EXPECT_THROW(vector.inner_most_ptr<int>(4), std::invalid_argument);
231+
232+
container::Tensor matrix(container::DataType::DT_INT, container::DeviceType::CpuDevice, {2, 2});
233+
EXPECT_THROW(matrix.inner_most_ptr<int>(2), std::invalid_argument);
234+
235+
container::Tensor empty(container::DataType::DT_INT, container::DeviceType::CpuDevice, container::TensorShape());
236+
EXPECT_THROW(empty.inner_most_ptr<int>(0), std::invalid_argument);
237+
238+
container::Tensor rank_three(container::DataType::DT_INT, container::DeviceType::CpuDevice, {1, 1, 1});
239+
EXPECT_THROW(rank_three.inner_most_ptr<int>(0), std::invalid_argument);
240+
}
241+
221242
TEST(Tensor, ReshapeDeathTest) {
222243
::testing::FLAGS_gtest_death_test_style = "threadsafe";
223244
container::Tensor t(container::DataType::DT_FLOAT, container::DeviceType::CpuDevice, {2, 3, 4});
@@ -533,4 +554,4 @@ TEST(Tensor, Accessor) {
533554
}
534555
}
535556

536-
} // namespace container
557+
} // namespace container

0 commit comments

Comments
 (0)