Skip to content

Commit 84a1c0e

Browse files
committed
pgsql ext: pg_query_params/pg_send_query_params false constant handling proposal.
making a new subset of zend_get_string calls and concealed for the pgsql module for now.
1 parent 64fde17 commit 84a1c0e

File tree

4 files changed

+79
-6
lines changed

4 files changed

+79
-6
lines changed

Zend/zend_operators.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -882,14 +882,14 @@ ZEND_API double ZEND_FASTCALL zval_get_double_func(zval *op) /* {{{ */
882882
}
883883
/* }}} */
884884

885-
static zend_always_inline zend_string* __zval_get_string_func(zval *op, zend_bool try) /* {{{ */
885+
static zend_always_inline zend_string* __zval_get_string_func(zval *op, zend_bool try, zend_bool noempbool) /* {{{ */
886886
{
887887
try_again:
888888
switch (Z_TYPE_P(op)) {
889889
case IS_UNDEF:
890890
case IS_NULL:
891891
case IS_FALSE:
892-
return ZSTR_EMPTY_ALLOC();
892+
return !noempbool ? ZSTR_EMPTY_ALLOC() : ZSTR_CHAR('0');
893893
case IS_TRUE:
894894
return ZSTR_CHAR('1');
895895
case IS_RESOURCE: {
@@ -928,13 +928,25 @@ static zend_always_inline zend_string* __zval_get_string_func(zval *op, zend_boo
928928

929929
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op) /* {{{ */
930930
{
931-
return __zval_get_string_func(op, 0);
931+
return __zval_get_string_func(op, 0, 0);
932932
}
933933
/* }}} */
934934

935935
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op) /* {{{ */
936936
{
937-
return __zval_get_string_func(op, 1);
937+
return __zval_get_string_func(op, 1, 0);
938+
}
939+
/* }}} */
940+
941+
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_noempbool_func(zval *op) /* {{{ */
942+
{
943+
return __zval_get_string_func(op, 0, 1);
944+
}
945+
/* }}} */
946+
947+
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_noempbool_func(zval *op) /* {{{ */
948+
{
949+
return __zval_get_string_func(op, 1, 1);
938950
}
939951
/* }}} */
940952

Zend/zend_operators.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,8 @@ ZEND_API zend_long ZEND_FASTCALL zval_get_long_func(zval *op);
272272
ZEND_API double ZEND_FASTCALL zval_get_double_func(zval *op);
273273
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_func(zval *op);
274274
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_func(zval *op);
275+
ZEND_API zend_string* ZEND_FASTCALL zval_get_string_noempbool_func(zval *op);
276+
ZEND_API zend_string* ZEND_FASTCALL zval_try_get_string_noempbool_func(zval *op);
275277

276278
static zend_always_inline zend_long zval_get_long(zval *op) {
277279
return EXPECTED(Z_TYPE_P(op) == IS_LONG) ? Z_LVAL_P(op) : zval_get_long_func(op);
@@ -282,6 +284,9 @@ static zend_always_inline double zval_get_double(zval *op) {
282284
static zend_always_inline zend_string *zval_get_string(zval *op) {
283285
return EXPECTED(Z_TYPE_P(op) == IS_STRING) ? zend_string_copy(Z_STR_P(op)) : zval_get_string_func(op);
284286
}
287+
static zend_always_inline zend_string *zval_get_string_noempbool(zval *op) {
288+
return EXPECTED(Z_TYPE_P(op) == IS_STRING) ? zend_string_copy(Z_STR_P(op)) : zval_get_string_noempbool_func(op);
289+
}
285290

286291
static zend_always_inline zend_string *zval_get_tmp_string(zval *op, zend_string **tmp) {
287292
if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
@@ -291,6 +296,14 @@ static zend_always_inline zend_string *zval_get_tmp_string(zval *op, zend_string
291296
return *tmp = zval_get_string_func(op);
292297
}
293298
}
299+
static zend_always_inline zend_string *zval_get_tmp_string_noempbool(zval *op, zend_string **tmp) {
300+
if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
301+
*tmp = NULL;
302+
return Z_STR_P(op);
303+
} else {
304+
return *tmp = zval_get_string_noempbool_func(op);
305+
}
306+
}
294307
static zend_always_inline void zend_tmp_string_release(zend_string *tmp) {
295308
if (UNEXPECTED(tmp)) {
296309
zend_string_release_ex(tmp, 0);
@@ -307,6 +320,16 @@ static zend_always_inline zend_string *zval_try_get_string(zval *op) {
307320
return zval_try_get_string_func(op);
308321
}
309322
}
323+
/* Like zval_get_string, but returns NULL if the conversion fails with an exception. */
324+
static zend_always_inline zend_string *zval_try_get_string_noempbool(zval *op) {
325+
if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
326+
zend_string *ret = zend_string_copy(Z_STR_P(op));
327+
ZEND_ASSUME(ret != NULL);
328+
return ret;
329+
} else {
330+
return zval_try_get_string_noempbool_func(op);
331+
}
332+
}
310333

311334
/* Like zval_get_tmp_string, but returns NULL if the conversion fails with an exception. */
312335
static zend_always_inline zend_string *zval_try_get_tmp_string(zval *op, zend_string **tmp) {
@@ -319,6 +342,17 @@ static zend_always_inline zend_string *zval_try_get_tmp_string(zval *op, zend_st
319342
return *tmp = zval_try_get_string_func(op);
320343
}
321344
}
345+
/* Like zval_get_tmp_string, but returns NULL if the conversion fails with an exception. */
346+
static zend_always_inline zend_string *zval_try_get_tmp_string_noempbool(zval *op, zend_string **tmp) {
347+
if (EXPECTED(Z_TYPE_P(op) == IS_STRING)) {
348+
zend_string *ret = Z_STR_P(op);
349+
*tmp = NULL;
350+
ZEND_ASSUME(ret != NULL);
351+
return ret;
352+
} else {
353+
return *tmp = zval_try_get_string_noempbool_func(op);
354+
}
355+
}
322356

323357
/* Like convert_to_string(), but returns whether the conversion succeeded and does not modify the
324358
* zval in-place if it fails. */

ext/pgsql/pgsql.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ PHP_FUNCTION(pg_query_params)
11201120
if (Z_TYPE_P(tmp) == IS_NULL) {
11211121
params[i] = NULL;
11221122
} else {
1123-
zend_string *param_str = zval_try_get_string(tmp);
1123+
zend_string *param_str = zval_try_get_string_noempbool(tmp);
11241124
if (!param_str) {
11251125
_php_pgsql_free_params(params, num_params);
11261126
RETURN_THROWS();
@@ -3755,7 +3755,7 @@ PHP_FUNCTION(pg_send_query_params)
37553755
params[i] = NULL;
37563756
} else {
37573757
zend_string *tmp_str;
3758-
zend_string *str = zval_get_tmp_string(tmp, &tmp_str);
3758+
zend_string *str = zval_get_tmp_string_noempbool(tmp, &tmp_str);
37593759

37603760
params[i] = estrndup(ZSTR_VAL(str), ZSTR_LEN(str));
37613761
zend_tmp_string_release(tmp_str);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
--TEST--
2+
PostgreSQL prepared queries with bool constants
3+
--SKIPIF--
4+
<?php
5+
include("skipif.inc");
6+
if (!function_exists('pg_prepare')) die('skip function pg_prepare() does not exist');
7+
?>
8+
--FILE--
9+
<?php
10+
11+
include('config.inc');
12+
13+
$db = pg_connect($conn_str);
14+
15+
$version = pg_version($db);
16+
if ($version['protocol'] >= 3) {
17+
$result = pg_query_params($db, "SELECT * FROM ".$table_name." WHERE;", array(true));
18+
// bug occurs with false as it turns out as empty.
19+
$result = pg_query_params($db, "SELECT * FROM ".$table_name." WHERE;", array(false));
20+
pg_free_result($result);
21+
}
22+
pg_close($db);
23+
24+
echo "OK";
25+
?>
26+
--EXPECT--
27+
OK

0 commit comments

Comments
 (0)