bucardo / dbdpg

Perl Postgres driver DBD::Pg aka dbdpg
48 stars 36 forks source link

memory management error using JSONB "key exists" operator (question mark) #33

Closed ryleyb closed 6 years ago

ryleyb commented 6 years ago

I have code that uses bind parameters and the ? operator for a JSONB field. I get a variety of different errors, each time I run my test script.

Test Script:

use strict;
use DBI;
use DBD::Pg;

my $connect_info = {
    dsn => 'dbi:Pg:dbname=xxx host=localhost port=5432',
    user => 'username',
    password => 'password'
};
my $qry = q{
select count(*)
 from tablename
 where
   col1 = $1 and
   jsonb_col \? 'key' and
   1 = $2
};

for (my $i=0;$i<50;$i++){
    print STDERR "iteration $i...";

    my $dbh = DBI->connect($connect_info->{dsn},$connect_info->{user},$connect_info->{password});
    my $sqlh = $dbh->prepare($qry) || warn "Can't prepare SQL statement: $DBI::errstr\n";

    $sqlh->bind_param('$1',10000);
    $sqlh->bind_param('$2',1);

    $sqlh->execute() || warn "Cant execute SQL statement: $DBI::errstr\n";

    $sqlh->finish();
    $dbh->disconnect();

    print STDERR "done \n";
}

Example output:

iteration 0...done
iteration 1...Statement has no placeholders to bind at test-dbd-pg.pl line 25.
*** Error in `perl': double free or corruption (fasttop): 0x0000000001cb2480 ***
Aborted

Another run:

iteration 0...done
iteration 1...DBD::Pg::st execute failed: ERROR:  invalid byte sequence for encoding "UTF8": 0xcf 0x03 at test-dbd-pg.pl line 28.
Cant execute SQL statement: ERROR:  invalid byte sequence for encoding "UTF8": 0xcf 0x03
done
... (repeated 48 more times)

If I take out the bind params or only have one of them, it works fine.

If I replace jsonb_col \? 'key' in the where clause with jsonb_typeof(jsonb_col->'key') is not null it works fine.

System info:

Other than those, I believe stock Debian Jessie. I will respond to any follow up questions promptly.

turnstep commented 6 years ago

Thank you for reporting this. Confirmed can duplicate, will try to figure out what is going on.

turnstep commented 6 years ago

This should be fixed in 22c134d850e857e1f9d0b67e2830a44c34ca623d

ryleyb commented 6 years ago

Looks good to me, ran my tests against it and it has stopped complaining!

Thanks for looking into this.