From e9c3d8055323e1264dd95323732d5768bc8b13b2 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Thu, 14 May 2020 14:13:54 +0200 Subject: [PATCH 1/2] 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. --- ext/mysqlnd/mysql_float_to_double.h | 4 ++-- ext/pdo_mysql/tests/bug79596.phpt | 32 +++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) create mode 100644 ext/pdo_mysql/tests/bug79596.phpt diff --git a/ext/mysqlnd/mysql_float_to_double.h b/ext/mysqlnd/mysql_float_to_double.h index 7ac77d91271e4..9fc77655066cd 100644 --- a/ext/mysqlnd/mysql_float_to_double.h +++ b/ext/mysqlnd/mysql_float_to_double.h @@ -31,7 +31,7 @@ /* * Convert from a 4-byte float to a 8-byte decimal by first converting - * the float to a string, and then the string to a double. + * the float to a string (Iinoring localization), and then the string to a double. * The decimals argument specifies the precision of the output. If decimals * is less than zero, then a gcvt(3) like logic is used with the significant * digits set to FLT_DIG i.e. 6. @@ -42,7 +42,7 @@ static inline double mysql_float_to_double(float fp4, int decimals) { if (decimals < 0) { php_gcvt(fp4, FLT_DIG, '.', 'e', num_buf); } else { - php_sprintf(num_buf, "%.*f", decimals, fp4); + snprintf(num_buf, MAX_CHAR_BUF_LEN, "%.*F", decimals, fp4); } return zend_strtod(num_buf, NULL); diff --git a/ext/pdo_mysql/tests/bug79596.phpt b/ext/pdo_mysql/tests/bug79596.phpt new file mode 100644 index 0000000000000..95e6e8ba87e83 --- /dev/null +++ b/ext/pdo_mysql/tests/bug79596.phpt @@ -0,0 +1,32 @@ +--TEST-- +Bug #79596 (MySQL FLOAT truncates to int some locales) +--SKIPIF-- + +--FILE-- +setAttribute(\PDO::ATTR_EMULATE_PREPARES, false); +$pdo->query('CREATE TABLE bug79596 (broken FLOAT(2,1))'); +$pdo->query('INSERT INTO bug79596 VALUES(4.9)'); +var_dump($pdo->query('SELECT broken FROM bug79596')->fetchColumn(0)); +?> +--CLEAN-- +exec("DROP TABLE IF EXISTS bug79596"); +?> +--EXPECT-- +float(4,9) From c8fe5c98597ca02d6f33e31f4417ace3d60d07d3 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Thu, 14 May 2020 15:15:06 +0200 Subject: [PATCH 2/2] [ci skip] Fix typo --- ext/mysqlnd/mysql_float_to_double.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ext/mysqlnd/mysql_float_to_double.h b/ext/mysqlnd/mysql_float_to_double.h index 9fc77655066cd..744d56a5f3862 100644 --- a/ext/mysqlnd/mysql_float_to_double.h +++ b/ext/mysqlnd/mysql_float_to_double.h @@ -31,7 +31,7 @@ /* * Convert from a 4-byte float to a 8-byte decimal by first converting - * the float to a string (Iinoring localization), and then the string to a double. + * the float to a string (ignoring localization), and then the string to a double. * The decimals argument specifies the precision of the output. If decimals * is less than zero, then a gcvt(3) like logic is used with the significant * digits set to FLT_DIG i.e. 6.