Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't warn for splice(@a,MAX_LEN) #10742

Closed
p5pRT opened this issue Oct 20, 2010 · 12 comments
Closed

Don't warn for splice(@a,MAX_LEN) #10742

p5pRT opened this issue Oct 20, 2010 · 12 comments

Comments

@p5pRT
Copy link

p5pRT commented Oct 20, 2010

Migrated from rt.perl.org#78462 (status was 'resolved')

Searchable as RT78462$

@p5pRT
Copy link
Author

p5pRT commented Oct 20, 2010

From @ikegami

Hi,

perl -we"my @​a; splice(@​a,100)"
splice() offset past end of array at -e line 1.

The intent of splice(@​a,MAX_LEN) is quite clearly to truncate the array if
it's too large. There's no reason to warn if the array is currently smaller
than the max length.

Patch coming in seconds.

Thanks,
Eric

@p5pRT
Copy link
Author

p5pRT commented Oct 20, 2010

From @ikegami

Patch attached.

@p5pRT
Copy link
Author

p5pRT commented Oct 20, 2010

From @ikegami

0001-Don-t-warn-for-splice-a-MAX_LEN-RT-78462.patch
From b82d83cdc63017718313fbbc5c692cd9e7b48554 Mon Sep 17 00:00:00 2001
From: Eric Brine <ikegami@adaelis.com>
Date: Tue, 19 Oct 2010 18:34:17 -0700
Subject: [PATCH] Don't warn for splice(@a,MAX_LEN) [RT#78462]

The intent of splice(@a,MAX_LEN) is quite clearly to truncate
the array if it's too large. There's no reason to warn if the
array is currently smaller than the max length.
---
 pod/perldelta.pod |    5 +++++
 pp.c              |    3 ++-
 t/lib/warnings/pp |   13 +++++++++++++
 3 files changed, 20 insertions(+), 1 deletions(-)

diff --git a/pod/perldelta.pod b/pod/perldelta.pod
index 3715fe4..fbe5eaa 100644
--- a/pod/perldelta.pod
+++ b/pod/perldelta.pod
@@ -124,6 +124,11 @@ You can now use the C<keys>, C<values>, C<each> builtin functions on arrays
 This is actually a change introduced in perl 5.12.0, but it was missed from
 that release's perldelta.
 
+=head2 C<splice()> doesn't warn when truncating
+
+You can now limit the size of an array using C<splice(@a,MAX_LEN)> without
+worrying about warnings.
+
 =head1 Security
 
 XXX Any security-related notices go here.  In particular, any security
diff --git a/pp.c b/pp.c
index b777f39..ad0611f 100644
--- a/pp.c
+++ b/pp.c
@@ -5198,7 +5198,8 @@ PP(pp_splice)
 	length = AvMAX(ary) + 1;
     }
     if (offset > AvFILLp(ary) + 1) {
-	Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
+	if (SP - MARK)
+	    Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
 	offset = AvFILLp(ary) + 1;
     }
     after = AvFILLp(ary) + 1 - (offset + length);
diff --git a/t/lib/warnings/pp b/t/lib/warnings/pp
index d158144..838274e 100644
--- a/t/lib/warnings/pp
+++ b/t/lib/warnings/pp
@@ -46,6 +46,19 @@ EXPECT
 Attempt to use reference as lvalue in substr at - line 5.
 ########
 # pp.c
+use warnings 'misc' ;
+@a = qw( a b c );
+splice(@a, 4, 0, 'e') ;
+use warnings 'misc' ;
+@a = qw( a b c );
+splice(@a, 4) ;
+no warnings 'misc' ;
+@a = qw( a b c );
+splice(@a, 4, 0, 'e') ;
+EXPECT
+splice() offset past end of array at - line 4.
+########
+# pp.c
 use warnings 'uninitialized' ;
 *x = *{ undef() };
 no warnings 'uninitialized' ;
-- 
1.7.1.1

@p5pRT
Copy link
Author

p5pRT commented Oct 20, 2010

@ikegami - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Oct 23, 2010

From @rgarcia

On 20 October 2010 04​:50, Eric Brine via RT <perlbug-followup@​perl.org> wrote​:

Patch attached.

I notice that this patch also removes the warning for C<splice @​array,
OFFSET, LENGTH>, which probably was not the intent ?

@p5pRT
Copy link
Author

p5pRT commented Oct 24, 2010

From @ikegami

On Sat, Oct 23, 2010 at 4​:47 PM, Rafael Garcia-Suarez <rgs@​consttype.org>wrote​:

On 20 October 2010 04​:50, Eric Brine via RT <perlbug-followup@​perl.org>
wrote​:

Patch attached.

I notice that this patch also removes the warning for C<splice @​array,
OFFSET, LENGTH>, which probably was not the intent ?

I knew I was missing something. I'll update the patch.

@p5pRT
Copy link
Author

p5pRT commented Nov 14, 2010

From @cpansprout

RT seemed to be down for a while and missed the latest patch. It is at​:

nntp​://nntp.perl.org/AANLkTikLovBP1CsJ5t6tEZ8hNCpH+_HnPmprTrURgNTf@​mail.gmail.com

@p5pRT
Copy link
Author

p5pRT commented Nov 14, 2010

From @cpansprout

On Nov 8, 2010, at 11​:08 AM, Eric Brine wrote​:

On Mon, Nov 8, 2010 at 1​:33 AM, Father Chrysostomos <sprout@​cpan.org> wrote​:
Eric Brine wrote​:

On Sun, Oct 24, 2010 at 3​:00 PM, Eric Brine <ikegami@​adaelis.com> wrote​:

