8000 Handle MemoryError for sequences by crazymerlyn · Pull Request #5418 · RustPython/RustPython · GitHub
[go: up one dir, main page]

Skip to content

Handle MemoryError for sequences #5418

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

Merged
merged 1 commit into from
Sep 29, 2024
Merged
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
8000
13 changes: 11 additions & 2 deletions vm/src/sequence.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
use crate::{
builtins::PyIntRef, function::OptionalArg, sliceable::SequenceIndexOp, types::PyComparisonOp,
vm::VirtualMachine, AsObject, PyObject, PyObjectRef, PyResult,
builtins::PyIntRef,
function::OptionalArg,
sliceable::SequenceIndexOp,
types::PyComparisonOp,
vm::{VirtualMachine, MAX_MEMORY_SIZE},
AsObject, PyObject, PyObjectRef, PyResult,
};
use optional::Optioned;
use std::ops::{Deref, Range};
Expand Down Expand Up @@ -95,6 +99,11 @@ where
{
fn mul(&self, vm: &VirtualMachine, n: isize) -> PyResult<Vec<T>> {
let n = vm.check_repeat_or_overflow_error(self.as_ref().len(), n)?;

if n > 1 && std::mem::size_of_val(self.as_ref()) >= MAX_MEMORY_SIZE / n {
return Err(vm.new_memory_error("".to_owned()));
}

let mut v = Vec::with_capacity(n * self.as_ref().len());
for _ in 0..n {
v.extend_from_slice(self.as_ref());
Expand Down
2 changes: 2 additions & 0 deletions vm/src/vm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub use interpreter::Interpreter;
pub(crate) use method::PyMethod;
pub use setting::Settings;

pub const MAX_MEMORY_SIZE: usize = isize::MAX as usize;

// Objects are live when they are on stack, or referenced by a name (for now)

/// Top level container of a python virtual machine. In theory you could
Expand Down
Loading
0