[php-src] PHP-8.6: Fix float literals losing precision in AST export (GH-25461)
| From: | Jorg Sowa | Date: | Wed, 25 Nov 2026 09:19:25 +0000 |
| Subject: | [php-src] PHP-8.6: Fix float literals losing precision in AST export (GH-25461) | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-144153@lists.php.net to get a copy of this message | ||
Author: Jorg Sowa (jorgsowa)
Date: 2026-11-25T10:15:53+01:00
Commit: https://github.com/php/php-src/commit/6debfb4e9ee6bf6e85fd3df87f552eba55e52dad
Raw diff: https://github.com/php/php-src/commit/6debfb4e9ee6bf6e85fd3df87f552eba55e52dad.diff
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
Changed paths:
A Zend/tests/assert/expect_float_precision.phpt
M NEWS
M Zend/zend_ast.c
Diff:
diff --git a/NEWS b/NEWS
index 3a8b3fcd7dd8..ea57424282cc 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 000000000000..b2a1cb6ae805
--- /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 e884ba897d86..a7763a262d5d 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));