Skip to content

Array slice indexing bug (when mixing positive and negative notation) #5559

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

Closed
p5pRT opened this issue Jun 11, 2002 · 6 comments
Closed

Array slice indexing bug (when mixing positive and negative notation) #5559

p5pRT opened this issue Jun 11, 2002 · 6 comments

Comments

@p5pRT
Copy link

p5pRT commented Jun 11, 2002

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

Searchable as RT9533$

@p5pRT
Copy link
Author

p5pRT commented Jun 11, 2002

From [email protected]

This is a bug report for perl from torbenh@​mips.com,
generated with the help of perlbug 1.33 running under perl v5.6.1.

#!/usr/local/bin/perl -w

# I hope the bug is obvious​: I expect all the 4 last cases to be the same
# I am using perl 5.6.1 on Sun/Solaris 5.8 - I also tried perl 5.6.0 on
# i386/Linux with same result
#
# /Torben Vendt Hansen

@​a = qw(1 2 3 4 5);

print("\@​a​: @​a\n");
print("OK​: \$a[3] $a[3]\n");
print("OK​: \$a[-2] $a[-2]\n");
print("OK​: \@​a[3 .. 4] @​a[3 .. 4]\n");
print("OK​: \@​a[-2 .. -1] @​a[-2 .. -1]\n");
print("BUG​: \@​a[3 .. -1] @​a[3 .. -1]\n");
print("BUG​: \@​a[-2 .. 4] @​a[-2 .. 4]\n");


Flags​:
  category=core
  severity=high


Site configuration information for perl v5.6.1​:

Configured by steve at Wed Apr 25 02​:36​:44 EDT 2001.

Summary of my perl5 (revision 5.0 version 6 subversion 1) configuration​:
  Platform​:
  osname=solaris, osvers=2.8, archname=sun4-solaris
  uname='sunos solaris 5.8 generic sun4u sparc sunw,ultra-5_10 '
  config_args='-Dcc=gcc -de'
  hint=recommended, useposix=true, d_sigaction=define
  usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
  useperlio=undef d_sfio=undef uselargefiles=define usesocks=undef
  use64bitint=undef use64bitall=undef uselongdouble=undef
  Compiler​:
  cc='gcc', ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64',
  optimize='-O',
  cppflags='-fno-strict-aliasing -I/usr/local/include'
  ccversion='', gccversion='2.95.3 20010315 (release)', gccosandvers='solaris2.8'
  intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=4321
  d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
  ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
  alignbytes=8, usemymalloc=y, prototype=define
  Linker and Libraries​:
  ld='gcc', ldflags =' -L/usr/local/lib '
  libpth=/usr/local/lib /usr/lib /usr/ccs/lib
  libs=-lsocket -lnsl -lgdbm -ldb -ldl -lm -lc
  perllibs=-lsocket -lnsl -ldl -lm -lc
  libc=/lib/libc.so, so=so, useshrplib=false, libperl=libperl.a
  Dynamic Linking​:
  dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags=' '
  cccdlflags='-fPIC', lddlflags='-G -L/usr/local/lib'

Locally applied patches​:
 


@​INC for perl v5.6.1​:
  /usr/local/lib/perl5/5.6.1/sun4-solaris
  /usr/local/lib/perl5/5.6.1
  /usr/local/lib/perl5/site_perl/5.6.1/sun4-solaris
  /usr/local/lib/perl5/site_perl/5.6.1
  /usr/local/lib/perl5/site_perl
  .


