From: Jarkko Hietaniemi Date: Sat, 18 May 2002 18:42:08 +0000 (+0000) Subject: Companion to #16601: cxinc would create uninitialized X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=4fc932073df150c47d1c1a91564ea6bfacbc9a85;p=p5sagit%2Fp5-mst-13.2.git Companion to #16601: cxinc would create uninitialized PERL_CONTEXTs. The bug was tickled by the test lib/Math/BigInt/t/upgrade.t, the new test of recurse.t added to check that I got the context stack extension right. Also rewrite recurse.t to use test.pl. p4raw-id: //depot/perl@16679 --- diff --git a/scope.c b/scope.c index 4ff903f..5ae9a31 100644 --- a/scope.c +++ b/scope.c @@ -80,8 +80,8 @@ Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems) si->si_cxmax = cxitems - 1; si->si_cxix = -1; si->si_type = PERLSI_UNDEF; - /* Needs to be Newz() because PUSHSUBST() in pp_subst() - * might otherwise read uninitialized heap. */ + /* Needs to be Newz() instead of New() because PUSHSUBST() + * in pp_subst() might otherwise read uninitialized heap. */ Newz(56, si->si_cxstack, cxitems, PERL_CONTEXT); return si; } @@ -89,8 +89,13 @@ Perl_new_stackinfo(pTHX_ I32 stitems, I32 cxitems) I32 Perl_cxinc(pTHX) { + IV old_max = cxstack_max; cxstack_max = GROW(cxstack_max); Renew(cxstack, cxstack_max + 1, PERL_CONTEXT); /* XXX should fix CXINC macro */ + /* Needs to Zero()ed because otherwise deep enough recursion + * (such as in lib/Math/BigInt/t/upgrade.t) will end up reading + * uninitialized heap. */ + Zero(cxstack + old_max + 1, cxstack_max - old_max, PERL_CONTEXT); return cxstack_ix + 1; } diff --git a/t/op/recurse.t b/t/op/recurse.t index dc823ed..a86744e 100755 --- a/t/op/recurse.t +++ b/t/op/recurse.t @@ -4,19 +4,26 @@ # test recursive functions. # -print "1..25\n"; +BEGIN { + chdir 't' if -d 't'; + @INC = qw(. ../lib); + require "test.pl"; + plan(tests => 26); +} + +use strict; -sub gcd ($$) { +sub gcd { return gcd($_[0] - $_[1], $_[1]) if ($_[0] > $_[1]); return gcd($_[0], $_[1] - $_[0]) if ($_[0] < $_[1]); $_[0]; } -sub factorial ($) { +sub factorial { $_[0] < 2 ? 1 : $_[0] * factorial($_[0] - 1); } -sub fibonacci ($) { +sub fibonacci { $_[0] < 2 ? 1 : fibonacci($_[0] - 2) + fibonacci($_[0] - 1); } @@ -26,7 +33,7 @@ sub fibonacci ($) { # For example ackermann(4,1) will take quite a long time. # It will simply eat away your memory. Trust me. -sub ackermann ($$) { +sub ackermann { return $_[1] + 1 if ($_[0] == 0); return ackermann($_[0] - 1, 1) if ($_[1] == 0); ackermann($_[0] - 1, ackermann($_[0], $_[1] - 1)); @@ -34,7 +41,7 @@ sub ackermann ($$) { # Highly recursive, highly boring. -sub takeuchi ($$$) { +sub takeuchi { $_[1] < $_[0] ? takeuchi(takeuchi($_[0] - 1, $_[1], $_[2]), takeuchi($_[1] - 1, $_[2], $_[0]), @@ -42,48 +49,30 @@ sub takeuchi ($$$) { : $_[2]; } -print 'not ' unless (($d = gcd(1147, 1271)) == 31); -print "ok 1\n"; -print "# gcd(1147, 1271) = $d\n"; - -print 'not ' unless (($d = gcd(1908, 2016)) == 36); -print "ok 2\n"; -print "# gcd(1908, 2016) = $d\n"; +is(gcd(1147, 1271), 31, "gcd(1147, 1271)"); -print 'not ' unless (($f = factorial(10)) == 3628800); -print "ok 3\n"; -print "# factorial(10) = $f\n"; +is(gcd(1908, 2016), 36, "gcd(1908, 2016)"); -print 'not ' unless (($f = factorial(factorial(3))) == 720); -print "ok 4\n"; -print "# factorial(factorial(3)) = $f\n"; +is(factorial(10), 3628800, "factorial(10)"); -print 'not ' unless (($f = fibonacci(10)) == 89); -print "ok 5\n"; -print "# fibonacci(10) = $f\n"; +is(factorial(factorial(3)), 720, "factorial(factorial(3))"); -print 'not ' unless (($f = fibonacci(fibonacci(7))) == 17711); -print "ok 6\n"; -print "# fibonacci(fibonacci(7)) = $f\n"; +is(fibonacci(10), 89, "fibonacci(10)"); -$i = 7; +is(fibonacci(fibonacci(7)), 17711, "fibonacci(fibonacci(7))"); -@ack = qw(1 2 3 4 2 3 4 5 3 5 7 9 5 13 29 61); +my @ack = qw(1 2 3 4 2 3 4 5 3 5 7 9 5 13 29 61); -for $x (0..3) { - for $y (0..3) { - $a = ackermann($x, $y); - print 'not ' unless ($a == shift(@ack)); - print "ok ", $i++, "\n"; - print "# ackermann($x, $y) = $a\n"; +for my $x (0..3) { + for my $y (0..3) { + my $a = ackermann($x, $y); + is($a, shift(@ack), "ackermann($x, y)"); } } -($x, $y, $z) = (18, 12, 6); +my ($x, $y, $z) = (18, 12, 6); -print 'not ' unless (($t = takeuchi($x, $y, $z)) == $z + 1); -print "ok ", $i++, "\n"; -print "# takeuchi($x, $y, $z) = $t\n"; +is(takeuchi($x, $y, $z), $z + 1, "takeuchi($x, $y, $z) == $z + 1"); { sub get_first1 { @@ -91,12 +80,12 @@ print "# takeuchi($x, $y, $z) = $t\n"; } sub get_list1 { - return [24] unless $_[0]; + return [curr_test] unless $_[0]; my $u = get_first1(0); [$u]; } my $x = get_first1(1); - print "ok $x\n"; + ok($x, "premature FREETMPS (change 5699)"); } { @@ -105,12 +94,24 @@ print "# takeuchi($x, $y, $z) = $t\n"; } sub get_list2 { - return [25] unless $_[0]; + return [curr_test] unless $_[0]; my $u = get_first2(0); return [$u]; } my $x = get_first2(1); - print "ok $x\n"; + ok($x, "premature FREETMPS (change 5699)"); +} + +{ + local $^W = 0; # We do not need recursion depth warning. + + sub sillysum { + return $_[0] + ($_[0] > 0 ? sillysum($_[0] - 1) : 0); + } + + is(sillysum(1000), 1000*1001/2, "recursive sum of 1..1000"); } -$i = 26; + + +