Skip to content

Commit 2393692

Browse files
committed
Merge branch 'PHP-7.4'
* PHP-7.4: Fix #79596: MySQL FLOAT truncates to int some locales
2 parents 3771d6f + 844a124 commit 2393692

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

ext/mysqlnd/mysql_float_to_double.h

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

3030
/*
3131
* Convert from a 4-byte float to a 8-byte decimal by first converting
32-
* the float to a string, and then the string to a double.
32+
* the float to a string (ignoring localization), and then the string to a double.
3333
* The decimals argument specifies the precision of the output. If decimals
3434
* is less than zero, then a gcvt(3) like logic is used with the significant
3535
* digits set to FLT_DIG i.e. 6.
@@ -40,7 +40,7 @@ static inline double mysql_float_to_double(float fp4, int decimals) {
4040
if (decimals < 0) {
4141
php_gcvt(fp4, FLT_DIG, '.', 'e', num_buf);
4242
} else {
43-
sprintf(num_buf, "%.*f", decimals, fp4);
43+
snprintf(num_buf, MAX_CHAR_BUF_LEN, "%.*F", decimals, fp4);
4444
}
4545

4646
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)