Environment for perl v5.6.1​:
  HOME=/home/torbenh
  LANG=C
  LANGUAGE (unset)
  LD_LIBRARY_PATH=/mips/sw/lsf42/sparcOS5_64/lib​:/mips/sw/lsf42/sparcOS5_64/lib​:/usr/local/lib​:/usr/openwin/lib​:/usr/dt/lib​::/usr/local/freetds/lib
  LOGDIR (unset)
  PATH=/usr/local/lsf/sparcOS5_64/bin​:.​:/home/torbenh/bin​:/home/torbenh/bin/Solaris​:/usr/local/bin​:/bin​:/usr/bin​:/sbin​:/usr/sbin​:/usr/ucb​:/usr/ccs/bin​:/usr/openwin/bin​:/usr/dt/bin​:/home/eng/bin
  PERL_BADLANG (unset)
  SHELL=/usr/local/bin/tcsh

@p5pRT
Copy link
Author

p5pRT commented Jun 11, 2002

From [Unknown Contact. See original ticket]

On Jun 11, Torben Vendt Hansen said​:

# I hope the bug is obvious​: I expect all the 4 last cases to be the same
# I am using perl 5.6.1 on Sun/Solaris 5.8 - I also tried perl 5.6.0 on
# i386/Linux with same result

It is not a bug.

@​a = qw(1 2 3 4 5);

print("BUG​: \@​a[3 .. -1] @​a[3 .. -1]\n");
print("BUG​: \@​a[-2 .. 4] @​a[-2 .. 4]\n");

@​a[-2 .. 4] is merely shorthand for @​a[-2, -1, 0, 1, 2, 3, 4], which is
consistant and proper. @​a[3 .. -1] should yield a warning; the range
operator takes two operands, a low and a high, in that order. It is NOT
magically linked to arrays somehow, and it does not work backwards.

--
Jeff "japhy" Pinyan japhy@​pobox.com http​://www.pobox.com/~japhy/
RPI Acacia brother #734 http​://www.perlmonks.org/ http​://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for? <tenderpuss> why, yansliterate of course.
[ I'm looking for programming work. If you like my work, let me know. ]

@p5pRT
Copy link
Author

p5pRT commented Jun 11, 2002

From @schwern

On Tue, Jun 11, 2002 at 10​:41​:58AM -0400, Jeff 'japhy' Pinyan wrote​:

@​a[-2 .. 4] is merely shorthand for @​a[-2, -1, 0, 1, 2, 3, 4], which is
consistant and proper. @​a[3 .. -1] should yield a warning; the range
operator takes two operands, a low and a high, in that order. It is NOT
magically linked to arrays somehow, and it does not work backwards.

Maybe the bug is that it doesn't produce a warning when used backwards.

$ perl -wle '@​a = qw(1 2 3 4 5); print @​a[3 .. -1]'

Or maybe it should just work backwards. :)

--
This sig file temporarily out of order.

@p5pRT
Copy link
Author

p5pRT commented Jun 11, 2002

From [Unknown Contact. See original ticket]

-----BEGIN PGP SIGNED MESSAGE-----

Moin,

From​: Michael G Schwern <schwern@​pobox.com>
To​: japhy@​pobox.com
Cc​: Torben Vendt Hansen <torbenh@​mips.com>, perl5-porters@​perl.org
Date​: Tue, 11 Jun 2002 15​:43​:07 -0400
Message-id​: <20020611194307.GM31617@​ool-18b93024.dyn.optonline.net>

On Tue, Jun 11, 2002 at 10​:41​:58AM -0400, Jeff 'japhy' Pinyan wrote​:

@​a[-2 .. 4] is merely shorthand for @​a[-2, -1, 0, 1, 2, 3, 4], which is
consistant and proper. @​a[3 .. -1] should yield a warning; the range
operator takes two operands, a low and a high, in that order. It is NOT
magically linked to arrays somehow, and it does not work backwards.

Maybe the bug is that it doesn't produce a warning when used backwards.

$ perl -wle '@​a = qw(1 2 3 4 5); print @​a[3 .. -1]'

Or maybe it should just work backwards. :)

You could overload it. Oh wait, I never managed to bribe^Wlobby someone to
implement this...​:)

- --
perl -MDev​::Bollocks -e'print Dev​::Bollocks->rand(),"\n"'
ambassadorially transform visionary m-commerce

