php-src icon indicating copy to clipboard operation
php-src copied to clipboard

pg_insert fails if one of the values has been passed by reference

Open dansmill opened this issue 4 years ago • 1 comments

Description

In a test postgresql database create a table like such:

create table foo (  bar text );

The following code:

<?php

function fee(&$a){}

$a = ["bar"=>"testing"];

$db = pg_connect('dbname=test host=localhost');
pg_insert($db, 'foo', $a);  //No error

$res = pg_query($db, "select * from foo");
var_dump(pg_fetch_all($res));  // Output is a single tuple with the field foo is "testing"

fee($a["bar"]);

pg_insert($db, 'foo', $a); //Error: pg_insert(): Expects NULL, string, long or double value for PostgreSQL 'text' (bar)

I can reproduce on 7.1 and 8.0

There is no error on 5.6.

PHP Version

PHP 8.0.3

Operating System

Rocky Linux release 8.4

dansmill avatar Mar 26 '22 05:03 dansmill

I think we need to deref the value:

 ext/pgsql/pgsql.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/ext/pgsql/pgsql.c b/ext/pgsql/pgsql.c
index 70a78643fa..64bce7dda4 100644
--- a/ext/pgsql/pgsql.c
+++ b/ext/pgsql/pgsql.c
@@ -4595,6 +4595,7 @@ PHP_PGSQL_API int php_pgsql_convert(PGconn *pg_link, const char *table_name, con
 
 	ZEND_HASH_FOREACH_STR_KEY_VAL(Z_ARRVAL_P(values), field, val) {
 		skip_field = 0;
+		ZVAL_DEREF(val);
 		ZVAL_NULL(&new_val);
 
 		/* TODO: Check when meta data can be broken and see if can use assertions instead */

cmb69 avatar Mar 26 '22 12:03 cmb69