From 58a9d1fc4b9782cdc8aaf9dd6fdfa2736076b173 Mon Sep 17 00:00:00 2001 From: Rafael Garcia-Suarez Date: Sun, 21 Mar 2004 13:06:20 +0000 Subject: [PATCH] Add a new warning "Negative repeat count" for the cases $x x -1. p4raw-id: //depot/perl@22543 --- pod/perlop.pod | 4 +++- pp.c | 5 +++++ t/lib/warnings/pp | 9 +++++++++ 3 files changed, 17 insertions(+), 1 deletion(-) diff --git a/pod/perlop.pod b/pod/perlop.pod index 86cb294..4430fe7 100644 --- a/pod/perlop.pod +++ b/pod/perlop.pod @@ -238,7 +238,9 @@ Binary "x" is the repetition operator. In scalar context or if the left operand is not enclosed in parentheses, it returns a string consisting of the left operand repeated the number of times specified by the right operand. In list context, if the left operand is enclosed in -parentheses, it repeats the list. +parentheses, it repeats the list. If the right operand is zero or +negative, it returns an empty string or an empty list, depending on the +context. print '-' x 80; # print row of dashes diff --git a/pp.c b/pp.c index d3434c3..53c7162 100644 --- a/pp.c +++ b/pp.c @@ -1386,6 +1386,11 @@ PP(pp_repeat) dSP; dATARGET; tryAMAGICbin(repeat,opASSIGN); { register IV count = POPi; + if (count < 0) { + if (ckWARN(WARN_MISC)) + Perl_warner(aTHX_ packWARN(WARN_MISC), "Negative repeat count"); + count = 0; + } if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) { dMARK; I32 items = SP - MARK; diff --git a/t/lib/warnings/pp b/t/lib/warnings/pp index 5ed7aa0..db42027 100644 --- a/t/lib/warnings/pp +++ b/t/lib/warnings/pp @@ -102,3 +102,12 @@ use utf8 ; $_ = "\x80 \xff" ; reverse ; EXPECT +######## +# pp.c +use warnings; +$a = "b" x -1; +$a = "b" x 0; +no warnings; +$a = "b" x -1; +EXPECT +Negative repeat count at - line 3. -- 2.7.4