http​://bloodgate.com/perl My current Perl projects
PGP key available on http​://bloodgate.com/tels.asc or via email

-----BEGIN PGP SIGNATURE-----
Version​: GnuPG v1.0.6 (GNU/Linux)
Comment​: When cryptography is outlawed, bayl bhgynjf jvyy unir cevinpl.

iQEVAwUBPQZvGXcLPEOTuEwVAQHNsQf+Ml4PTgnv4B2OhAFsyJBmg4k1GtCQN4/1
vVzEGTdMcN8koijMFFWlNJ6lf85JFMQDh0UJnhoCloplgciWSgqGr6nJXAZT/q7p
v1C5GB6J+75xZLKfFLJfMvB32Ec0kIZp92X391byGh/q7GgJaAtjJMQs6clvUbe3
Tp22x6czvrI3n4+2ZizYwkFkAQqpIzhd7+Qnx9uS1sNkjoyt28Ag0STlnxRdWJry
/whmxGKzRlpxWlj9E+ROSgCkstfdq4ndAtc8VFMYn/q8rs2CEQ8kMsCeIc7L8QSn
XSoNf4BiqtJE2yrQYGVNc85/kQwaH9zm6vG6QJrx8y6HKJMkVWFeZw==
=N4yf
-----END PGP SIGNATURE-----

@p5pRT
Copy link
Author

p5pRT commented Jun 12, 2002

From @Abigail

On Tue, Jun 11, 2002 at 03​:43​:07PM -0400, Michael G Schwern wrote​:

On Tue, Jun 11, 2002 at 10​:41​:58AM -0400, Jeff 'japhy' Pinyan wrote​:

@​a[-2 .. 4] is merely shorthand for @​a[-2, -1, 0, 1, 2, 3, 4], which is
consistant and proper. @​a[3 .. -1] should yield a warning; the range
operator takes two operands, a low and a high, in that order. It is NOT
magically linked to arrays somehow, and it does not work backwards.

Maybe the bug is that it doesn't produce a warning when used backwards.

I don't think so. I find the behaviour useful and I often exploit it.
Not with 2 constants in the range of course, but with one of them
variable.

  # Deal with the rest of the array.
  foreach my $elem (@​array [$i .. $#array]) { ... }

And this neatly skips doesn't run the loop if the range is empty,
that is, if $i &gt; $#array.

$ perl -wle '@​a = qw(1 2 3 4 5); print @​a[3 .. -1]'

Or maybe it should just work backwards. :)

 
That's why we have $#array and @​array in scalar context.

Abigail

@p5pRT
Copy link
Author

p5pRT commented Jun 12, 2002

From @iabyn

On Wed, Jun 12, 2002 at 02​:59​:10AM -0700, Abigail wrote​:

On Tue, Jun 11, 2002 at 03​:43​:07PM -0400, Michael G Schwern wrote​:

On Tue, Jun 11, 2002 at 10​:41​:58AM -0400, Jeff 'japhy' Pinyan wrote​:

@​a[-2 .. 4] is merely shorthand for @​a[-2, -1, 0, 1, 2, 3, 4], which is
consistant and proper. @​a[3 .. -1] should yield a warning; the range
operator takes two operands, a low and a high, in that order. It is NOT
magically linked to arrays somehow, and it does not work backwards.

Maybe the bug is that it doesn't produce a warning when used backwards.

I don't think so. I find the behaviour useful and I often exploit it.
Not with 2 constants in the range of course, but with one of them
variable.

\# Deal with the rest of the array\.
foreach my $elem \(@&#8203;array \[$i \.\. $\#array\]\) \{  \.\.\.  \}

Maybe a warning only when both values are constant?
(Of course, this would still annoy those people using eval.)

Dave.

--
"Emacs isn't a bad OS once you get used to it.
It just lacks a decent editor."

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