Skip to content

php: every multiplication over 2**32 was silently a float - #732

Merged
staalmannen merged 1 commit into
mainfrom
claude/upgrade-ape-c-library-mmZGd
Nov 5, 2026
Merged

staalmannen merged 1 commit into
mainfrom
claude/upgrade-ape-c-library-mmZGd

Conversation

@staalmannen

Copy link
Copy Markdown
Owner

exit() is fixed -- the smoke test runs to completion and its exit status is usable again. The two cases left were both `-9223372036854775807 - 1' coming back a float while PHP_INT_MIN itself was a clean int.

The subtraction was innocent, and the compiler said so: the host kencc compiled php's real zend_operators.c and fast_long_sub_function is SUBQ, ANDQ with the sign mask in a register, CMPQ -- 64 bits throughout, and tracing it with 0x8000000000000001 and 1 takes the integer arm. Reading the generated code of the real function beat reasoning about which #elif arm the config selects.

PHP compiles unary minus as (-1) * x, so the float came from the multiply. zend_multiply.h:132's fallback -- the arm taken with no _builtin*_overflow, no GNUC asm and no Win32, which is exactly this port -- opens

long   __lres  = (a) * (b);

and every other arm in that file is guarded by
SIZEOF_LONG == SIZEOF_ZEND_LONG or SIZEOF_ZEND_LONG == 4. This one assumes it. long is 32 bits here and zend_long is 64, so __lres held the product's low half, the delta test against __dres reported an overflow that had not happened, and the result was a double.

So every integer multiplication whose product did not fit in 32 bits silently returned a float: 2000000 * 2000000 was float(4.0E+12). A wrong answer in the commonest arithmetic there is, with no diagnostic, reaching every php program run on this port.

zend_long __lres now. The before/after -S diff is four lines: IMULQ into a register that was then MOVQL'd to 32 bits and CVTSL2SD'd, against IMULQ kept whole and CVTSQ2SD. 515 TUs still compile.

The test needed correcting too. Its first version covered +, -, max, strlen and str_repeat and not `*', so it caught this only sideways through unary minus, and nothing in the failing output said the word multiply. Section 3b asks directly now and includes two cases that a fix which simply never reports an overflow would fail: PHP_INT_MAX * 2 and PHP_INT_MIN * 2 must still be floats. 0 failures on the host.

Second round running in which the tree's most-cited invariant -- long is 32 bits on amd64 -- arrives inside upstream code that is correct on every LP64 platform.

Claude-Session: https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs

exit() is fixed -- the smoke test runs to completion and its exit
status is usable again.  The two cases left were both
`-9223372036854775807 - 1' coming back a float while PHP_INT_MIN
itself was a clean int.

The subtraction was innocent, and the compiler said so: the host
kencc compiled php's real zend_operators.c and fast_long_sub_function
is SUBQ, ANDQ with the sign mask in a register, CMPQ -- 64 bits
throughout, and tracing it with 0x8000000000000001 and 1 takes the
integer arm.  Reading the generated code of the real function beat
reasoning about which #elif arm the config selects.

PHP compiles unary minus as (-1) * x, so the float came from the
multiply.  zend_multiply.h:132's fallback -- the arm taken with no
__builtin_*_overflow, no __GNUC__ asm and no Win32, which is exactly
this port -- opens

	long   __lres  = (a) * (b);

and every other arm in that file is guarded by
SIZEOF_LONG == SIZEOF_ZEND_LONG or SIZEOF_ZEND_LONG == 4.  This one
assumes it.  long is 32 bits here and zend_long is 64, so __lres held
the product's low half, the delta test against __dres reported an
overflow that had not happened, and the result was a double.

So every integer multiplication whose product did not fit in 32 bits
silently returned a float: 2000000 * 2000000 was float(4.0E+12).  A
wrong answer in the commonest arithmetic there is, with no
diagnostic, reaching every php program run on this port.

zend_long __lres now.  The before/after -S diff is four lines: IMULQ
into a register that was then MOVQL'd to 32 bits and CVTSL2SD'd,
against IMULQ kept whole and CVTSQ2SD.  515 TUs still compile.

The test needed correcting too.  Its first version covered +, -, max,
strlen and str_repeat and not `*', so it caught this only sideways
through unary minus, and nothing in the failing output said the word
multiply.  Section 3b asks directly now and includes two cases that a
fix which simply never reports an overflow would fail: PHP_INT_MAX * 2
and PHP_INT_MIN * 2 must still be floats.  0 failures on the host.

Second round running in which the tree's most-cited invariant --
long is 32 bits on amd64 -- arrives inside upstream code that is
correct on every LP64 platform.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WGAwvvTwDg2yknFkmZ3qzs
@staalmannen
staalmannen merged commit aa848c3 into main Nov 5, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants