summary refs log tree commit diff
diff options
context:
space:
mode:
authorDaniel Shahaf <d.s@daniel.shahaf.name>2016-11-20 11:46:48 +0000
committerDaniel Shahaf <d.s@daniel.shahaf.name>2016-11-20 11:56:02 +0000
commitf35198d8379303f47c935d8fdd18bc1a76a111aa (patch)
tree98dadf670f191ab932d82a115989a3c64d4c49d0
parent6e1684e332dff35992b6c71aab9b4958627263a6 (diff)
downloadzsh-f35198d8379303f47c935d8fdd18bc1a76a111aa.tar.gz
zsh-f35198d8379303f47c935d8fdd18bc1a76a111aa.tar.xz
zsh-f35198d8379303f47c935d8fdd18bc1a76a111aa.zip
39992: setarrvalue: Allocate a correctly-sized array.
No memory was lost; the array was allocated with room for one (char *) element
more than was required.
-rw-r--r--ChangeLog3
-rw-r--r--Src/params.c12
2 files changed, 13 insertions, 2 deletions
diff --git a/ChangeLog b/ChangeLog
index cdaa0c1fe..13e52e51b 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,8 @@
 2016-11-20  Daniel Shahaf  <d.s@daniel.shahaf.name>
 
+	* 39992: Src/params.c: setarrvalue: Allocate a correctly-sized
+	array.
+
 	* unposted (after 39952): Src/Zle/zle_params.c: Restore C89
 	compatibility.
 
diff --git a/Src/params.c b/Src/params.c
index 3c8658cc3..9d741cb7b 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2654,8 +2654,13 @@ setarrvalue(Value v, char **val)
 	    v->end = v->start;
 
 	post_assignment_length = v->start + arrlen(val);
-	if (v->end <= pre_assignment_length)
-	    post_assignment_length += pre_assignment_length - v->end + 1;
+	if (v->end < pre_assignment_length) {
+	    /* 
+	     * Allocate room for array elements between the end of the slice `v'
+	     * and the original array's end.
+	     */
+	    post_assignment_length += pre_assignment_length - v->end;
+	}
 
 	p = new = (char **) zalloc(sizeof(char *)
 		                   * (post_assignment_length + 1));
@@ -2671,6 +2676,9 @@ setarrvalue(Value v, char **val)
 		*p++ = ztrdup(*q++);
 	*p = NULL;
 
+	DPUTS2(p - new != post_assignment_length, "setarrvalue: wrong allocation: %d 1= %lu",
+	       post_assignment_length, (unsigned long)(p - new));
+
 	v->pm->gsu.a->setfn(v->pm, new);
 
         /* Ownership of all strings has been