From: Jarkko Hietaniemi Date: Sat, 2 Mar 2002 02:27:05 +0000 (+0000) Subject: PATCH: "splice() offset past end of array" warning. (take 2) X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=8cbc2e3b35a2d1fc6313ced2b006c574c892f00d;p=p5sagit%2Fp5-mst-13.2.git PATCH: "splice() offset past end of array" warning. (take 2) From: Schuyler Erle Date: Fri, 01 Mar 2002 14:22:19 -0800 Message-ID: <3C7FFF1B.E74979B1@oreilly.com> Subject: Re: PATCH: "splice() offset past end of array" warning. From: Mark-Jason Dominus Date: Fri, 01 Mar 2002 17:19:49 -0500 Message-ID: <20020301221949.7610.qmail@plover.com> p4raw-id: //depot/perl@14939 --- diff --git a/pod/perldiag.pod b/pod/perldiag.pod index 9d6e07b..7f1e77e 100644 --- a/pod/perldiag.pod +++ b/pod/perldiag.pod @@ -3311,6 +3311,14 @@ See L. (F) A sort comparison subroutine may not return a list value with more or less than one element. See L. +=item splice() offset past end of array + +(W misc) You attempted to specify an offset that was past the end of +the array passed to splice(). Splicing will instead commence at the end +of the array, rather than past it. If this isn't what you want, try +explicitly pre-extending the array by assigning $#array = $offset. See +L. + =item Split loop (P) The split was looping infinitely. (Obviously, a split shouldn't diff --git a/pod/perlfunc.pod b/pod/perlfunc.pod index 777f20a..99caa88 100644 --- a/pod/perlfunc.pod +++ b/pod/perlfunc.pod @@ -4641,7 +4641,9 @@ removed. The array grows or shrinks as necessary. If OFFSET is negative then it starts that far from the end of the array. If LENGTH is omitted, removes everything from OFFSET onward. If LENGTH is negative, leaves that many elements off the end of the array. -If both OFFSET and LENGTH are omitted, removes everything. +If both OFFSET and LENGTH are omitted, removes everything. If OFFSET is +past the end of the array, perl issues a warning, and splices at the +end of the array. The following equivalences hold (assuming C<$[ == 0>): diff --git a/pp.c b/pp.c index f856ea6..488c2e4 100644 --- a/pp.c +++ b/pp.c @@ -3926,8 +3926,11 @@ PP(pp_splice) offset = 0; length = AvMAX(ary) + 1; } - if (offset > AvFILLp(ary) + 1) + if (offset > AvFILLp(ary) + 1) { + if (ckWARN(WARN_MISC)) + Perl_warner(aTHX_ WARN_MISC, "splice() offset past end of array" ); offset = AvFILLp(ary) + 1; + } after = AvFILLp(ary) + 1 - (offset + length); if (after < 0) { /* not that much array */ length += after; /* offset+length now in array */ diff --git a/t/op/splice.t b/t/op/splice.t index d1bfe99..6d9b71f 100755 --- a/t/op/splice.t +++ b/t/op/splice.t @@ -21,7 +21,7 @@ print "ok 4\n"; print "not " unless j(splice(@a,5,1,5)) eq "5" && j(@a) eq j(0..11); print "ok 5\n"; -print "not " unless j(splice(@a, 20, 0, 12, 13)) eq "" && j(@a) eq j(0..13); +print "not " unless j(splice(@a, @a, 0, 12, 13)) eq "" && j(@a) eq j(0..13); print "ok 6\n"; print "not " unless j(splice(@a, -@a, @a, 1, 2, 3)) eq j(0..13) && j(@a) eq j(1..3);