Skip to content

Commit 6debfb4

Browse files
committed
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
1 parent d09481d commit 6debfb4

3 files changed

Lines changed: 27 additions & 1 deletion

File tree

‎NEWS‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ PHP NEWS
4444
. Fixed propagation of public(set) of readonly trait property. (ilutov)
4545
. Fixed bug GH-25313 (Assertion !EG(exception) in ZEND_FE_FREE when unpacking
4646
a generator into a by-ref parameter throws). (ndossche)
47+
. Fixed float literals losing precision in AST export. (Jorg Sowa)
4748

4849
- Curl:
4950
. Fixed curl_reset() retaining CURLOPT_PRIVATE after reset. (lacatoire)
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
--TEST--
2+
assert() message preserves float literals regardless of precision
3+
--INI--
4+
zend.assertions=1
5+
precision=14
6+
--FILE--
7+
<?php
8+
foreach ([
9+
fn() => assert(0.30000000000000004 === 0.3),
10+
fn() => assert(123456789012345678.0 === 0.0),
11+
fn() => assert(0.1 === 1.5e-300),
12+
fn() => assert(1.0 === 2.0),
13+
] as $fn) {
14+
try {
15+
$fn();
16+
} catch (AssertionError $e) {
17+
echo $e::class, ": ", $e->getMessage(), "\n";
18+
}
19+
}
20+
?>
21+
--EXPECT--
22+
AssertionError: assert(0.30000000000000004 === 0.3)
23+
AssertionError: assert(1.2345678901234568E+17 === 0.0)
24+
AssertionError: assert(0.1 === 1.5E-300)
25+
AssertionError: assert(1.0 === 2.0)

‎Zend/zend_ast.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2113,7 +2113,7 @@ static ZEND_COLD void zend_ast_export_zval(smart_str *str, const zval *zv, int p
21132113
break;
21142114
case IS_DOUBLE:
21152115
smart_str_append_double(
2116-
str, Z_DVAL_P(zv), (int) EG(precision), /* zero_fraction */ true);
2116+
str, Z_DVAL_P(zv), /* precision */ -1, /* zero_fraction */ true);
21172117
break;
21182118
case IS_STRING:
21192119
zend_ast_export_quoted_str(str, Z_STR_P(zv));

0 commit comments

Comments
 (0)