diff options
author | Bruce Merry <bmerry@ska.ac.za> | 2017-05-25 15:17:36 +0200 |
---|---|---|
committer | Jason Rhinelander <jason@imaginary.ca> | 2017-05-25 10:51:28 -0400 |
commit | eee4f4fc7ea10fdde4216ea677b5acbf85b856be (patch) | |
tree | c2000e7939a11f24526e4885a7a57d41983ce770 | |
parent | 6a0f6c40cd00f16fd3d145db1ae0f897da716965 (diff) | |
download | platform_external_python_pybind11-eee4f4fc7ea10fdde4216ea677b5acbf85b856be.tar.gz platform_external_python_pybind11-eee4f4fc7ea10fdde4216ea677b5acbf85b856be.tar.bz2 platform_external_python_pybind11-eee4f4fc7ea10fdde4216ea677b5acbf85b856be.zip |
Fix invalid memory access in vector insert method
The stl_bind.h wrapper for `Vector.insert` neglected to do a bounds
check.
-rw-r--r-- | include/pybind11/stl_bind.h | 2 | ||||
-rw-r--r-- | tests/test_stl_binders.py | 4 |
2 files changed, 5 insertions, 1 deletions
diff --git a/include/pybind11/stl_bind.h b/include/pybind11/stl_bind.h index 49dc9a5..9411747 100644 --- a/include/pybind11/stl_bind.h +++ b/include/pybind11/stl_bind.h @@ -145,6 +145,8 @@ void vector_modifiers(enable_if_t<std::is_copy_constructible<typename Vector::va cl.def("insert", [](Vector &v, SizeType i, const T &x) { + if (i > v.size()) + throw index_error(); v.insert(v.begin() + (DiffType) i, x); }, arg("i") , arg("x"), diff --git a/tests/test_stl_binders.py b/tests/test_stl_binders.py index b200be4..1d52b8b 100644 --- a/tests/test_stl_binders.py +++ b/tests/test_stl_binders.py @@ -18,11 +18,13 @@ def test_vector_int(): assert v_int != v_int2 v_int2.append(2) - v_int2.append(3) v_int2.insert(0, 1) v_int2.insert(0, 2) v_int2.insert(0, 3) + v_int2.insert(6, 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_int2[2:-2] = v_int |