Check str case methods and re.escape against max_memory
Summary
With max_memory set, the str case methods and re.escape can kill the worker instead of raising MemoryError:
monty --max-memory 12MB -c "s = 'x' * 9_000_000; y = s.upper()"
monty worker: allocation of 9000000 bytes exceeds the memory limit (exit 65)
monty --max-memory 8MB -c "import re; y = re.escape(' ' * 4_500_000)"
monty worker: allocation of 9000000 bytes exceeds the memory limit (exit 65)
Both build their result in a plain String that the tracker never sees, so a large enough input crosses the hard ceiling before the next checkpoint. Lowercasing or escaping a document that is a good fraction of the budget is ordinary code, so this seemed worth the same graceful path the split and findall preflights have.
Changes:
re.escape: pre-check the existing2 * lenreservation withcheck_estimated_size. The loop is unchanged.- ASCII
lower/upper/casefold: pre-checks.len(), which is exact. - Everything else (non-ASCII input, and
capitalize/title/swapcase) goes through a newmap_casehelper inunicode_type.rs. A mapping can be longer than its input ('ɐ'.upper()is 3 bytes from 2,'ΐ'.upper()is 6 from 2), so a check sized to the input is not enough. For inputs overLARGE_RESULT_THRESHOLD / 12bytes,map_casechecks the initial buffer and every later growth withcheck_allocation, and maps in chunks that cannot reallocate behind the check. Smaller inputs take the old path unchanged. I didn't useStringBuilderhere because its 2x growth would refuse results that fit, and the per-charResultcost about 20% on short non-ASCII strings. - Updated the pre-checked lists in
limitations/resource_limits.md,docs/resource-limits.mdand CLAUDE.md, and droppeds.to_lowercase()from CLAUDE.md's "already bounded" examples.
bytes.lower() and friends are left alone because #1208 already adds check_allocation to them.
Tests
-
Added cases to
large_allocations_are_rejected_before_the_hard_limit: ASCIIlowerandtitle,'é'(non-ASCII, same length),'ΐ'.upper()(fails in the growth check, not the initial one) andre.escape. On main the test fails withmonty worker: allocation of 16777216 bytes exceeds the memory limit/unexpected EOF from worker. With this change it passes. -
Added asserts to
test_cases/str__methods.pywith inputs over the threshold, so the chunked path is checked against CPython (expansion, final sigma, title, swapcase, capitalize, casefold). -
cargo test -p monty-runtime --test subprocess,cargo test -p montyandmake test-casesall pass, along withmake format-rs lint-rs lint-py lint-md. -
A sweep over 5 chars (
ɐ ΐ İ é ß), 4 methods and 15 sizes up to 15 MiB at a 24 MiB limit: main killed the worker in 8 of 300 runs, this branch in 0. Those 8 now raiseMemoryError, and every other run gives the same result on both. -
Timing on release builds, best of 9 runs: 20-char and 1 MB strings, ASCII and non-ASCII, are all within noise of main. A 1 MB string that expands on
upper()is about 30% faster, because it reallocates a few times instead of doubling.re.escapeis unchanged. -
I have read the contribution guide.
-
I understand and can explain the proposed changes, including any generated with AI assistance.
Summary by cubic
Fixes str case methods and re.escape killing the worker with max_memory set instead of raising MemoryError. Both previously built their result in a plain String that the memory tracker never saw, so a large enough input crossed the hard ceiling before the next checkpoint.
re.escapepre-checks its existing2 * lenreservation withcheck_estimated_size; the loop is unchanged.- ASCII
lower/upper/casefoldpre-checks.len(), which is exact. - Non-ASCII mappings go through a new
map_casehelper that checks the initial buffer and every later growth for inputs overLARGE_RESULT_THRESHOLD / 12bytes; smaller inputs take the old path. bytes.lower()and friends are left alone; #1208 already addscheck_allocationto them.