10000 Merge pull request #5418 from crazymerlyn/memoryerror · RustPython/RustPython@29d9534 · GitHub
[go: up one dir, main page]

Skip to content

Commit 29d9534

Browse files
authored
Merge pull request #5418 from crazymerlyn/memoryerror
Handle MemoryError for sequences
2 parents 37dc28a + 6fb19ac commit 29d9534

File tree

2 files changed

+13
-2
lines changed

2 files changed

+13
-2
lines changed

vm/src/sequence.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
use crate::{
2-
builtins::PyIntRef, function::OptionalArg, sliceable::SequenceIndexOp, types::PyComparisonOp,
3-
vm::VirtualMachine, AsObject, PyObject, PyObjectRef, PyResult,
2+
builtins::PyIntRef,
3+
function::OptionalArg,
4+
sliceable::SequenceIndexOp,
5+
types::PyComparisonOp,
6+
vm::{VirtualMachine, MAX_MEMORY_SIZE},
7+
AsObject, PyObject, PyObjectRef, PyResult,
48
};
59
use optional::Optioned;
610
use std::ops::{Deref, Range};
@@ -95,6 +99,11 @@ where
9599
{
96100
fn mul(&self, vm: &VirtualMachine, n: isize) -> PyResult<Vec<T>> {
97101
let n = vm.check_repeat_or_overflow_error(self.as_ref().len(), n)?;
102+
103+
if n > 1 && std::mem::size_of_val(self.as_ref()) >= MAX_MEMORY_SIZE / n {
104+
return Err(vm.new_memory_error("".to_owned()));
105+
}
106+
98107
let mut v = Vec::with_capacity(n * self.as_ref().len());
99108
for _ in 0..n {
100109
v.extend_from_slice(self.as_ref());

vm/src/vm/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ pub use interpreter::Interpreter;
5353
pub(crate) use method::PyMethod;
5454
pub use setting::Settings;
5555

56+
pub const MAX_MEMORY_SIZE: usize = isize::MAX as usize;
57+
5658
// Objects are live when they are on stack, or referenced by a name (for now)
5759

5860
/// Top level container of a python virtual machine. In theory you could

0 commit comments

Comments
 (0)
0