Skip to content

Commit 92965b0

Browse files
committed
Bug #46408: Fix double formatting for PostgreSQL bound parameters
1 parent 785e66a commit 92965b0

File tree

5 files changed

+49
-2
lines changed

5 files changed

+49
-2
lines changed

NEWS

+5-1
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ PHP NEWS
1919

2020
- cURL:
2121
. Added new functions curl_escape, curl_multi_setopt, curl_multi_strerror
22-
curl_pause, curl_reset, curl_share_close, curl_share_init,
22+
curl_pause, curl_reset, curl_share_close, curl_share_init,
2323
curl_share_setopt curl_strerror and curl_unescape. (Pierrick)
2424
. Addes new curl options CURLOPT_TELNETOPTIONS, CURLOPT_GSSAPI_DELEGATION,
2525
CURLOPT_ACCEPTTIMEOUT_MS, CURLOPT_SSL_OPTIONS, CURLOPT_TCP_KEEPALIVE,
2626
CURLOPT_TCP_KEEPIDLE and CURLOPT_TCP_KEEPINTVL. (Pierrick)
2727

28+
- pgsql:
29+
. Bug #46408: Locale number format settings can cause pg_query_params to
30+
break with numerics. (asmecher, Lars)
31+
2832
18 Dec 2012, PHP 5.5.0 Alpha 2
2933

3034
- General improvements:

Zend/zend_operators.c

+18
Original file line numberDiff line numberDiff line change
@@ -572,6 +572,24 @@ ZEND_API void convert_to_boolean(zval *op) /* {{{ */
572572
}
573573
/* }}} */
574574

575+
ZEND_API void _convert_to_cstring(zval *op ZEND_FILE_LINE_DC) /* {{{ */
576+
{
577+
double dval;
578+
switch (Z_TYPE_P(op)) {
579+
case IS_DOUBLE: {
580+
TSRMLS_FETCH();
581+
dval = Z_DVAL_P(op);
582+
Z_STRLEN_P(op) = zend_spprintf(&Z_STRVAL_P(op), 0, "%.*H", (int) EG(precision), dval);
583+
/* %H already handles removing trailing zeros from the fractional part, yay */
584+
break;
585+
}
586+
default:
587+
return _convert_to_string(op);
588+
}
589+
Z_TYPE_P(op) = IS_STRING;
590+
}
591+
/* }}} */
592+
575593
ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC) /* {{{ */
576594
{
577595
long lval;

Zend/zend_operators.h

+2
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ ZEND_API int increment_function(zval *op1);
301301
ZEND_API int decrement_function(zval *op2);
302302

303303
ZEND_API void convert_scalar_to_number(zval *op TSRMLS_DC);
304+
ZEND_API void _convert_to_cstring(zval *op ZEND_FILE_LINE_DC);
304305
ZEND_API void _convert_to_string(zval *op ZEND_FILE_LINE_DC);
305306
ZEND_API void convert_to_long(zval *op);
306307
ZEND_API void convert_to_double(zval *op);
@@ -314,6 +315,7 @@ ZEND_API void multi_convert_to_double_ex(int argc, ...);
314315
ZEND_API void multi_convert_to_string_ex(int argc, ...);
315316
ZEND_API int add_char_to_string(zval *result, const zval *op1, const zval *op2);
316317
ZEND_API int add_string_to_string(zval *result, const zval *op1, const zval *op2);
318+
#define convert_to_cstring(op) if ((op)->type != IS_STRING) { _convert_to_cstring((op) ZEND_FILE_LINE_CC); }
317319
#define convert_to_string(op) if ((op)->type != IS_STRING) { _convert_to_string((op) ZEND_FILE_LINE_CC); }
318320

319321
ZEND_API double zend_string_to_double(const char *number, zend_uint length);

ext/pgsql/pgsql.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -1736,7 +1736,7 @@ PHP_FUNCTION(pg_query_params)
17361736
} else {
17371737
zval tmp_val = **tmp;
17381738
zval_copy_ctor(&tmp_val);
1739-
convert_to_string(&tmp_val);
1739+
convert_to_cstring(&tmp_val);
17401740
if (Z_TYPE(tmp_val) != IS_STRING) {
17411741
php_error_docref(NULL TSRMLS_CC, E_WARNING,"Error converting parameter");
17421742
zval_dtor(&tmp_val);

ext/pgsql/tests/bug46408.phpt

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Bug #46408 (Locale number format settings can cause pg_query_params to break with numerics)
3+
--SKIPIF--
4+
<?php
5+
require_once('skipif.inc');
6+
?>
7+
--FILE--
8+
<?php
9+
10+
require_once('config.inc');
11+
12+
$dbh = pg_connect($conn_str);
13+
setlocale(LC_ALL, 'hr_HR.utf-8', 'hr_HR');
14+
echo 3.5.PHP_EOL;
15+
pg_query_params("SELECT $1::numeric", array(3.5));
16+
pg_close($dbh);
17+
18+
echo "Done".PHP_EOL;
19+
20+
?>
21+
--EXPECTF--
22+
3,5
23+
Done

0 commit comments

Comments
 (0)