Skip to content

Commit 5ed8b2b

Browse files
authored
Fix GH-18897: printf: empty precision is interpreted as precision 6, not as precision 0 (#18912)
Like in other languages, and especially C where printf originates from, a missing precision should be treated as a 0 precision. Because the ADJ_PRECISION flag was not set, the double formatting code resetted the precision to the default float precision of 6.
1 parent 39cf276 commit 5ed8b2b

File tree

4 files changed

+18
-0
lines changed

4 files changed

+18
-0
lines changed

NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -263,6 +263,8 @@ PHP NEWS
263263
(nielsdos)
264264
. Fixed exit code handling of sendmail cmd and added warnings.
265265
(Jesse Hathaway)
266+
. Fixed bug GH-18897 (printf: empty precision is interpreted as precision 6,
267+
not as precision 0). (nielsdos)
266268

267269
- Streams:
268270
. Fixed bug GH-16889 (stream_select() timeout useless for pipes on Windows).

UPGRADING

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,11 @@ PHP 8.5 UPGRADE NOTES
116116
. SplFileObject::fwrite's parameter $length is now nullable. The default
117117
value changed from 0 to null.
118118

119+
- Standard:
120+
. Using a printf-family function with a formatter that did not specify the
121+
precision previously incorrectly reset the precision instead of treating
122+
it as a precision of 0. See GH-18897.
123+
119124
========================================
120125
2. New Features
121126
========================================

ext/standard/formatted_print.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
591591
expprec = 1;
592592
} else {
593593
precision = 0;
594+
adjusting |= ADJ_PRECISION;
594595
}
595596
} else {
596597
precision = 0;
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
--TEST--
2+
GH-18897 (printf: empty precision is interpreted as precision 6, not as precision 0)
3+
--FILE--
4+
<?php
5+
printf("%.f\n", 3.1415926535);
6+
printf("%.0f\n", 3.1415926535);
7+
?>
8+
--EXPECT--
9+
3
10+
3

0 commit comments

Comments
 (0)