zsh-workers
 help / color / mirror / code / Atom feed
From: Bart Schaefer <schaefer@brasslantern.com>
To: zsh-workers@zsh.org
Subject: Re: zsh segfaul in 5.0.2, known bug?
Date: Mon, 30 Dec 2013 23:22:55 -0800	[thread overview]
Message-ID: <131230232255.ZM27137@torch.brasslantern.com> (raw)
In-Reply-To: <CACmrRmTgZQT_U+_T6c4vfZ32Jmz=8cUCOH+iMKnQJNXue6Es1Q@mail.gmail.com>

On Dec 30,  4:07pm, Sam Roberts wrote:
}
} % (export path=$PATH; unset PATH; date)
} export: can't assign new value for array path
} zsh: segmentation fault (core dumped)  ( export path=$PATH; unset PATH; date; )

Hmm, this is a sneaky one.  The "export" is entirely irrelevant.

If the command is "unset path", then we first unset the path array, and
then unset the PATH scalar, which repairs the environment and thereby
resets an internal pointer that tracks how much of the path array has
been searched.

However, when the command is "unset PATH", we first unset the scalar,
which resets the internal pointer, but then unset the array, which
leaves that other pointer aimed at the freed memory that formerly
held the path.

*Except* that unsetting the path array immediately re-allocates it as
an array with no elements.  In most cases this re-uses the same block
that was just freed, so by accident the other pointer becomes valid
again.  If the malloc() library routine happens to have the property
that it might not return that same most-recently-freed block, then the
other pointer remains invalid and a crash results when the next path
search tries to pick up where the previous one left off.  (There was no
previous search, but it can't tell that because the pointer is bad.)

The ZSH_MEM_DEBUG library happens *to* re-use the most-recently-freed
block, so a typical debugger session does not reveal the problem.  I
found it with valgrind and then puzzled out the order of operations.

The following fixes the crash, but it's exposing in arrvarsetfn() some
knowledge that was previously confined to arrfixenv(), so there may be
a cleaner way to handle it.

diff --git a/Src/params.c b/Src/params.c
index 26ad6b2..ad9e347 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -3380,8 +3380,12 @@ arrvarsetfn(Param pm, char **x)
 	*dptr = mkarray(NULL);
     else
 	*dptr = x;
-    if (pm->ename && x)
-	arrfixenv(pm->ename, x);
+    if (pm->ename) {
+	if (x)
+	    arrfixenv(pm->ename, x);
+	else if (*dptr == path)
+	    pathchecked = path;
+    }
 }
 
 /**/


      parent reply	other threads:[~2013-12-31  7:23 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-12-31  0:07 Sam Roberts
2013-12-31  6:06 ` Axel Beckert
2013-12-31  7:22 ` Bart Schaefer [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=131230232255.ZM27137@torch.brasslantern.com \
    --to=schaefer@brasslantern.com \
    --cc=zsh-workers@zsh.org \
    /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).