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.
This commit is contained in:
Niels Dossche 2025-06-24 19:06:47 +02:00 committed by GitHub
parent 39cf27689b
commit 5ed8b2be55
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 18 additions and 0 deletions

2
NEWS
View file

@ -263,6 +263,8 @@ PHP NEWS
(nielsdos) (nielsdos)
. Fixed exit code handling of sendmail cmd and added warnings. . Fixed exit code handling of sendmail cmd and added warnings.
(Jesse Hathaway) (Jesse Hathaway)
. Fixed bug GH-18897 (printf: empty precision is interpreted as precision 6,
not as precision 0). (nielsdos)
- Streams: - Streams:
. Fixed bug GH-16889 (stream_select() timeout useless for pipes on Windows). . Fixed bug GH-16889 (stream_select() timeout useless for pipes on Windows).

View file

@ -116,6 +116,11 @@ PHP 8.5 UPGRADE NOTES
. SplFileObject::fwrite's parameter $length is now nullable. The default . SplFileObject::fwrite's parameter $length is now nullable. The default
value changed from 0 to null. value changed from 0 to null.
- Standard:
. Using a printf-family function with a formatter that did not specify the
precision previously incorrectly reset the precision instead of treating
it as a precision of 0. See GH-18897.
======================================== ========================================
2. New Features 2. New Features
======================================== ========================================

View file

@ -591,6 +591,7 @@ php_formatted_print(char *format, size_t format_len, zval *args, int argc, int n
expprec = 1; expprec = 1;
} else { } else {
precision = 0; precision = 0;
adjusting |= ADJ_PRECISION;
} }
} else { } else {
precision = 0; precision = 0;

View file

@ -0,0 +1,10 @@
--TEST--
GH-18897 (printf: empty precision is interpreted as precision 6, not as precision 0)
--FILE--
<?php
printf("%.f\n", 3.1415926535);
printf("%.0f\n", 3.1415926535);
?>
--EXPECT--
3
3