Subject: | Unexpected SV break with sv_setsv() |
version.pm uses sv_setsv. It causes misterious behaviour when user uses version.pm with list::util or something.
Please look this code: http://ideone.com/9b0PAq
version->parse() breaks $a.
perlapi.pod says "The source SV may be destroyed if it is mortal, so don't use this function if the source SV needs to be reused." on sv_setsv docs. I suggest to use SvSetSV_nosteal() instead.
I attached the patch file.
Subject: | version.patch |
diff --git a/t/09_list_util.t b/t/09_list_util.t
new file mode 100644
index 0000000..4833849
--- /dev/null
+++ b/t/09_list_util.t
@@ -0,0 +1,24 @@
+use strict;
+use version;
+use Test::More tests => 2;
+use List::Util qw(reduce);
+
+{
+ my $fail = 0;
+ my $ret = reduce {
+ version->parse($a);
+ $fail++ unless defined $a;
+ 1
+ } "0.039", "0.035";
+ is $fail, 0;
+}
+
+{
+ my $fail = 0;
+ my $ret = reduce {
+ version->qv($a);
+ $fail++ unless defined $a;
+ 1
+ } "0.039", "0.035";
+ is $fail, 0;
+}
diff --git a/vutil/vutil.c b/vutil/vutil.c
index fb7cc12..e99655c 100644
--- a/vutil/vutil.c
+++ b/vutil/vutil.c
@@ -516,7 +516,7 @@ Perl_new_version(pTHX_ SV *ver)
}
else {
#endif
- sv_setsv(rv,ver); /* make a duplicate */
+ SvSetSV_nosteal(rv, ver); /* make a duplicate */
#ifdef SvVOK
}
}
diff --git a/vutil/vxs.xs b/vutil/vxs.xs
index e225ebb..6808100 100644
--- a/vutil/vxs.xs
+++ b/vutil/vxs.xs
@@ -190,7 +190,7 @@ PPCODE:
if ( !SvVOK(ver) ) { /* not already a v-string */
#endif
rv = sv_newmortal();
- sv_setsv(rv,ver); /* make a duplicate */
+ SvSetSV_nosteal(rv,ver); /* make a duplicate */
UPG_VERSION(rv, TRUE);
#ifdef SvVOK
}