Skip to content

Commit 09873f3

Browse files
authored
Check lower ranks in storage_view (OpenNMT#2073)
1 parent 9b14b45 commit 09873f3

3 files changed

Lines changed: 14 additions & 2 deletions

File tree

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
### Fixes and improvements
66

7+
* Fix out-of-bounds reads in StorageView index validation (#2073) by [@jordimas](https://github.com/jordimas), reported by Nathan Keys (Halo Forge Labs)
8+
79
## [v4.8.1](https://github.com/OpenNMT/CTranslate2/releases/tag/v4.8.1) (2026-07-03)
810

911
### New features

include/ctranslate2/storage_view.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ namespace ctranslate2 {
1919

2020
#define GUARD_DIM(DIM, RANK) \
2121
do { \
22-
if (DIM >= RANK) \
22+
if (DIM < 0 || DIM >= RANK) \
2323
THROW_INVALID_ARGUMENT("can't index dimension " \
2424
+ std::to_string(DIM) \
2525
+ " for a storage with rank " \
@@ -194,7 +194,7 @@ namespace ctranslate2 {
194194

195195
template <typename T>
196196
const T& at(dim_t index) const {
197-
if (index >= _size)
197+
if (index < 0 || index >= _size)
198198
THROW_INVALID_ARGUMENT("index is out of bounds ("
199199
+ std::to_string(index) + " >= "
200200
+ std::to_string(_size) + ")");

tests/storage_view_test.cc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,16 @@ TEST(StorageViewTest, ZeroDim) {
1717
EXPECT_EQ(b.dim(2), 2);
1818
}
1919

20+
TEST(StorageViewTest, InvalidNegativeDim) {
21+
StorageView scalar(1.0f);
22+
EXPECT_THROW(scalar.dim(-1), std::invalid_argument);
23+
}
24+
25+
TEST(StorageViewTest, InvalidNegativeIndex) {
26+
StorageView storage({1}, std::vector<float>{0});
27+
EXPECT_THROW(storage.at<float>(-1), std::invalid_argument);
28+
}
29+
2030
TEST(StorageViewTest, BoolOperator) {
2131
StorageView a;
2232
EXPECT_FALSE(bool(a));

0 commit comments

Comments
 (0)