Skip to content

Commit d1cd489

Browse files
committed
Fix #79596: MySQL FLOAT truncates to int some locales
We must not do locale aware float to string conversion here; instead we using our `snprintf()` implementation with the `F` specifier.
1 parent 5bdb4ab commit d1cd489

File tree

3 files changed

+35
-2
lines changed

3 files changed

+35
-2
lines changed

NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ PHP NEWS
66
. Fixed bug #79566 (Private SHM is not private on Windows). (cmb)
77
. Fixed bug #79489 (.user.ini does not inherit). (cmb)
88

9+
- MySQLnd:
10+
. Fixed bug #79596 (MySQL FLOAT truncates to int some locales). (cmb)
11+
912
- Opcache:
1013
. Fixed bug #79535 (PHP crashes with specific opcache.optimization_level).
1114
(Nikita)

ext/mysqlnd/mysql_float_to_double.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131

3232
/*
3333
* Convert from a 4-byte float to a 8-byte decimal by first converting
34-
* the float to a string, and then the string to a double.
34+
* the float to a string (ignoring localization), and then the string to a double.
3535
* The decimals argument specifies the precision of the output. If decimals
3636
* is less than zero, then a gcvt(3) like logic is used with the significant
3737
* digits set to FLT_DIG i.e. 6.
@@ -42,7 +42,7 @@ static inline double mysql_float_to_double(float fp4, int decimals) {
4242
if (decimals < 0) {
4343
php_gcvt(fp4, FLT_DIG, '.', 'e', num_buf);
4444
} else {
45-
php_sprintf(num_buf, "%.*f", decimals, fp4);
45+
snprintf(num_buf, MAX_CHAR_BUF_LEN, "%.*F", decimals, fp4);
4646
}
4747

4848
return zend_strtod(num_buf, NULL);

ext/pdo_mysql/tests/bug79596.phpt

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
--TEST--
2+
Bug #79596 (MySQL FLOAT truncates to int some locales)
3+
--SKIPIF--
4+
<?php
5+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'skipif.inc');
6+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
7+
MySQLPDOTest::skip();
8+
if (!setlocale(LC_ALL, 'de_DE', 'de-DE')) die('skip German locale not available');
9+
?>
10+
--FILE--
11+
<?php
12+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
13+
14+
setlocale(LC_ALL, 'de_DE', 'de-DE');
15+
16+
$pdo = MySQLPDOTest::factory();
17+
$pdo->setAttribute(\PDO::ATTR_EMULATE_PREPARES, false);
18+
$pdo->query('CREATE TABLE bug79596 (broken FLOAT(2,1))');
19+
$pdo->query('INSERT INTO bug79596 VALUES(4.9)');
20+
var_dump($pdo->query('SELECT broken FROM bug79596')->fetchColumn(0));
21+
?>
22+
--CLEAN--
23+
<?php
24+
require_once(__DIR__ . DIRECTORY_SEPARATOR . 'mysql_pdo_test.inc');
25+
26+
$pdo = MySQLPDOTest::factory();
27+
$pdo->exec("DROP TABLE IF EXISTS bug79596");
28+
?>
29+
--EXPECT--
30+
float(4,9)

0 commit comments

Comments
 (0)