◐ Shell
clean mode source ↗

Handle MemoryError for list and strings. by philippeitis · Pull Request #1779 · RustPython/RustPython

@philippeitis

This aims to address #1750 by raising a Memory error for excessively large allocations. It adds a MAX_MEMORY_SIZE const to VM which is then used by objlist.rs and objstr.rs when doing imul or mul operations.

philippeitis

@coolreader18

I'm not sure if this is the correct approach; 2**61 bytes is 2.3 exabytes, which probably wouldn't get hit before an actual OOM error occurs in the process. There is a RFC that allows for catching OOMs in a few different ways (try_reserve -> Result<(), ...> functions on Vec or String, oom=panic rustc option that will panic and unwind instead of just aborting) but none of them are stable yet. I'm not sure how to approach this problem, honestly.

@philippeitis

That is true, but this is also a reasonable sanity check, since a list with 8 byte pointers can only contain up to usize::MAX / 8 elements. Beyond that, allocating becomes the virtual machine's problem and will need to dealt with accordingly.

auvipy

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be great if you could rebase it for further review.