Fix invalid memory access in vector insert method

The stl_bind.h wrapper for `Vector.insert` neglected to do a bounds
check.
This commit is contained in:
Bruce Merry 2017-05-25 15:17:36 +02:00 committed by Jason Rhinelander
parent 6a0f6c40cd
commit eee4f4fc7e
2 changed files with 5 additions and 1 deletions

View File

@ -145,6 +145,8 @@ void vector_modifiers(enable_if_t<std::is_copy_constructible<typename Vector::va
cl.def("insert", cl.def("insert",
[](Vector &v, SizeType i, const T &x) { [](Vector &v, SizeType i, const T &x) {
if (i > v.size())
throw index_error();
v.insert(v.begin() + (DiffType) i, x); v.insert(v.begin() + (DiffType) i, x);
}, },
arg("i") , arg("x"), arg("i") , arg("x"),

View File

@ -18,11 +18,13 @@ def test_vector_int():
assert v_int != v_int2 assert v_int != v_int2
v_int2.append(2) v_int2.append(2)
v_int2.append(3)
v_int2.insert(0, 1) v_int2.insert(0, 1)
v_int2.insert(0, 2) v_int2.insert(0, 2)
v_int2.insert(0, 3) v_int2.insert(0, 3)
v_int2.insert(6, 3)
assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]" assert str(v_int2) == "VectorInt[3, 2, 1, 0, 1, 2, 3]"
with pytest.raises(IndexError):
v_int2.insert(8, 4)
v_int.append(99) v_int.append(99)
v_int2[2:-2] = v_int v_int2[2:-2] = v_int