From 81d4d11fa152eddc847ffa34f5ecb4532147373d Mon Sep 17 00:00:00 2001 From: Kenichi Ishigaki Date: Thu, 29 May 2014 16:05:45 +0900 Subject: [PATCH] fixed rt-96050; sqlite_db_filename returns an error (instead of segfault) if database connection is closed --- dbdimp.c | 7 +++++- ..._96050_db_filename_for_a_closed_database.t | 24 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 t/rt_96050_db_filename_for_a_closed_database.t diff --git a/dbdimp.c b/dbdimp.c index bb486bd..70e8242 100644 --- a/dbdimp.c +++ b/dbdimp.c @@ -444,6 +444,7 @@ sqlite_db_disconnect(SV *dbh, imp_dbh_t *imp_dbh) sqlite_error(dbh, rc, sqlite3_errmsg(imp_dbh->db)); } } + imp_dbh->db = NULL; av_undef(imp_dbh->functions); SvREFCNT_dec(imp_dbh->functions); @@ -467,7 +468,6 @@ sqlite_db_destroy(SV *dbh, imp_dbh_t *imp_dbh) if (DBIc_ACTIVE(imp_dbh)) { sqlite_db_disconnect(dbh, imp_dbh); } - imp_dbh->db = NULL; DBIc_IMPSET_off(imp_dbh); } @@ -1372,6 +1372,11 @@ sqlite_db_filename(pTHX_ SV *dbh) D_imp_dbh(dbh); const char *filename; + if (!imp_dbh->db) { + sqlite_error(dbh, -1, "Can't tell the filename of a closed database"); + return &PL_sv_undef; + } + croak_if_db_is_null(); filename = sqlite3_db_filename(imp_dbh->db, "main"); diff --git a/t/rt_96050_db_filename_for_a_closed_database.t b/t/rt_96050_db_filename_for_a_closed_database.t new file mode 100644 index 0000000..aaf6854 --- /dev/null +++ b/t/rt_96050_db_filename_for_a_closed_database.t @@ -0,0 +1,24 @@ +#!/usr/bin/env perl + +use strict; +BEGIN { + $| = 1; + $^W = 1; +} + +use t::lib::Test; +use Test::More tests => 4; +use Test::NoWarnings; + +my $dbh = connect_ok( RaiseError => 1, PrintError => 0 ); +{ + my $filename = eval { $dbh->sqlite_db_filename }; + ok !$@, "no filename (because it's in-memory); no error"; +} + +$dbh->disconnect; + +{ + my $filename = eval { $dbh->sqlite_db_filename }; + ok $@ && !$filename, "got an error; no filename; and no segfault"; +}