Skip to content

Commit

Permalink
Fix GH-13354: ext/pgsql: pg_execute, pg_send_query_params and_send_ex…
Browse files Browse the repository at this point in the history
…ecute null value by reference.

For these, when passing null values by refence, queries return erroneous values unlike
pg_query_params behaving as expected.

close GH-13355.
  • Loading branch information
georgebarbarosie authored and devnexen committed Feb 8, 2024
1 parent 7096eff commit 452e008
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 3 deletions.
4 changes: 4 additions & 0 deletions NEWS
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ PHP NEWS
. Fixed bug #75712 (getenv in php-fpm should not read $_ENV, $_SERVER).
(Jakub Zelenka)

- PGSQL:
. Fixed bug GH-13354 (pg_execute/pg_send_query_params/pg_send_execute
with null value passed by reference). (George Barbarosie)

- Standard:
. Fixed array key as hash to string (case insensitive) comparison typo
for the second operand buffer size (albeit unused for now). (A. Slepykh)
Expand Down
6 changes: 3 additions & 3 deletions ext/pgsql/pgsql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1288,7 +1288,7 @@ PHP_FUNCTION(pg_execute)
params = (char **)safe_emalloc(sizeof(char *), num_params, 0);

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) {

ZVAL_DEREF(tmp);
if (Z_TYPE_P(tmp) == IS_NULL) {
params[i] = NULL;
} else {
Expand Down Expand Up @@ -3653,7 +3653,7 @@ PHP_FUNCTION(pg_send_query_params)
params = (char **)safe_emalloc(sizeof(char *), num_params, 0);

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) {

ZVAL_DEREF(tmp);
if (Z_TYPE_P(tmp) == IS_NULL) {
params[i] = NULL;
} else {
Expand Down Expand Up @@ -3820,7 +3820,7 @@ PHP_FUNCTION(pg_send_execute)
params = (char **)safe_emalloc(sizeof(char *), num_params, 0);

ZEND_HASH_FOREACH_VAL(Z_ARRVAL_P(pv_param_arr), tmp) {

ZVAL_DEREF(tmp);
if (Z_TYPE_P(tmp) == IS_NULL) {
params[i] = NULL;
} else {
Expand Down
80 changes: 80 additions & 0 deletions ext/pgsql/tests/gh13354.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
--TEST--
GH-13354 (null-by-reference handling in pg_execute, pg_send_query_params, pg_send_execute)
--EXTENSIONS--
pgsql
--SKIPIF--
<?php include("skipif.inc"); ?>
--FILE--
<?php
include 'config.inc';

$db = pg_connect($conn_str);
$val = null;

$query = 'SELECT $1::text IS NULL;';
$params_null = [null];
$params_null_by_ref = [&$val];

pg_prepare($db, 'test', $query);


// method 1, pg_execute
$val = null;
$res = pg_execute($db, 'test', $params_null);
echo "pg_execute, null value: " . pg_fetch_result($res, 0, 0) . "\n";
pg_free_result($res);

$res = pg_execute($db, 'test', $params_null_by_ref);
echo "pg_execute, null value by reference: " . pg_fetch_result($res, 0, 0) . "\n";
pg_free_result($res);


// method 2, pg_query_params
$res = pg_query_params($db, $query, $params_null);
echo "pg_query_params, null value: " . pg_fetch_result($res, 0, 0) . "\n";
pg_free_result($res);

$res = pg_query_params($db, $query, $params_null_by_ref);
echo "pg_query_params, null value by reference: " . pg_fetch_result($res, 0, 0) . "\n";
pg_free_result($res);


// method 3, pg_send_query_params
$res = pg_send_query_params($db, $query, $params_null);
pg_consume_input($db);
$res = pg_get_result($db);
echo "pg_send_query_params, null value: " . pg_fetch_result($res, 0, 0) . "\n";
pg_free_result($res);

$res = pg_send_query_params($db, $query, $params_null_by_ref);
pg_consume_input($db);
$res = pg_get_result($db);
echo "pg_send_query_params, null value by reference: " . pg_fetch_result($res, 0, 0) . "\n";
pg_free_result($res);


// method 4, pg_send_prepare, pg_send_execute
pg_send_execute($db, 'test', $params_null);
pg_consume_input($db);
$res = pg_get_result($db);
echo "pg_send_execute, null value: " . pg_fetch_result($res, 0, 0) . "\n";
pg_free_result($res);

pg_send_execute($db, 'test', $params_null_by_ref);
pg_consume_input($db);
$res = pg_get_result($db);
echo "pg_send_execute, null value by reference: " . pg_fetch_result($res, 0, 0) . "\n";
pg_free_result($res);

pg_close($db);

?>
--EXPECT--
pg_execute, null value: t
pg_execute, null value by reference: t
pg_query_params, null value: t
pg_query_params, null value by reference: t
pg_send_query_params, null value: t
pg_send_query_params, null value by reference: t
pg_send_execute, null value: t
pg_send_execute, null value by reference: t

0 comments on commit 452e008

Please sign in to comment.