Skip to content

Commit 5722a51

Browse files
committed
Fix crash with out of bounds index (issue #572)
1 parent 3703a78 commit 5722a51

File tree

3 files changed

+44
-1
lines changed

3 files changed

+44
-1
lines changed

include/chaiscript/dispatchkit/bootstrap_stl.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ namespace chaiscript::bootstrap::standard_library {
130130
auto itr = container.begin();
131131
auto end = container.end();
132132

133-
if (pos < 0 || std::distance(itr, end) < (pos - 1)) {
133+
if (pos < 0 || std::distance(itr, end) <= pos) {
134134
throw std::range_error("Cannot erase past end of range");
135135
}
136136

unittests/vector_erase_at.chai

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,24 @@
11
auto x = [1, 2, 3]
22
x.erase_at(1)
33
assert_equal([1,3], x);
4+
5+
try {
6+
// We expect this to throw because of erasing an out of bounds index
7+
x.erase_at(2)
8+
assert_true(false)
9+
} catch (e) {
10+
assert_true(true)
11+
}
12+
13+
try {
14+
// We expect this to throw because of erasing an out of bounds index
15+
x.erase_at(-1)
16+
assert_true(false)
17+
} catch (e) {
18+
assert_true(true)
19+
}
20+
21+
x.erase_at(0)
22+
assert_equal([3], x)
23+
x.erase_at(0)
24+
assert_equal([], x)

unittests/vector_insert_at.chai

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
11
auto x = [1, 2, 3]
22
x.insert_at(1, 6)
33
assert_equal([1,6,2,3], x);
4+
5+
try {
6+
// We expect this to throw because of inserting an out of bounds index
7+
x.insert_at(5, 55)
8+
assert_true(false)
9+
} catch (e) {
10+
assert_true(true)
11+
}
12+
13+
// Inserting to the end should be allowed
14+
x.insert_at(4, 44)
15+
16+
try {
17+
// We expect this to throw because of inserting an out of bounds index
18+
x.insert_at(-1, 111)
19+
assert_true(false)
20+
} catch (e) {
21+
assert_true(true)
22+
}
23+
24+
x.insert_at(0, 100)
25+
assert_equal([100, 1, 6, 2, 3, 44], x)

0 commit comments

Comments
 (0)