Skip to content

Commit

Permalink
Check if handle is active when calling prepare
Browse files Browse the repository at this point in the history
  • Loading branch information
dveeden committed Nov 13, 2023
1 parent 24f68db commit 16be6e4
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,4 @@ MYMETA.*
*.old
INSTALL.html
/DBD-mysql-*
socket.o
5 changes: 5 additions & 0 deletions dbdimp.c
Original file line number Diff line number Diff line change
Expand Up @@ -2977,6 +2977,11 @@ dbd_st_prepare(
D_imp_xxh(sth);
D_imp_dbh_from_sth;

if (!DBIc_ACTIVE(imp_dbh)) {
do_error(sth, JW_ERR_NOT_ACTIVE, "Statement not active" ,NULL);
return FALSE;
}

if (DBIc_TRACE_LEVEL(imp_xxh) >= 2)
PerlIO_printf(DBIc_LOGPIO(imp_xxh),
"\t-> dbd_st_prepare MYSQL_VERSION_ID %d, SQL statement: %s\n",
Expand Down
29 changes: 29 additions & 0 deletions t/gh352.t
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use strict;
use warnings;

use Test::More;
use DBI;
use lib 't', '.';
require 'lib.pl';

use vars qw($test_dsn $test_user $test_password);

my $dbh;
eval {$dbh= DBI->connect($test_dsn, $test_user, $test_password,
{ RaiseError => 1, PrintError => 1, AutoCommit => 0 });};

if ($@) {
diag $@;
plan skip_all => "no database connection";
}
plan tests => 2;

# https://github.com/perl5-dbi/DBD-mysql/issues/352
# Calling prepare on a disconnected handle causes the call to mysql_real_escape_string to segfault

my $sth;
ok $dbh->disconnect;
my $result = eval {
$dbh->prepare('SELECT ?');
};
ok !$result

0 comments on commit 16be6e4

Please sign in to comment.