Skip to content

Fix SplObjectStorage getHash() guard leaking across a bailout#22308

Open
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/spl-observer-gethash-bailout
Open

Fix SplObjectStorage getHash() guard leaking across a bailout#22308
iliaal wants to merge 1 commit into
php:masterfrom
iliaal:fix/spl-observer-gethash-bailout

Conversation

@iliaal

@iliaal iliaal commented Jun 14, 2026

Copy link
Copy Markdown
Contributor

The getHash() recursion guard bumps a request-persistent counter around the userland getHash() call but only decrements it on the normal return. A bailout inside an overridden getHash() (out of memory, timeout, any fatal) skips the decrement, and nothing resets the counter per request, so on a persistent SAPI every later request on the same worker wrongly throws "Modification of SplObjectStorage during getHash() is prohibited". Decrement inside a zend_catch and re-raise the bailout so the counter is balanced on every exit.

class P extends SplObjectStorage {
    public function getHash($o): string {
        ini_set('memory_limit', '2M');
        str_repeat('a', 100 * 1024 * 1024); // OOM bailout
        return 'x';
    }
}
(new P())->offsetSet(new stdClass());           // request 1 bails out
(new SplObjectStorage())->offsetSet(new stdClass()); // later request, no override: throws before the patch

@ndossche

Copy link
Copy Markdown
Member

A much nicer way is likely to reset spl_object_storage_get_hash_depth on request init/shutdown.

@iliaal iliaal force-pushed the fix/spl-observer-gethash-bailout branch from 58eada0 to 3fc40d5 Compare June 14, 2026 22:40
@iliaal

iliaal commented Jun 14, 2026

Copy link
Copy Markdown
Contributor Author

Agreed, cleaner. Reset the counter in SPL's existing RINIT and dropped the zend_try/catch. Folded into PHP_RINIT(spl) rather than a new per-extension hook, and RINIT over RSHUTDOWN so a worker that skipped a clean shutdown still starts at zero.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You need a specific skipif include for the usage of php_cli_server_start in your test

The getHash() recursion guard increments a request-persistent counter
around the userland getHash() call but decrements it only on the normal
return path. A bailout inside an overridden getHash() (out-of-memory,
timeout, or any fatal) skips the decrement, and the counter is never
reset per request, so on a persistent SAPI every later request on the
same worker wrongly throws "Modification of SplObjectStorage during
getHash() is prohibited". Reset the counter in the SPL request init so
each request starts at zero regardless of how the previous one exited.
@iliaal iliaal force-pushed the fix/spl-observer-gethash-bailout branch from 3fc40d5 to c6ff5fc Compare June 15, 2026 11:22
@iliaal

iliaal commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

Moved it to ext/spl/tests/ and added the php_cli_server.inc availability guard to SKIPIF.

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

I think this is also okay to go into 8.4.

@iliaal

iliaal commented Jun 15, 2026

Copy link
Copy Markdown
Contributor Author

I think this is also okay to go into 8.4.

8.4 and 8.5 don't have the getHash() recursion guard at all, it was added on master in d694135 (GH-21831). No counter there to leak across a bailout, so nothing to backport.

@ndossche

Copy link
Copy Markdown
Member

Ah okay, apologies. Good to go then

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