This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
sprintf(): add memory wrap tests
authorDavid Mitchell <davem@iabyn.com>
Tue, 9 May 2017 13:29:11 +0000 (14:29 +0100)
committerDavid Mitchell <davem@iabyn.com>
Wed, 7 Jun 2017 08:10:59 +0000 (09:10 +0100)
In various places Perl_sv_vcatpvfn_flags() does croak_memory_wrap()
(including a couple added by the previous commit to fix RT #131260),
but there don't appear to be any tests for them.

So this commit adds some tests.

t/op/sprintf2.t

index de6eb4b..c2308bb 100644 (file)
@@ -914,4 +914,30 @@ SKIP: {
     is(sprintf("%a", eval '0x1p-16494'), '0x1p-16494'); # underflow
 }
 
     is(sprintf("%a", eval '0x1p-16494'), '0x1p-16494'); # underflow
 }
 
+# check all calls to croak_memory_wrap()
+# RT #131260
+
+{
+    my $s = 8 * $Config{sizesize};
+    my $i = 1;
+    my $max;
+    while ($s--) { $max |= $i; $i <<= 1; }
+    my $max40 = $max - 40; # see the magic fudge factor in sv_vcatpvfn_flags()
+
+    my @tests = (
+                  # format, arg
+                  ["%.${max}a",        1.1 ],
+                  ["%.${max40}a",      1.1 ],
+                  ["%.${max}i",          1 ],
+                  ["%.${max}i",         -1 ],
+    );
+
+    for my $test (@tests) {
+        my ($fmt, $arg) = @$test;
+        eval { my $s = sprintf $fmt, $arg; };
+        like("$@", qr/panic: memory wrap/, qq{memory wrap: "$fmt", "$arg"});
+    }
+}
+
+
 done_testing();
 done_testing();