Skip to content

Commit 8b31069

Browse files
committed
Check if handle is active when calling prepare
1 parent 24f68db commit 8b31069

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ MYMETA.*
1313
*.old
1414
INSTALL.html
1515
/DBD-mysql-*
16+
socket.o

dbdimp.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2977,6 +2977,11 @@ dbd_st_prepare(
29772977
D_imp_xxh(sth);
29782978
D_imp_dbh_from_sth;
29792979

2980+
if (!DBIc_ACTIVE(imp_dbh)) {
2981+
do_error(sth, JW_ERR_NOT_ACTIVE, "Statement not active" ,NULL);
2982+
return FALSE;
2983+
}
2984+
29802985
if (DBIc_TRACE_LEVEL(imp_xxh) >= 2)
29812986
PerlIO_printf(DBIc_LOGPIO(imp_xxh),
29822987
"\t-> dbd_st_prepare MYSQL_VERSION_ID %d, SQL statement: %s\n",

t/gh352.t

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
use strict;
2+
use warnings;
3+
4+
use Test::More;
5+
use DBI;
6+
use lib 't', '.';
7+
require 'lib.pl';
8+
9+
use vars qw($test_dsn $test_user $test_password);
10+
11+
my $dbh;
12+
eval {$dbh= DBI->connect($test_dsn, $test_user, $test_password,
13+
{ RaiseError => 1, PrintError => 1, AutoCommit => 0 });};
14+
15+
if ($@) {
16+
diag $@;
17+
plan skip_all => "no database connection";
18+
}
19+
plan tests => 2;
20+
21+
# https://github.com/perl5-dbi/DBD-mysql/issues/352
22+
# Calling prepare on a disconnected handle causes the call to mysql_real_escape_string to segfault
23+
24+
my $sth;
25+
ok $dbh->disconnect;
26+
my $result = eval {
27+
$dbh->prepare('SELECT ?');
28+
};
29+
ok !$result

0 commit comments

Comments
 (0)