Skip to content

Commit 8ded37d

Browse files
committed
Raise OverflowError is the index for list.index() overflows
1 parent b101a52 commit 8ded37d

File tree

3 files changed

+15
-0
lines changed

3 files changed

+15
-0
lines changed

tests/snippets/list.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,10 @@
6060
x = ['a', 'b', 'c']
6161
x.insert(-100, 'z')
6262
assert x == ['z', 'a', 'b', 'c']
63+
64+
try:
65+
x.insert(100000000000000000000, 'z')
66+
except OverflowError:
67+
pass
68+
else:
69+
assert False, "OverflowError was not raised"

vm/src/obj/objlist.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -277,6 +277,9 @@ fn list_insert(vm: &mut VirtualMachine, args: PyFuncArgs) -> PyResult {
277277
(element, None)
278278
]
279279
);
280+
if objint::get_value(insert_position) > std::usize::MAX.into() {
281+
return Err(vm.new_overflow_error("Python int too large to convert to Rust usize".to_string()));
282+
}
280283
let mut vec = get_mut_elements(list);
281284
let position = match objint::get_value(insert_position) {
282285
ref i if (*i).is_negative() => {

vm/src/vm.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,11 @@ impl VirtualMachine {
153153
self.new_exception(zero_division_error, msg)
154154
}
155155

156+
pub fn new_overflow_error (&mut self, msg: String) -> PyObjectRef {
157+
let overflow_error = self.ctx.exceptions.overflow_error.clone();
158+
self.new_exception(overflow_error, msg)
159+
}
160+
156161
pub fn new_scope(&mut self, parent_scope: Option<PyObjectRef>) -> PyObjectRef {
157162
// let parent_scope = self.current_frame_mut().locals.clone();
158163
self.ctx.new_scope(parent_scope)

0 commit comments

Comments
 (0)