summaryrefslogtreecommitdiff
path: root/ext
diff options
context:
space:
mode:
authorMáté Kocsis <kocsismate@woohoolabs.com>2020-02-29 22:47:04 +0100
committerMáté Kocsis <kocsismate@woohoolabs.com>2020-05-08 10:52:23 +0200
commit4a816584a4d483722485e5163396ea1bb2a6aee7 (patch)
treed85d2f5a93b66da2b0b611cdb7600ae2b684ad7c /ext
parent6e2cd97b4368e888193819dc0a6d1306b219ec21 (diff)
downloadphp-git-4a816584a4d483722485e5163396ea1bb2a6aee7.tar.gz
Make float to string casts locale-independent
From now on, float to string casting will always behave locale-independently. RFC: https://wiki.php.net/rfc/locale_independent_float_to_string Closes GH-5224 Co-authored-by: George Peter Banyard <girgias@php.net>
Diffstat (limited to 'ext')
-rw-r--r--ext/intl/tests/bug67052-win32.phpt2
-rw-r--r--ext/intl/tests/bug67052.phpt2
-rw-r--r--ext/json/tests/bug41403.phpt6
-rw-r--r--ext/pdo/pdo_stmt.c11
-rw-r--r--ext/pgsql/pgsql.c2
-rw-r--r--ext/soap/tests/bugs/bug39815.phpt2
-rw-r--r--ext/standard/tests/strings/locale_independent_float_to_string.phpt107
-rw-r--r--ext/standard/var.c4
8 files changed, 118 insertions, 18 deletions
diff --git a/ext/intl/tests/bug67052-win32.phpt b/ext/intl/tests/bug67052-win32.phpt
index 2c27624562..060a98b9fb 100644
--- a/ext/intl/tests/bug67052-win32.phpt
+++ b/ext/intl/tests/bug67052-win32.phpt
@@ -25,5 +25,5 @@ ut_run();
?>
--EXPECT--
-1234567,891
+1234567.891
de-de
diff --git a/ext/intl/tests/bug67052.phpt b/ext/intl/tests/bug67052.phpt
index 6cdfd9f5a9..56d825aa5d 100644
--- a/ext/intl/tests/bug67052.phpt
+++ b/ext/intl/tests/bug67052.phpt
@@ -30,5 +30,5 @@ ut_run();
?>
--EXPECT--
-1234567,891
+1234567.891
de_DE.UTF-8
diff --git a/ext/json/tests/bug41403.phpt b/ext/json/tests/bug41403.phpt
index 685c831838..91b4de7ead 100644
--- a/ext/json/tests/bug41403.phpt
+++ b/ext/json/tests/bug41403.phpt
@@ -23,15 +23,15 @@ echo "Done\n";
--EXPECT--
array(1) {
[0]=>
- float(2,1)
+ float(2.1)
}
array(1) {
[0]=>
- float(0,15)
+ float(0.15)
}
array(1) {
[0]=>
- float(123,13452345)
+ float(123.13452345)
}
array(2) {
[0]=>
diff --git a/ext/pdo/pdo_stmt.c b/ext/pdo/pdo_stmt.c
index 42cc447608..b095425caf 100644
--- a/ext/pdo/pdo_stmt.c
+++ b/ext/pdo/pdo_stmt.c
@@ -230,15 +230,8 @@ static int really_register_bound_param(struct pdo_bound_param_data *param, pdo_s
}
if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_STR && param->max_value_len <= 0 && !Z_ISNULL_P(parameter)) {
- if (Z_TYPE_P(parameter) == IS_DOUBLE) {
- char *p;
- int len = zend_spprintf_unchecked(&p, 0, "%.*H", (int) EG(precision), Z_DVAL_P(parameter));
- ZVAL_STRINGL(parameter, p, len);
- efree(p);
- } else {
- if (!try_convert_to_string(parameter)) {
- return 0;
- }
+ if (!try_convert_to_string(parameter)) {
+ return 0;
}
} else if (PDO_PARAM_TYPE(param->param_type) == PDO_PARAM_INT && (Z_TYPE_P(parameter) == IS_FALSE || Z_TYPE_P(parameter) == IS_TRUE)) {
convert_to_long(parameter);
diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c
index 9450bf0f61..3a4d6fd578 100644
--- a/ext/pgsql/pgsql.c
+++ b/ext/pgsql/pgsql.c
@@ -1987,7 +1987,7 @@ PHP_FUNCTION(pg_query_params)
zval tmp_val;
ZVAL_COPY(&tmp_val, tmp);
- convert_to_cstring(&tmp_val);
+ convert_to_string(&tmp_val);
if (Z_TYPE(tmp_val) != IS_STRING) {
php_error_docref(NULL, E_WARNING,"Error converting parameter");
zval_ptr_dtor(&tmp_val);
diff --git a/ext/soap/tests/bugs/bug39815.phpt b/ext/soap/tests/bugs/bug39815.phpt
index 57b429ec43..e8786ada5a 100644
--- a/ext/soap/tests/bugs/bug39815.phpt
+++ b/ext/soap/tests/bugs/bug39815.phpt
@@ -41,7 +41,7 @@ setlocale(LC_ALL,"en_US","en_US.ISO8859-1");
var_dump($x->test());
echo $x->__getLastResponse();
--EXPECT--
-float(123,456)
+float(123.456)
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns1="http://testuri.org" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"><SOAP-ENV:Body><ns1:testResponse><return xsi:type="xsd:float">123.456</return></ns1:testResponse></SOAP-ENV:Body></SOAP-ENV:Envelope>
float(123.456)
diff --git a/ext/standard/tests/strings/locale_independent_float_to_string.phpt b/ext/standard/tests/strings/locale_independent_float_to_string.phpt
new file mode 100644
index 0000000000..634d2ce0e6
--- /dev/null
+++ b/ext/standard/tests/strings/locale_independent_float_to_string.phpt
@@ -0,0 +1,107 @@
+--TEST--
+Test that floats are converted to string locale independently
+--SKIPIF--
+<?php
+
+if (!setlocale
+ (LC_ALL,
+ "german", "de", "de_DE", "de_DE.ISO8859-1", "de_DE.ISO_8859-1", "de_DE.UTF-8",
+ "french", "fr", "fr_FR", "fr_FR.ISO8859-1", "fr_FR.ISO_8859-1", "fr_FR.UTF-8",
+ )) {
+ die("skip - locale needed for this test is not supported on this platform");
+}
+
+if (!extension_loaded("json")) {
+ print "skip - test requires the json extension";
+}
+
+?>
+--FILE--
+<?php
+
+function print_float(float $f)
+{
+ echo "- casting:\n";
+ echo $f . "\n";
+ echo strval($f) . "\n";
+ $g = $f;
+ settype($g, "string");
+ echo $g . "\n";
+
+ echo "- *printf functions:\n";
+ printf("%.2f\n", $f);
+ printf("%.2F\n", $f);
+ echo sprintf("%.2f", $f) . "\n";
+ echo sprintf("%.2F", $f) . "\n";
+
+ echo "- export/import:\n";
+ echo var_export($f, true) . "\n";
+ echo serialize($f) . "\n";
+ echo json_encode($f) . "\n";
+
+ echo "- debugging:\n";
+ echo print_r($f, true) . "\n";
+ var_dump($f);
+ debug_zval_dump($f);
+
+ echo "- other:\n";
+ echo implode([$f]) . "\n";
+}
+
+setlocale(LC_ALL, "C");
+echo "C locale:\n";
+
+print_float(3.14);
+
+setlocale(
+ LC_ALL,
+ "german", "de", "de_DE", "de_DE.ISO8859-1", "de_DE.ISO_8859-1", "de_DE.UTF-8",
+ "french", "fr", "fr_FR", "fr_FR.ISO8859-1", "fr_FR.ISO_8859-1", "fr_FR.UTF-8",
+);
+echo "\nde_DE locale:\n";
+
+print_float(3.14);
+
+?>
+--EXPECT--
+C locale:
+- casting:
+3.14
+3.14
+3.14
+- *printf functions:
+3.14
+3.14
+3.14
+3.14
+- export/import:
+3.14
+d:3.14;
+3.14
+- debugging:
+3.14
+float(3.14)
+float(3.14)
+- other:
+3.14
+
+de_DE locale:
+- casting:
+3.14
+3.14
+3.14
+- *printf functions:
+3,14
+3.14
+3,14
+3.14
+- export/import:
+3.14
+d:3.14;
+3.14
+- debugging:
+3.14
+float(3.14)
+float(3.14)
+- other:
+3.14
diff --git a/ext/standard/var.c b/ext/standard/var.c
index 609acf449e..5b4fd8fe54 100644
--- a/ext/standard/var.c
+++ b/ext/standard/var.c
@@ -114,7 +114,7 @@ again:
php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
break;
case IS_DOUBLE:
- php_printf("%sfloat(%.*G)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
+ php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
break;
case IS_STRING:
php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));
@@ -295,7 +295,7 @@ again:
php_printf("%sint(" ZEND_LONG_FMT ")\n", COMMON, Z_LVAL_P(struc));
break;
case IS_DOUBLE:
- php_printf("%sfloat(%.*G)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
+ php_printf_unchecked("%sfloat(%.*H)\n", COMMON, (int) PG(serialize_precision), Z_DVAL_P(struc));
break;
case IS_STRING:
php_printf("%sstring(%zd) \"", COMMON, Z_STRLEN_P(struc));