[php-src] PR #22259: ext/bcmath: guard BcMath\Number against an uninitialized bc_num
| From: | nicolas-grekas | Date: | Mon, 08 Jun 2026 18:56:20 +0000 |
| Subject: | [php-src] PR #22259: ext/bcmath: guard BcMath\Number against an uninitialized bc_num | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-36347@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/22259
Author: nicolas-grekas
BcMath\Number is a final internal class with a custom
create_object, so the engine rejects
ReflectionClass::newInstanceWithoutConstructor() for it and unserialize()
always routes through __unserialize(). A freshly create_object-ed instance
has a NULL bc_num only transiently, before
__construct()/__unserialize() runs; an uninitialized instance can never
escape to userland.
C code can however build one via create_object directly (e.g. the deep-clone extension
at symfony/php-ext-deepclone, while reconstructing object graphs). Every BcMath\Number
handler dereferences intern->num, so such an instance is a NULL-pointer dereference
waiting to happen: (string) $n, clone $n, $n <=> $x,
$n + 1, $n->add(...), (bool) $n, var_dump($n)
all crash.
This guards the entry points that read num (value stringification /
__serialize / get_properties_for, clone, the
compare and do_operation operator overloads, the
arithmetic/sqrt/floor/ceil/round/divmod/powmod/compare
methods, the bool cast and has_property) so they throw
> The BcMath\Number object has not been correctly initialized by its constructor
instead of crashing, reusing the wording DateTime/DateTimeZone already use
for the same situation. Fully constructed instances are unaffected.
No .phpt is included because the uninitialized state is not reachable from pure PHP; it
is exercised by the deep-clone extension's test suite.
Surfaced while adding BcMath\Number support to the deep-clone extension and its
polyfill for symfony/symfony#64323.