PATCH: "splice() offset past end of array" warning. (take 2)
authorJarkko Hietaniemi <jhi@iki.fi>
Sat, 2 Mar 2002 02:27:05 +0000 (02:27 +0000)
committerJarkko Hietaniemi <jhi@iki.fi>
Sat, 2 Mar 2002 02:27:05 +0000 (02:27 +0000)
From: Schuyler Erle <schuyler@oreilly.com>
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 <mjd@plover.com>
Date: Fri, 01 Mar 2002 17:19:49 -0500
Message-ID: <20020301221949.7610.qmail@plover.com>

p4raw-id: //depot/perl@14939

pod/perldiag.pod
pod/perlfunc.pod
pp.c
t/op/splice.t

index 9d6e07b4ab7a3a1e02636b162a876301513e36f4..7f1e77e9ca150ec494654a36e780e91ab5a96b74 100644 (file)
@@ -3311,6 +3311,14 @@ See L<perlfunc/sort>.
 (F) A sort comparison subroutine may not return a list value with more
 or less than one element.  See L<perlfunc/sort>.
 
+=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<perlfunc/splice>.
+
 =item Split loop
 
 (P) The split was looping infinitely.  (Obviously, a split shouldn't
index 777f20a0efdcb7962ac15e46c6388ebe69ef18ba..99caa8849b2d7aad7914efebbce187ea74c1c6e7 100644 (file)
@@ -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 f856ea66124a3e4241af5bd3cd9abab7028993f9..488c2e43f83d96dd93fafe7f63d8d0957fa37b95 100644 (file)
--- 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 */
index d1bfe999e5fb81c05ccb7d9865d386531721d265..6d9b71f06474a4451cc59e970d18c9d6aba48a76 100755 (executable)
@@ -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);