Skip to content

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

Closed

Description

@dansmill

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

Activity

  1. cmb69 commented on Mar 26, 2022

    @cmb69
    Member

    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 */
  2. deleted a comment from Knele83 on Mar 27, 2022
  3. added a commit that references this issue on Mar 28, 2022
    74caae4
  4. linked a pull request that will close this issueFix GH-8253: pg_insert() fails for references #8262on Mar 28, 2022
  5. self-assigned this
    on Mar 28, 2022
  6. added 2 commits that reference this issue on Mar 29, 2022
    7e8dcda
    c9c5ee3
  7. added a commit that references this issue on Apr 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions