zsh-workers
 help / color / mirror / code / Atom feed
From: Peter Stephenson <pws@csr.com>
To: zsh-workers@sunsite.dk
Subject: Re: Memory access bug with negative subscripts
Date: Wed, 25 Jul 2007 10:22:04 +0100	[thread overview]
Message-ID: <20070725102204.799d9cb2@news01.csr.com> (raw)
In-Reply-To: <070724215222.ZM4902@torch.brasslantern.com>

On Tue, 24 Jul 2007 21:52:22 -0700
Bart Schaefer <schaefer@brasslantern.com> wrote:
> This bug appears at least as far back as zsh 3.0.5, and probably longer
> ago.  Still happens in the latest CVS co from SourceForge.
> 
> x=x
> x[-10]=y
> print $x
> 
> I can't predict what you'll see, but unless you get very lucky it won't
> be what you ought to see.

The only sensible choice seems to be to make this do what arrays do,
add the element at the start.  This is inconistent with assignments
to subscript 0 which now cause an error, but that was quite specifically to
deal with the removal of fake KSH_ARRAYS and detect an incorrect assumption
of zero-offset addressing.  There isn't really a logical link, anyway,
since negative offsets count from the end.

Index: Src/params.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/params.c,v
retrieving revision 1.132
diff -u -r1.132 params.c
--- Src/params.c	6 Jul 2007 21:52:39 -0000	1.132
+++ Src/params.c	25 Jul 2007 09:16:26 -0000
@@ -2076,8 +2076,11 @@
 	    }
 	    if (v->start > zlen)
 		v->start = zlen;
-	    if (v->end < 0)
+	    if (v->end < 0) {
 		v->end += zlen + 1;
+		if (v->end < 0)
+		    v->end = 0;
+	    }
 	    else if (v->end > zlen)
 		v->end = zlen;
 	    x = (char *) zalloc(v->start + strlen(val) + zlen - v->end + 1);
Index: Test/D04parameter.ztst
===================================================================
RCS file: /cvsroot/zsh/zsh/Test/D04parameter.ztst,v
retrieving revision 1.26
diff -u -r1.26 D04parameter.ztst
--- Test/D04parameter.ztst	6 Jul 2007 13:10:45 -0000	1.26
+++ Test/D04parameter.ztst	25 Jul 2007 09:16:27 -0000
@@ -920,3 +920,20 @@
 0:Numeric sorting
 >a6 a17 a117 b6 b17 b117
 >b117 b17 b6 a117 a17 a6
+
+  x=sprodj
+  x[-10]=scrumf
+  print $x
+0:Out of range negative scalar subscripts
+>scrumfsprodj
+
+  a=(some sunny day)
+  a[-10]=(we\'ll meet again)
+  print -l $a
+0:Out of range negative array subscripts
+>we'll
+>meet
+>again
+>some
+>sunny
+>day


-- 
Peter Stephenson <pws@csr.com>                  Software Engineer
CSR PLC, Churchill House, Cambridge Business Park, Cowley Road
Cambridge, CB4 0WZ, UK                          Tel: +44 (0)1223 692070


.


      reply	other threads:[~2007-07-25  9:22 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-07-25  4:52 Bart Schaefer
2007-07-25  9:22 ` Peter Stephenson [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20070725102204.799d9cb2@news01.csr.com \
    --to=pws@csr.com \
    --cc=zsh-workers@sunsite.dk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).