Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

### Fixes and improvements

* Fix out-of-bounds reads in StorageView index validation (#2073) by [@jordimas](https://github.com/jordimas), reported by Nathan Keys (Halo Forge Labs)

## [v4.8.1](https://github.com/OpenNMT/CTranslate2/releases/tag/v4.8.1) (2026-07-03)

### New features
Expand Down
4 changes: 2 additions & 2 deletions include/ctranslate2/storage_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace ctranslate2 {

#define GUARD_DIM(DIM, RANK) \
do { \
if (DIM >= RANK) \
if (DIM < 0 || DIM >= RANK) \
THROW_INVALID_ARGUMENT("can't index dimension " \
+ std::to_string(DIM) \
+ " for a storage with rank " \
Expand Down Expand Up @@ -194,7 +194,7 @@ namespace ctranslate2 {

template <typename T>
const T& at(dim_t index) const {
if (index >= _size)
if (index < 0 || index >= _size)
THROW_INVALID_ARGUMENT("index is out of bounds ("
+ std::to_string(index) + " >= "
+ std::to_string(_size) + ")");
Expand Down
10 changes: 10 additions & 0 deletions tests/storage_view_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ TEST(StorageViewTest, ZeroDim) {
EXPECT_EQ(b.dim(2), 2);
}

TEST(StorageViewTest, InvalidNegativeDim) {
StorageView scalar(1.0f);
EXPECT_THROW(scalar.dim(-1), std::invalid_argument);
}

TEST(StorageViewTest, InvalidNegativeIndex) {
StorageView storage({1}, std::vector<float>{0});
EXPECT_THROW(storage.at<float>(-1), std::invalid_argument);
}

TEST(StorageViewTest, BoolOperator) {
StorageView a;
EXPECT_FALSE(bool(a));
Expand Down
Loading