8000 Handle MemoryError for list and strings. by philippeitis · Pull Request #1779 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Handle MemoryError for list and strings. #1779

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 23 additions & 15 deletions vm/src/obj/objlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use crate::pyobject::{
PyObjectRef, PyRef, PyResult, PyValue, TryFromObject, TypeProtocol,
};
use crate::sequence::{self, SimpleSeq};
use crate::vm::{ReprGuard, VirtualMachine};
use crate::vm::{ReprGuard, VirtualMachine, MAX_MEMORY_SIZE};

/// Built-in mutable sequence.
///
Expand Down Expand Up @@ -119,14 +119,16 @@ impl PyList {
Ok(result) => match result.as_bigint().to_u8() {
Some(result) => elements.push(result),
None => {
return Err(vm.new_value_error("bytes must be in range (0, 256)".to_owned()))
return Err(
vm.new_value_error("bytes must be in range (0, 256)".to_owned())
);
}
},
_ => {
return Err(vm.new_type_error(format!(
"'{}' object cannot be interpreted as an integer",
elem.class().name
)))
)));
}
}
}
Expand Down Expand Up @@ -468,25 +470,31 @@ impl PyList {
}

#[pymethod(name = "__mul__")]
fn mul(&self, counter: isize, vm: &VirtualMachine) -> PyObjectRef {
let new_elements = sequence::seq_mul(&self.borrow_sequence(), counter)
.cloned()
.collect();
vm.ctx.new_list(new_elements)
fn mul(&self, counter: isize, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
if counter < 0 || self.len() * (counter as usize) < MAX_MEMORY_SIZE {
let new_elements = sequence::seq_mul(&self.borrow_sequence(), counter)
.cloned()
.collect();
return Ok(vm.ctx.new_list(new_elements));
}
Err(vm.new_memory_error("".to_owned()))
}

#[pymethod(name = "__rmul__")]
fn rmul(&self, counter: isize, vm: &VirtualMachine) -> PyObjectRef {
fn rmul(&self, counter: isize, vm: &VirtualMachine) -> PyResult<PyObjectRef> {
self.mul(counter, &vm)
}

#[pymethod(name = "__imul__")]
fn imul(zelf: PyRef<Self>, counter: isize) -> PyRef<Self> {
let new_elements = sequence::seq_mul(&zelf.borrow_sequence(), counter)
.cloned()
.collect();
zelf.elements.replace(new_elements);
zelf
fn imul(zelf: PyRef<Self>, counter: isize, vm: &VirtualMachine) -> PyResult<PyRef<Self>> {
if counter < 0 || zelf.len() * (counter as usize) < MAX_MEMORY_SIZE {
let new_elements = sequence::seq_mul(&zelf.borrow_sequence(), counter)
.cloned()
.collect();
zelf.elements.replace(new_elements);
return Ok(zelf);
}
Err(vm.new_memory_error("".to_owned()))
}

#[pymethod]
Expand Down
19 changes: 11 additions & 8 deletions vm/src/obj/objstr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use crate::pyobject::{
Either, IdProtocol, IntoPyObject, ItemProtocol, PyClassImpl, PyContext, PyIterable,
PyObjectRef, PyRef, PyResult, PyValue, TryIntoRef, TypeProtocol,
};
use crate::vm::VirtualMachine;
use crate::vm::{VirtualMachine, MAX_MEMORY_SIZE};

/// str(object='') -> str
/// str(bytes_or_buffer[, encoding[, errors]]) -> str
Expand Down Expand Up @@ -328,13 +328,16 @@ impl PyString {

#[pymethod(name = "__mul__")]
fn mul(&self, multiplier: isize, vm: &VirtualMachine) -> PyResult<String> {
multiplier
.max(0)
.to_usize()
.map(|multiplier| self.value.repeat(multiplier))
.ok_or_else(|| {
vm.new_overflow_error("cannot fit 'int' into an index-sized integer".to_owned())
})
if multiplier < 0 || self.len() * (multiplier as usize) < MAX_MEMORY_SIZE {
return multiplier
.max(0)
.to_usize()
.map(|multiplier| self.value.repeat(multiplier))
.ok_or_else(|| {
vm.new_overflow_error("cannot fit 'int' into an index-sized integer".to_owned())
});
}
Err(vm.new_memory_error("".to_owned()))
}

#[pymethod(name = "__rmul__")]
Expand Down
6 changes: 6 additions & 0 deletions vm/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ pub struct VirtualMachine {
}

pub const NSIG: usize = 64;
pub const MAX_MEMORY_SIZE: usize = (std::usize::MAX >> 3) + 1;

#[derive(Copy, Clone)]
pub enum InitParameter {
Expand Down Expand Up @@ -383,6 +384,11 @@ impl VirtualMachine {
self.new_exception_msg(type_error, msg)
}

pub fn new_memory_error(&self, msg: String) -> PyBaseExceptionRef {
let memory_error = self.ctx.exceptions.memory_error.clone();
self.new_exception_msg(memory_error, msg)
}

pub fn new_name_error(&self, msg: String) -> PyBaseExceptionRef {
let name_error = self.ctx.exceptions.name_error.clone();
self.new_exception_msg(name_error, msg)
Expand Down
0