Bug #78265 [NEW]: printf does not properly format decimals when using '%.8F'
| From: | mmucklo at gmail dot com | Date: | Tue, 09 Jul 2019 06:43:17 +0000 |
| Subject: | Bug #78265 [NEW]: printf does not properly format decimals when using '%.8F' | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-221665@lists.php.net to get a copy of this message | ||
From: mmucklo at gmail dot com
Operating system: Linux / any
PHP version: 7.3.7
Package: I18N and L10N related
Bug Type: Bug
Bug description:printf does not properly format decimals when using '%.8F'
Description:
------------
printf does not properly format uppercase floats.
This is in the master branch and goes as far back as 5.6.40, but I
didn't trace back any further.
The problem code is here:
https://github.com/php/php-src/blob/master/main/spprintf.c#L639
It only pays attention to a lower case 'f'
WARNING - FIXING this could cause unsuspected results as this bug has
been present as far back as 5.6.40 at least (the furthest back I
looked).
Specifically the code in microtime calls into the printf function
(eventually), and this will change the output of microtime() to be
locale-specific.
https://github.com/php/php-src/blob/master/ext/standard/microtime.c#L81
This is not necessarily a bad thing, but there are perhaps libraries
that expect the buggy behavior at this point (e.g. one that I presently
maintain:
https://github.com/mmucklo/DtcQueueBundle/blob/5.0.0/Util/Util.php#L104-L105)
I'm not suggesting that it should not get fixed, but just to be cautious
maybe where in the release cycle such a patch should go.
As a reference (couldn't instantly find a C version, but this CPP
version seems accurate):
http://www.cplusplus.com/reference/cstdio/printf/
Also this Stackoverflow seems to clarify things. Pay particular
attention to the second answer (about nan and inf capitalization), as I
think PHP may do that wrong as well, although I didn't test it, but just
looking at the code in spprintf.c, I don't see any specific logic to
handle that case.
https://stackoverflow.com/questions/34706228/difference-between-upper-and-lower-case-double-float-type-specifiers-in-c
Test script:
---------------
<?php
// NOTE your system needs to have this locale loaded first
// On ubuntu 19 which I'm using (via docker) do the following
// from the shell prompt (if you are not root, become root, or prefix
with sudo):
//
// locale-gen de_DE
// locale-gen de_DE.UTF-8
// dpkg-reconfigure locales
//
// # you may also need the locales package installed (apt install
locales).
//
$loc_de = setlocale(LC_ALL, 'de_DE.UTF-8', 'de_DE', 'de',
'ge');
printf("%0.8f\n", 0.12345);
printf("%0.8F\n", 0.12335);
// One should show the comma, the other won't
/**
Here is some C code to reproduce the expected result
#include <stdio.h>
#include <locale.h>
int main(int argc, char**argv) {
setlocale(LC_ALL, "de_DE.UTF-8");
printf("%.8f\n", 0.12345);
printf("%.8F\n", 0.12335);
return 0;
}
*/
Expected result:
----------------
0,12345000
0,12335000
Actual result:
--------------
0,12345000
0.12335000
--
Edit bug report at https://bugs.php.net/bug.php?id=78265&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=78265&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=78265&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=78265&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=78265&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78265&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78265&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78265&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78265&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78265&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78265&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78265&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78265&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78265&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=78265&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=78265&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78265&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78265&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78265&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78265&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78265&r=mysqlcfg