Subject: | [PATCH] Handle stack reallocation |
See the attached patch. You should be able to feed it straight to ‘git am’.
Subject: | patch.text |
From: Father Chrysostomos <sprout@cpan.org>
Handle stack reallocation in callbacks
ST expands to something referencing PL_stack_base. Since the order of
evalution of a=b is undefined in C, the value of PL_stack_base may be
read before sv_clone is called. sv_clone may reallocate the stack, so
the value gets written to freed memory and clone() returns its argu-
ment instead of the clone.
diff -Nurp Data-Clone-0.003-zBXsq6-orig/Data-Clone.xs Data-Clone-0.003-zBXsq6/Data-Clone.xs
--- Data-Clone-0.003-zBXsq6-orig/Data-Clone.xs 2011-01-15 05:58:49.000000000 -0800
+++ Data-Clone-0.003-zBXsq6/Data-Clone.xs 2014-01-29 21:26:23.000000000 -0800
@@ -392,7 +392,8 @@ void
clone(SV* sv)
CODE:
{
- ST(0) = sv_clone(sv);
+ sv = sv_clone(sv);
+ ST(0) = sv;
XSRETURN(1);
}
diff -Nurp Data-Clone-0.003-zBXsq6-orig/MANIFEST Data-Clone-0.003-zBXsq6/MANIFEST
--- Data-Clone-0.003-zBXsq6-orig/MANIFEST 2011-01-15 05:53:37.000000000 -0800
+++ Data-Clone-0.003-zBXsq6/MANIFEST 2014-01-29 21:25:25.000000000 -0800
@@ -28,6 +28,7 @@ t/03_scalar_ref.t
t/04_tree.t
t/05_super.t
t/06_tie.t
+t/07_stack.t
t/10_threads.t
t/11_leaktrace.t
xshelper.h
diff -Nurp Data-Clone-0.003-zBXsq6-orig/t/07_stack.t Data-Clone-0.003-zBXsq6/t/07_stack.t
--- Data-Clone-0.003-zBXsq6-orig/t/07_stack.t 1969-12-31 16:00:00.000000000 -0800
+++ Data-Clone-0.003-zBXsq6/t/07_stack.t 2014-01-29 21:25:09.000000000 -0800
@@ -0,0 +1,22 @@
+#!perl -w
+
+use strict;
+use warnings FATAL => 'all';
+
+use Test::More;
+
+use Data::Clone;
+
+{
+ package Bar;
+ sub clone {
+ () = (1)x100000; # extend the stack
+ return []
+ }
+}
+
+my $before = bless [], Bar::;
+my $after = clone($before);
+isn't $after, $before, 'stack reallocation during callback';
+
+done_testing;