Commit 6debfb4e9ee for php
commit 6debfb4e9ee6bf6e85fd3df87f552eba55e52dad
Author: Jorg Sowa <jorg.sowa@gmail.com>
Date: Wed Nov 25 10:15:53 2026 +0100
Fix float literals losing precision in AST export (GH-25461)
zend_ast_export_zval() used the precision INI setting, so e.g. assert()
messages showed 0.30000000000000004 as 0.3. Use the shortest round-trip
representation instead.
Closes GH-25461
diff --git a/NEWS b/NEWS
index 3a8b3fcd7dd..ea57424282c 100644
--- a/NEWS
+++ b/NEWS
@@ -44,6 +44,7 @@ PHP NEWS
. Fixed propagation of public(set) of readonly trait property. (ilutov)
. Fixed bug GH-25313 (Assertion !EG(exception) in ZEND_FE_FREE when unpacking
a generator into a by-ref parameter throws). (ndossche)
+ . Fixed float literals losing precision in AST export. (Jorg Sowa)
- Curl:
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
diff --git a/Zend/tests/assert/expect_float_precision.phpt b/Zend/tests/assert/expect_float_precision.phpt
new file mode 100644
index 00000000000..b2a1cb6ae80
--- /dev/null
+++ b/Zend/tests/assert/expect_float_precision.phpt
@@ -0,0 +1,25 @@
+--TEST--
+assert() message preserves float literals regardless of precision
+--INI--
+zend.assertions=1
+precision=14
+--FILE--
+<?php
+foreach ([
+ fn() => assert(0.30000000000000004 === 0.3),
+ fn() => assert(123456789012345678.0 === 0.0),
+ fn() => assert(0.1 === 1.5e-300),
+ fn() => assert(1.0 === 2.0),
+] as $fn) {
+ try {
+ $fn();
+ } catch (AssertionError $e) {
+ echo $e::class, ": ", $e->getMessage(), "\n";
+ }
+}
+?>
+--EXPECT--
+AssertionError: assert(0.30000000000000004 === 0.3)
+AssertionError: assert(1.2345678901234568E+17 === 0.0)
+AssertionError: assert(0.1 === 1.5E-300)
+AssertionError: assert(1.0 === 2.0)
diff --git a/Zend/zend_ast.c b/Zend/zend_ast.c
index e884ba897d8..a7763a262d5 100644
--- a/Zend/zend_ast.c
+++ b/Zend/zend_ast.c
@@ -2113,7 +2113,7 @@ static ZEND_COLD void zend_ast_export_zval(smart_str *str, const zval *zv, int p
break;
case IS_DOUBLE:
smart_str_append_double(
- str, Z_DVAL_P(zv), (int) EG(precision), /* zero_fraction */ true);
+ str, Z_DVAL_P(zv), /* precision */ -1, /* zero_fraction */ true);
break;
case IS_STRING:
zend_ast_export_quoted_str(str, Z_STR_P(zv));