Permalink
Browse files

Don't overwrite $@ when XS code looks up per-package options

  • Loading branch information...
autarch committed Oct 26, 2012
1 parent 358f025 commit 1e5d97931eeb15c062e7353b129f86dde22a8a62
Showing with 34 additions and 21 deletions.
  1. +13 −21 lib/Params/Validate/XS.xs
  2. +21 −0 t/33-keep-errsv.t
View
@@ -738,40 +738,32 @@ convert_array2hash(AV* in, HV* options, HV* out) {
return 1;
}
-
/* get current Params::Validate options */
static HV*
get_options(HV* options) {
HV* OPTIONS;
HV* ret;
- SV** temp;
- char* pkg;
- SV* buffer;
- SV* caller;
+ HE *he;
+ HV *stash;
+ SV* pkg;
+ SV *pkg_options;
ret = (HV*) sv_2mortal((SV*) newHV());
- buffer = sv_2mortal(newSVpv("caller(0)", 0));
- SvTAINTED_off(buffer);
-
- caller = eval_pv(SvPV_nolen(buffer), 1);
- if (SvTYPE(caller) == SVt_NULL) {
- pkg = "main";
- }
- else {
- pkg = SvPV_nolen(caller);
- }
-
/* get package specific options */
+ stash = CopSTASH(PL_curcop);
+ pkg = sv_2mortal(newSVpv(HvNAME(stash), 0));
+
OPTIONS = get_hv("Params::Validate::OPTIONS", 1);
- if ((temp = hv_fetch(OPTIONS, pkg, strlen(pkg), 0))) {
- SvGETMAGIC(*temp);
- if (SvROK(*temp) && SvTYPE(SvRV(*temp)) == SVt_PVHV) {
+ if ((he = hv_fetch_ent(OPTIONS, pkg, 0, 0))) {
+ pkg_options = HeVAL(he);
+ SvGETMAGIC(pkg_options);
+ if (SvROK(pkg_options) && SvTYPE(SvRV(pkg_options)) == SVt_PVHV) {
if (options) {
- merge_hashes((HV*) SvRV(*temp), ret);
+ merge_hashes((HV*) SvRV(pkg_options), ret);
}
else {
- return (HV*) SvRV(*temp);
+ return (HV*) SvRV(pkg_options);
}
}
}
View
@@ -0,0 +1,21 @@
+use strict;
+use warnings;
+
+use Params::Validate qw( validate SCALAR );
+
+use Test::More;
+
+$@ = 'foo';
+v( bar => "doz" );
+
+is(
+ $@,
+ 'foo',
+ 'calling validate() does not clobber'
+);
+
+done_testing();
+
+sub v {
+ validate( @_, { bar => { type => SCALAR } } );
+}

0 comments on commit 1e5d979

Please sign in to comment.