I knew I was missing something. I'll update the patch.

Attached

This causes test failures in ../cpan/DB_File/t/db-recno.t. Do the
tests need to be updated?

Yes, but not just the tests. DB_File seeks to duplicate core splice's behaviour, so it will need to change to match. I'll have to write a patch to submit upstream. I'll notify this ticket when I do.

I’ve just had a look. The tests are checking that the number of warnings is the same for splice @​non-magical and tied(@​ary)->SPLICE. So it’s the module itself, not the tests, that needs changing.

As an aside, that module isn't built on my system using Configure -d. I tried to specify it as an 'extra' module, but then Perl tries to get it from CPAN. What's the proper method of building it?

Get a Mac?

I really don’t know. Configure -d works for me. Do you have BerkeleyDB installed?

@p5pRT
Copy link
Author

p5pRT commented Jun 20, 2011

From @ikegami

On Mon, Nov 8, 2010 at 1​:33 AM, Father Chrysostomos <sprout@​cpan.org>
wrote​:
This causes test failures in ../cpan/DB_File/t/db-recno.t. Do the
tests need to be updated?

I got DB_File updated upstream, and it was incorporated into blead.

Rebased patch attached.

Thanks,
Eric

@p5pRT
Copy link
Author

p5pRT commented Jun 20, 2011

From @ikegami

0001-The-intent-of-splice-a-MAX_LEN-is-quite-clearly-to-t.patch
From 3893fef7fda5b2563eb64f8a2a6d66d815bf3108 Mon Sep 17 00:00:00 2001
From: Eric Brine <ikegami@adaelis.com>
Date: Wed, 2 Mar 2011 17:30:17 -0500
Subject: [PATCH] The intent of splice(@a,MAX_LEN) is quite clearly to truncate
 the array if it's too large. There's no reason to warn if it's
 currently smaller than the max length.

---
 pod/perldelta.pod |    5 +++++
 pp.c              |    4 +++-
 t/lib/warnings/pp |   19 +++++++++++++++++++
 3 files changed, 27 insertions(+), 1 deletions(-)

diff --git a/pod/perldelta.pod b/pod/perldelta.pod
index 5a020c9..78a6a04 100644
--- a/pod/perldelta.pod
+++ b/pod/perldelta.pod
@@ -27,6 +27,11 @@ L<feature.pm|feature>, even outside the scope of C<use feature>.  Relevant
 documentation files L<CORE>, L<feature>, L<perlfunc>, L<perlsub>, and
 L<perlsyn> have been updated.
 
+=head2 C<splice()> doesn't warn when truncating
+
+You can now limit the size of an array using C<splice(@a,MAX_LEN)> without
+worrying about warnings.
+
 =head2 C<continue> no longer requires the "switch" feature
 
 The C<continue> keyword has two meanings.  It can introduce a C<continue>
diff --git a/pp.c b/pp.c
index 385f1be..d3d4cc8 100644
--- a/pp.c
+++ b/pp.c
@@ -5434,6 +5434,7 @@ S_deref_plain_array(pTHX_ AV *ary)
 PP(pp_splice)
 {
     dVAR; dSP; dMARK; dORIGMARK;
+    int num_args = (SP - MARK);
     register AV *ary = DEREF_PLAIN_ARRAY(MUTABLE_AV(*++MARK));
     register SV **src;
     register SV **dst;
@@ -5477,7 +5478,8 @@ PP(pp_splice)
 	length = AvMAX(ary) + 1;
     }
     if (offset > AvFILLp(ary) + 1) {
-	Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
+	if (num_args > 2)
+	    Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "splice() offset past end of array" );
 	offset = AvFILLp(ary) + 1;
     }
     after = AvFILLp(ary) + 1 - (offset + length);
diff --git a/t/lib/warnings/pp b/t/lib/warnings/pp
index d158144..2251b94 100644
--- a/t/lib/warnings/pp
+++ b/t/lib/warnings/pp
@@ -46,6 +46,25 @@ EXPECT
 Attempt to use reference as lvalue in substr at - line 5.
 ########
 # pp.c
+use warnings 'misc' ;
+@a = qw( a b c );
+splice(@a, 4, 0, 'e') ;
+@a = qw( a b c );
+splice(@a, 4, 1) ;
+@a = qw( a b c );
+splice(@a, 4) ;
+no warnings 'misc' ;
+@a = qw( a b c );
+splice(@a, 4, 0, 'e') ;
+@a = qw( a b c );
+splice(@a, 4, 1) ;
+@a = qw( a b c );
+splice(@a, 4) ;
+EXPECT
+splice() offset past end of array at - line 4.
+splice() offset past end of array at - line 6.
+########
+# pp.c
 use warnings 'uninitialized' ;
 *x = *{ undef() };
 no warnings 'uninitialized' ;
-- 
1.7.2.5

@p5pRT
Copy link
Author

p5pRT commented Jun 21, 2011

From @cpansprout

On Mon Jun 20 12​:54​:48 2011, ikegami@​adaelis.com wrote​:

On Mon, Nov 8, 2010 at 1​:33 AM, Father Chrysostomos <sprout@​cpan.org>
wrote​:
This causes test failures in ../cpan/DB_File/t/db-recno.t. Do the
tests need to be updated?

I got DB_File updated upstream, and it was incorporated into blead.

Rebased patch attached.

Thank you. Applied as 5cd408a.

@p5pRT
Copy link
Author

p5pRT commented Jun 21, 2011

@cpansprout - Status changed from 'open' to 'resolved'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant