php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #26625 pg_convert sets NULL incorrectly for character data types
Submitted: 2003-12-15 03:32 UTC Modified: 2003-12-22 10:53 UTC
Votes:1
Avg. Score:2.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: gregkiyomi at digitaljunkies dot ca Assigned:
Status: Closed Package: PostgreSQL related
PHP Version: 5.0.0b2 (beta2) OS: linux
Private report: No CVE-ID:
 [2003-12-15 03:32 UTC] gregkiyomi at digitaljunkies dot ca
Description:
------------
When using pg_convert() with PGSQL_CONV_FORCE_NULL option set the resulting value for character data types (text,char,varchar) is set as 'NULL' instead of NULL.  This therefore inserts the string 'NULL' into the database instead of the desired NULL value.  This also obviously doesn't fail a NOT NULL check as it should.

The culprit is ext/pgsql/pgsql.c

*** pgsql.c~    Mon Dec 15 00:17:01 2003
--- pgsql.c     Mon Dec 15 01:28:56 2003
***************
*** 3785,3790 ****
--- 3785,3791 ----
if (Z_STRLEN_PP(val) == 0) {
    if (opt & PGSQL_CONV_FORCE_NULL) {
        ZVAL_STRING(new_val, "NULL", 1);
+       break;
    }
    else {
        ZVAL_STRING(new_val, empty_string, 1);

This prevents the NULL value from being passed through php_pgsql_add_quotes().

This is also present in php 4.3.3.

Reproduce code:
---------------
$con_str = "host=localhost dbname=database user=user password=pass";
$con = pg_connect($con_str);

$data = array("varchar_field1"=>"test",
              "varchar_field2"=>NULL,
              "varchar_field3"=>"");

$array = pg_convert($con,"test_table",$data,4);
echo("<pre>");
print_r($array);
echo("</pre>");


Expected result:
----------------
Array
(
    [varchar_field1] => 'test'
    [varchar_field2] => NULL
    [varchar_field3] => NULL
)


Actual result:
--------------
Array
(
    [varchar_field1] => 'test'
    [varchar_field2] => NULL
    [varchar_field3] => 'NULL'
)


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2003-12-15 19:57 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 [2003-12-21 15:15 UTC] gregkiyomi at digitaljunkies dot ca
The 'corrected' code now puts nothing in for empty strings if PGSQL_CONV_FORCE_NULL is *not* set.  It should put '' in.  This will break queries.

Expected result:
----------------
Array
(
    [varchar_field1] => 'test'
    [varchar_field2] => NULL
    [varchar_field3] => ''
)

Actual result:
--------------
Array
(
    [varchar_field1] => 'test'
    [varchar_field2] => NULL
    [varchar_field3] => 
)
 [2003-12-22 10:53 UTC] iliaa@php.net
This bug has been fixed in CVS.

In case this was a PHP problem, snapshots of the sources are packaged
every three hours; this change will be in the next snapshot. You can
grab the snapshot at http://snaps.php.net/.
 
In case this was a documentation problem, the fix will show up soon at
http://www.php.net/manual/.

In case this was a PHP.net website problem, the change will show
up on the PHP.net site and on the mirror sites in short time.
 
Thank you for the report, and for helping us make PHP better.


 
PHP Copyright © 2001-2014 The PHP Group
All rights reserved.
Last updated: Wed Apr 23 07:02:14 2014 UTC