Skip to content

Fix GH-24492 OOM handling in URL encoding functions - #24596

Draft
Kaus1ubh wants to merge 1 commit into
php:PHP-8.4from
Kaus1ubh:codex/urlencode-fallible-oom
Draft

Kaus1ubh wants to merge 1 commit into
php:PHP-8.4from
Kaus1ubh:codex/urlencode-fallible-oom

Conversation

@Kaus1ubh

@Kaus1ubh Kaus1ubh commented Oct 23, 2026 •

Copy link
Copy Markdown

With a 16 MiB memory limit and a 200% hard limit, encoding a 12 MiB string of NUL bytes currently terminates the process with exit 137 before registered shutdown handlers run. Both urlencode() and rawurlencode() reach the non-fallible allocation path when reserving the output buffer.

This changes the PHP functions to use fallible allocation and return through RETURN_OOM() on failure. The original buffer is released if truncation fails. The exported C functions php_url_encode() and php_raw_url_encode() keep their existing allocation behavior and return contract; their callers in cookies, sessions, phpinfo and query building still assume a non-NULL result.

This addresses the two PHP functions from #24492 and complements #24548, which leaves URL encoding out of scope. Making the C functions fallible would change their contract for every caller, so it is left out of this fix.

Validation on Linux with a debug/AddressSanitizer build of PHP-8.4:

  • Both new OOM tests fail before the patch (exit 137, no shutdown output) and pass after it (normal fatal OOM, exit 255, shutdown output present).
  • URL encoding, query building, cookie and selected session tests: 60 tests, 59 pass, 1 documented ASAN skip, 0 failures. ASAN_OPTIONS=detect_leaks=0 SKIP_ASAN=1; normal Zend allocator enabled.
  • Output checks cover empty/short inputs, 15/16/17-byte prefixes, a longer prefix, NUL, spaces and escaped punctuation.

Rebased onto current PHP-8.4; the NEWS entry is under 8.4.29. CI covers Windows and the non-ASAN builds.

@cinematografiagranados-cpu

This comment was marked as spam.

@Kaus1ubh
Kaus1ubh force-pushed the codex/urlencode-fallible-oom branch from 72c0cec to d306d11 Compare November 23, 2026 21:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants