[DBD::Pg] Prevent core dump if the second argument to the quote() method is anything but a hashref. Also make the output a little smarter in that area. Add tests for same. (CPAN bug #101980)

dbdpg-commits-gENoVmVU/[email protected] Fri, 6 Feb 2015 20:49:31 +0000
Newsgroups gmane.comp.db.postgresql.dbdpg.cvs
Message-ID <[email protected]>
Committed by Greg Sabino Mullane <greg-O9fzpki4YnJWk0Htik3J/[email protected]>

Prevent core dump if the second argument to the quote()
method is anything but a hashref. Also make the output a little smarter in
that area. Add tests for same. (CPAN bug #101980)

---
 Changes        |  8 ++++++++
 Pg.xs          |  8 +++++++-
 t/03dbmethod.t | 20 +++++++++++++++++++-
 3 files changed, 34 insertions(+), 2 deletions(-)

diff --git a/Changes b/Changes
index 8953de7..0c635bf 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,14 @@
  'GSM' is Greg Sabino Mullane, greg-738XdyZ4GzZWk0Htik3J/[email protected]
 
 
+Version ??
+
+  - Prevent core dump if the second argument to the quote() method 
+    is anything but a hashref
+    [Greg Sabino Mullane]
+    (CPAN bug #101980)
+
+
 Version 3.5.0  Released January 6, 2015 (git commit bb13d3306fd1c73fac1c0c8a330c14e6b8443942)
 
   - Allow "placeholder escaping" by the use of a backslash directly before it, e.g.
diff --git a/Pg.xs b/Pg.xs
index 3c7d4e6..3dde3f9 100644
--- a/Pg.xs
+++ b/Pg.xs
@@ -252,6 +252,9 @@ quote(dbh, to_quote_sv, type_sv=Nullsv)
 				}
 				else {
 					SV **svp;
+					/* Currently the type argument must be a hashref, so throw an exception if not */
+					if (!SvROK(type_sv) || SvTYPE(SvRV(type_sv)) != SVt_PVHV)
+						croak("Second argument to quote must be a hashref");
 					if ((svp = hv_fetch((HV*)SvRV(type_sv),"pg_type", 7, 0)) != NULL) {
 						type_info = pg_type_data(SvIV(*svp));
 					}
@@ -263,7 +266,10 @@ quote(dbh, to_quote_sv, type_sv=Nullsv)
 					}
 				}
 				if (!type_info) {
-					warn("Unknown type %" IVdf ", defaulting to UNKNOWN",SvIV(type_sv));
+					if (NULL == type_info)
+						warn("No type given, defaulting to UNKNOWN");
+					else
+						warn("Unknown type %" IVdf ", defaulting to UNKNOWN", SvIV(type_sv));
 					type_info = pg_type_data(PG_UNKNOWN);
 				}
 			}
diff --git a/t/03dbmethod.t b/t/03dbmethod.t
index 6103d98..764256b 100644
--- a/t/03dbmethod.t
+++ b/t/03dbmethod.t
@@ -26,7 +26,7 @@ my $dbh = connect_database();
 if (! $dbh) {
 	plan skip_all => 'Connection to database failed, cannot continue testing';
 }
-plan tests => 559;
+plan tests => 562;
 
 isnt ($dbh, undef, 'Connect to database for database handle method testing');
 
@@ -1314,6 +1314,24 @@ is ($dbh->quote('foobar'), q{'foobar'}, $t);
 # Test various quote types
 #
 
+## Invalid type arguments
+$t='DB handle method "quote" throws exception on non-reference type argument';
+eval { $dbh->quote('abc', 'def'); };
+like ($@, qr{hashref}, $t);
+
+$t='DB handle method "quote" throws exception on arrayref type argument';
+eval { $dbh->quote('abc', ['arraytest']); };
+like ($@, qr{hashref}, $t);
+
+SKIP: {
+	eval { require Test::Warn; };
+	if ($@) {
+		skip ('Need Test::Warn for some tests', 1);
+	}
+
+	$t='DB handle method "quote" allows an empty hashref';
+	Test::Warn::warning_like ( sub { $dbh->quote('abc', {}); }, qr/UNKNOWN/, $t);
+}
 
 ## Points
 $t='DB handle method "quote" works with type PG_POINT';
-- 
1.8.4