zsh-users
 help / color / mirror / code / Atom feed
* segfault when name a local variable "path"
@ 2015-11-20 10:38 Corentin Henry
  2015-11-20 11:11 ` Peter Stephenson
  0 siblings, 1 reply; 2+ messages in thread
From: Corentin Henry @ 2015-11-20 10:38 UTC (permalink / raw)
  To: zsh-users

[-- Attachment #1: Type: text/plain, Size: 637 bytes --]

Hi,

I have the following script:

# segfault.sh
function foo() {
    local path=
}


I source it an execute foo:

% source segfault.sh
% foo

Then zsh segaults. Is this a known bug? If not, can anyone reproduce?
My setup:

arch-lenovo% uname -a
Linux arch-lenovo 4.2.5-1-ARCH #1 SMP PREEMPT Tue Oct 27 08:13:28 CET 2015
x86_64 GNU/Linux
arch-lenovo% zsh --version
zsh 5.1.1 (x86_64-unknown-linux-gnu)

I installed zsh using archlinux package manager. I use prezto (
https://github.com/sorin-ionescu/prezto) but I'm able to reproduce with a
plain zsh config.

Please let me know how I can provide more info, if needed.

Cheers,
Corentin

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: segfault when name a local variable "path"
  2015-11-20 10:38 segfault when name a local variable "path" Corentin Henry
@ 2015-11-20 11:11 ` Peter Stephenson
  0 siblings, 0 replies; 2+ messages in thread
From: Peter Stephenson @ 2015-11-20 11:11 UTC (permalink / raw)
  To: zsh-users

On Fri, 20 Nov 2015 11:38:40 +0100
Corentin Henry <corentinhenry@gmail.com> wrote:
> I have the following script:
> 
> # segfault.sh
> function foo() {
>     local path=
> }
> 
> 
> I source it an execute foo:
> 
> % source segfault.sh
> % foo
> 
> Then zsh segaults. Is this a known bug? If not, can anyone reproduce?

No, yes.  You get a bit more of a hint if DEBUG is defined.

1: builtin.c:2441: BUG: inconsistent scalar value for array

What's happening is "path" is special, and you haven't set the "hide"
attribute so the local still refers to that special variable, which is
an array, but the value is a scalar.

We should probably be lenient in a case like the above, where the intent
is obvious.

It's less clear we should be lenient in a case like

function foo() {
   local path=something
}

because if the something had ":" in it would be a mistake for assigning
PATH instead.  However, that's how normal assignment works, so unless we
change that the most consistent thing is to allow it in this case,
too.

[ws

diff --git a/Src/builtin.c b/Src/builtin.c
index 18dfdce..22479a7 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -2438,10 +2438,26 @@ typeset_single(char *cname, char *pname, Param pm, UNUSED(int func),
     if (ASG_VALUEP(asg) && !dont_set) {
 	Param ipm = pm;
 	if (pm->node.flags & (PM_ARRAY|PM_HASHED)) {
-	    DPUTS(!ASG_ARRAYP(asg), "BUG: inconsistent scalar value for array");
-	    if (!(pm=assignaparam(pname, asg->value.array ?
-				  zlinklist2array(asg->value.array) :
-				  mkarray(NULL), 0)))
+	    char **arrayval;
+	    if (!ASG_ARRAYP(asg)) {
+		/*
+		 * Attempt to assign a scalar value to an array.
+		 * This can happen if the array is special.
+		 * We'll be lenient and guess what the user meant.
+		 */
+		if (*asg->value.scalar) {
+		    /* Array with one value */
+		    arrayval = mkarray(ztrdup(asg->value.scalar));
+		} else {
+		    /* Empty array */
+		    arrayval = mkarray(NULL);
+		}
+	    }
+	    else if (asg->value.array)
+		arrayval = zlinklist2array(asg->value.array);
+	    else
+		arrayval = mkarray(NULL);
+	    if (!(pm=assignaparam(pname, arrayval, 0)))
 		return NULL;
 	} else {
 	    DPUTS(ASG_ARRAYP(asg), "BUG: inconsistent array value for scalar");
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index 210c0d8..a3c5d71 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -1847,3 +1847,22 @@
 0:nested parameter name references
 >all these worlds belong to foo
 >worlds
+
+  (
+  path=(/random /value)
+  testfn1() {
+    local path=
+    print $#path
+  }
+  testfn1
+  testfn2() {
+    local path=/somewhere
+    print $#path $path
+  }
+  testfn2
+  print $#path $path
+  )
+0:Local special variables with loose typing
+>0
+>1 /somewhere
+>2 /random /value


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-11-20 11:11 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-11-20 10:38 segfault when name a local variable "path" Corentin Henry
2015-11-20 11:11 ` Peter Stephenson

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).