Skip to content

Commit 956073f

Browse files
author
Rue Yokaze
committed
add boundary check
1 parent 46b120c commit 956073f

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

python_multi_array.cpp

+11-2
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,10 @@ namespace python_multi_array
168168
T* ptr = This->origin();
169169
for (size_t i = 0; i < N; ++i)
170170
{
171+
if (This->shape()[i] <= s[i])
172+
{
173+
throw std::invalid_argument("index");
174+
}
171175
ptr += This->strides()[i] * s[i];
172176
}
173177
return *ptr;
@@ -179,6 +183,10 @@ namespace python_multi_array
179183
T* ptr = This->origin();
180184
for (size_t i = 0; i < N; ++i)
181185
{
186+
if (This->shape()[i] <= s[i])
187+
{
188+
throw std::invalid_argument("index");
189+
}
182190
ptr += This->strides()[i] * s[i];
183191
}
184192
*ptr = value;
@@ -205,7 +213,7 @@ namespace python_multi_array
205213
// assume idx to be a list or a tuple
206214
if (N != python::len(idx))
207215
{
208-
throw std::invalid_argument("idx");
216+
throw std::invalid_argument("index");
209217
}
210218
size_t s[N];
211219
for (size_t i = 0; i < N; ++i)
@@ -236,7 +244,7 @@ namespace python_multi_array
236244
// assume idx to be a list or a tuple
237245
if (N != python::len(idx))
238246
{
239-
throw std::invalid_argument("idx");
247+
throw std::invalid_argument("index");
240248
}
241249
size_t s[N];
242250
for (size_t i = 0; i < N; ++i)
@@ -679,6 +687,7 @@ BOOST_PYTHON_MODULE(multi_array)
679687

680688
def("make", make);
681689

690+
// define aliases of numpy data types
682691
python::scope This;
683692
This.attr("bool8") = bool8;
684693
This.attr("uint8") = uint8;

0 commit comments

Comments
 (0)