FAQ
(Note this is faking the perlbug report)

Repeating a list with "x -1" (or any negative number) causes a panic,
but only in blead.

andy@baby[~]$ cat testmany
#!/bin/bash

for VER in 5.4.0 5.5.3 5.6.0 5.6.1 5.6.2 5.8.0 5.8.1 5.8.2 5.8.3 5.9.1 blead; do
PERL="perl$VER"
echo $PERL
$PERL -le'@x=1;@y=(@x)x-1'
done


andy@baby[~]$ ./testmany
perl5.4.0
perl5.5.3
perl5.6.0
perl5.6.1
perl5.6.2
perl5.8.0
perl5.8.1
perl5.8.2
perl5.8.3
perl5.9.1
perlblead
panic: list extend at -e line 1.


Does it make sense to x-1? Personally, I think a negative number ought
to act like zero, but whatever it does, we should document it.

xoa

--
Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance

Search Discussions

  • Richard Soderberg at Mar 21, 2004 at 4:26 am

    On 2004-03-20 19:05:36 -0800, andy@petdance.com (Andy Lester) said:

    (Note this is faking the perlbug report)

    Repeating a list with "x -1" (or any negative number) causes a panic,
    but only in blead. [snip]
    panic: list extend at -e line 1.

    Does it make sense to x-1? Personally, I think a negative number
    ought to act like zero, but whatever it does, we should document it.
    The code path in pp_repeat is three-pronged: if count > 1, then repeat
    the items count times; if count < 1, then remove the items; otherwise,
    if count == 0, leave the items alone. The "list extend" panic is
    triggered when the final count of items is calculated -- but it's being
    calculated *before* the count branch. Since the list panic is
    triggered by calculations performed with negative values of count,
    moving the calculations inside the count > 1 block resolves the panic.
    The results of the calculations are only necessary if the count > 1
    branch is taken, so this bugfix is also technically an optimization :)

    Here's a patch for the code, to fix the panic; this will resolve the
    problem you described in #27791, as well; porters, Andy's patch [1]
    provides tests and documentation for the now-fixed behavior of
    pp_repeat with negative counts.

    [1] http://www.nntp.perl.org/group/perl.perl5.porters/89887

    - R.

    --- perl-current/pp.c Thu Mar 18 11:23:19 2004
    +++ ./pp.c Sat Mar 20 19:50:34 2004
    @@ -1389,15 +1389,17 @@
    if (GIMME == G_ARRAY && PL_op->op_private & OPpREPEAT_DOLIST) {
    dMARK;
    I32 items = SP - MARK;
    - I32 max;
    - static const char list_extend[] = "panic: list extend";

    - max = items * count;
    - MEM_WRAP_CHECK_1(max, SV*, list_extend);
    - if (items > 0 && max > 0 && (max < items || max < count))
    - Perl_croak(aTHX_ list_extend);
    - MEXTEND(MARK, max);
    if (count > 1) {
    + I32 max;
    + static const char list_extend[] = "panic: list extend";
    +
    + max = items * count;
    + MEM_WRAP_CHECK_1(max, SV*, list_extend);
    + if (items > 0 && max > 0 && (max < items || max < count))
    + Perl_croak(aTHX_ list_extend);
    + MEXTEND(MARK, max);
    +
    while (SP > MARK) {
    #if 0
    /* This code was intended to fix 20010809.028:
  • Andreas J Koenig at Mar 21, 2004 at 10:14 am
    On Sat, 20 Mar 2004 21:05:36 -0600, Andy Lester said:
    ----Program----
    @x=1;@y=(@x)x-1

    ----Output of .../pQb9Nqu/perl-5.8.0@22516/bin/perl----

    ----EOF ($?='0')----
    ----Output of .../pxEitd4/perl-5.8.0@22517/bin/perl----
    panic: list extend at tests/andy_x-1.pl line 1.

    ----EOF ($?='65280')----

    Change 22517 by nicholas@faith on 2004/03/17 17:58:44

    Subject: malloc wrappage
    From: Jarkko Hietaniemi <jhi@iki.fi>
    Message-Id: <37BF70DE-5C0E-11D8-B5A1-00039362CB92@iki.fi>
    Date: Tue, 10 Feb 2004 23:15:14 +0200

    plus change croak to Perl_croak_nocontext to make ithread safe
    plus make it conditional on PERL_MALLOC_WRAP (default for blead is on)



    --
    andreas
  • Rafael Garcia-Suarez at Mar 21, 2004 at 1:07 pm

    Andy Lester wrote:
    $PERL -le'@x=1;@y=(@x)x-1' <
    Does it make sense to x-1? Personally, I think a negative number ought
    to act like zero, but whatever it does, we should document it.
    This should at the very least produce a run time warning.
  • Rafael Garcia-Suarez at Mar 21, 2004 at 1:27 pm

    Rafael Garcia-Suarez wrote:
    Andy Lester wrote:
    $PERL -le'@x=1;@y=(@x)x-1' <
    Does it make sense to x-1? Personally, I think a negative number ought
    to act like zero, but whatever it does, we should document it.
    This should at the very least produce a run time warning.
    ...which I added as #22543. (and I fixed the panic too)
  • Mark Jason Dominus at Mar 21, 2004 at 3:54 pm

    Rafael Garcia-Suarez <rgarciasuarez@free.fr>:
    Rafael Garcia-Suarez wrote:
    Andy Lester wrote:
    $PERL -le'@x=1;@y=(@x)x-1' <
    Does it make sense to x-1? Personally, I think a negative number ought
    to act like zero, but whatever it does, we should document it.
    This should at the very least produce a run time warning.
    ...which I added as #22543. (and I fixed the panic too)
    I wonder if it's really a good idea to put in a warning for this. It
    seems to me like behavior someone might reasonably be depending on.
    It's analogous to the way that

    $x .. $y

    yields the empty list when $x > $y. There's no warning for .. when
    $x > $y, and 'x -1' seems similar to me. Is it possible that people
    are overreacting because of the panic?
  • Andy Lester at Mar 21, 2004 at 3:24 pm

    Does it make sense to x-1? Personally, I think a negative number ought
    to act like zero, but whatever it does, we should document it.
    This should at the very least produce a run time warning.
    I don't disagree. However, as it stands now, and as it has been back to
    5.004, a negative number is effectively a zero. We should apply my
    patch and Richard's patch to at least get it documented and non-panicing
    while p5p argues at length about it.

    xoa

    --
    Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance
  • Andy Lester at Mar 21, 2004 at 3:27 pm

    I don't disagree. However, as it stands now, and as it has been back to
    5.004, a negative number is effectively a zero. We should apply my
    patch and Richard's patch to at least get it documented and non-panicing
    while p5p argues at length about it.
    And we still need the original test patch applied.

    --- bleadperl/t/op/repeat.t 2001-11-18 12:11:25.000000000 -0600
    +++ patch/t/op/repeat.t 2004-03-20 19:07:45.000000000 -0600
    @@ -6,13 +6,15 @@
    }

    require './test.pl';
    -plan(tests => 25);
    +plan(tests => 33);

    # compile time

    is('-' x 5, '-----', 'compile time x');
    is('-' x 1, '-', ' x 1');
    is('-' x 0, '', ' x 0');
    +is('-' x -1, '', ' x -1');
    +is('-' x undef, '', ' x undef');

    is('ab' x 3, 'ababab', ' more than one char');

    @@ -22,9 +24,15 @@
    is($a x 5, '-----', 'run time x');
    is($a x 1, '-', ' x 1');
    is($a x 0, '', ' x 0');
    +is($a x -3, '', ' x -3');
    +is($a x undef, '', ' x undef');

    $a = 'ab';
    is($a x 3, 'ababab', ' more than one char');
    +$a = 'ab';
    +is($a x 0, '', ' more than one char');
    +$a = 'ab';
    +is($a x -12, '', ' more than one char');

    $a = 'xyz';
    $a x= 2;
    @@ -45,6 +53,9 @@
    is(join(':', (9,9) x 4), '9:9:9:9:9:9:9:9', '(X,X) x Y');
    is(join('', (split(//,"123")) x 2), '123123', 'split and x');

    +is(join('', @x x -12), '', '@x x -12');
    +is(join('', (@x) x -14), '', '(@x) x -14');
    +

    # This test is actually testing for Digital C compiler optimizer bug,
    # present in Dec C versions 5.* and 6.0 (used in Digital UNIX and VMS),

    --
    Andy Lester => andy@petdance.com => www.petdance.com => AIM:petdance
  • Rafael Garcia-Suarez at Mar 21, 2004 at 5:44 pm

    Andy Lester wrote:
    I don't disagree. However, as it stands now, and as it has been back to
    5.004, a negative number is effectively a zero. We should apply my
    patch and Richard's patch to at least get it documented and non-panicing
    while p5p argues at length about it.
    And we still need the original test patch applied.

    --- bleadperl/t/op/repeat.t 2001-11-18 12:11:25.000000000 -0600
    +++ patch/t/op/repeat.t 2004-03-20 19:07:45.000000000 -0600
    Thanks, applied as #22549.

    (I have probably missed Richard's patch, I don't see it anywhere)
  • Ton Hospel at Mar 21, 2004 at 4:02 pm
    In article <20040321140746.7003_3a3.rgarciasuarez@_ree._r>,
    Rafael Garcia-Suarez <rgarciasuarez@free.fr> writes:
    Andy Lester wrote:
    $PERL -le'@x=1;@y=(@x)x-1' <
    Does it make sense to x-1? Personally, I think a negative number ought
    to act like zero, but whatever it does, we should document it.
    This should at the very least produce a run time warning.
    I at least used it in real code. It's very convenient when e.g.
    "almost" lining up. Think:
    $str . " " x ($width - length $str) . " Something that comes behind"
    (but then for more complex cases, for this you could also use sprintf).
  • Rafael Garcia-Suarez at Mar 21, 2004 at 5:46 pm

    Ton Hospel wrote:
    In article <20040321140746.7003_3a3.rgarciasuarez@_ree._r>,
    Rafael Garcia-Suarez <rgarciasuarez@free.fr> writes:
    Andy Lester wrote:
    $PERL -le'@x=1;@y=(@x)x-1' <
    Does it make sense to x-1? Personally, I think a negative number ought
    to act like zero, but whatever it does, we should document it.
    This should at the very least produce a run time warning.
    I at least used it in real code. It's very convenient when e.g.
    "almost" lining up. Think:
    $str . " " x ($width - length $str) . " Something that comes behind"
    (but then for more complex cases, for this you could also use sprintf).
    Right. Spurious warning backed out (as apparently "x $negnumber" is not
    really an error difficult to catch compared to a desired behaviour.)

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupperl5-porters @
categoriesperl
postedMar 21, '04 at 3:05a
activeMar 21, '04 at 5:46p
posts11
users6
websiteperl.org

People

Translate

site design / logo © 2022 Grokbase