From: Abigail Date: Tue, 6 May 2008 14:57:36 +0000 (+0200) Subject: Add index() tests for embedded nulls X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=10489e41959ba56eedd511a8c320dfec04d8f588;p=p5sagit%2Fp5-mst-13.2.git Add index() tests for embedded nulls Subject: Re: [perl #53746] bug with index() matching beyond end of string when \0 bytes (00000000) are involved Message-Id: <20080506125736.GC17310@abigail.be> p4raw-id: //depot/perl@33951 --- diff --git a/t/op/index.t b/t/op/index.t index 38da96c..896cd12 100755 --- a/t/op/index.t +++ b/t/op/index.t @@ -7,7 +7,7 @@ BEGIN { } use strict; -plan( tests => 69 ); +plan( tests => 111 ); run_tests() unless caller; @@ -160,4 +160,43 @@ SKIP: { is(index($t, 'xyz'), 4, "0xfffffffd and utf8cache"); } + +# Tests for NUL characters. +{ + my @tests = ( + ["", -1, -1, -1], + ["foo", -1, -1, -1], + ["\0", 0, -1, -1], + ["\0\0", 0, 0, -1], + ["\0\0\0", 0, 0, 0], + ["foo\0", 3, -1, -1], + ["foo\0foo\0\0", 3, 7, -1], + ); + foreach my $l (1 .. 3) { + my $q = "\0" x $l; + my $i = 0; + foreach my $test (@tests) { + $i ++; + my $str = $$test [0]; + my $res = $$test [$l]; + + { + local $::TODO = ($l == 3 && $i == 7 ? "Bug #53746" : ""); + is (index ($str, $q), $res, "Find NUL character(s)"); + } + + # + # Bug #53746 shows a difference between variables and literals, + # so test literals as well. + # + my $test_str = qq {is (index ("$str", "$q"), $res, } . + qq {"Find NUL character(s)")}; + $test_str =~ s/\0/\\0/g; + + eval $test_str; + die $@ if $@; + } + } +} + }