zsh-workers
 help / color / mirror / code / Atom feed
* segfault due to chpwd_function
@ 2009-12-11 17:36 Joel Bernstein
  2009-12-11 17:52 ` Joel Bernstein
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Joel Bernstein @ 2009-12-11 17:36 UTC (permalink / raw)
  To: zsh-workers

I found a scenario leading to a segfault caused by a chpwd_function. The function in question runs to completion when called from command line directly after the 'cd' command, if it is removed from chpwd_functions and called manually. The chpwd_function in question calls the "prompt" function.

Curiously, if I add debug prints to my chpwd_function and precmd_function, I see that the chpwd_function completes and the precmd_function is not entered. Thus I conclude that some state is mutated by my chpwd_function (e.g. use of prompt()?) which causes zsh to segfault after running it.

The segfault (on e.g. cd ~/foo) looks like:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: KERN_INVALID_ADDRESS at address: 0x0000000000007075
0x0000000100020be4 in hasher ()
(gdb) bt
#0  0x0000000100020be4 in hasher ()
#1  0x0000000100020c2b in gethashnode ()
#2  0x0000000100069995 in callhookfunc ()
#3  0x000000010000f057 in bin_cd ()
#4  0x000000010000cf0b in execbuiltin ()
#5  0x0000000100016adb in execcmd ()
#6  0x0000000100017995 in execpline2 ()
#7  0x0000000100017d7c in execpline ()
#8  0x0000000100018e27 in execlist ()
#9  0x0000000100018f65 in execode ()
#10 0x000000010002ab27 in loop ()
#11 0x000000010002bf74 in zsh_main ()
#12 0x0000000100001374 in start ()

And my chpwd_function looks like:
prompt_chpwd () {
        current_prompt=$(prompt -c | tail -n1 | perl -pe 's/\W//g') 
        zgit_isgit && {
                if [ "$current_prompt" != "wunjo" ]
                then
                        prompt wunjo
                fi
        } || {
                if [ "$current_prompt" != "joel" ]
                then
                        prompt joel
                fi
        }
}

I believe I can workaround this issue by setting the desired prompt theme to a var in my chpwd_function and calling prompt() from my precmd_function but it seems an odd behaviour and perhaps a bug?

I am running zsh 4.3.10 (i386-apple-darwin10.0.0). 
Please let me know if I can provide more information.

/joel

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

* Re: segfault due to chpwd_function
  2009-12-11 17:36 segfault due to chpwd_function Joel Bernstein
@ 2009-12-11 17:52 ` Joel Bernstein
  2009-12-11 18:11 ` Bart Schaefer
  2009-12-11 18:41 ` Peter Stephenson
  2 siblings, 0 replies; 6+ messages in thread
From: Joel Bernstein @ 2009-12-11 17:52 UTC (permalink / raw)
  To: zsh-workers

On 11 Dec 2009, at 17:36, Joel Bernstein wrote:
> I believe I can workaround this issue by setting the desired prompt theme to a var in my chpwd_function and calling prompt() from my precmd_function 

Actually, this doesn't work, I still get a segfault:
Program received signal EXC_BAD_ACCESS, Could not access memory.
Reason: 13 at address: 0x0000000000000000
0x0000000100020be4 in hasher ()
(gdb) bt
#0  0x0000000100020be4 in hasher ()
#1  0x0000000100020c2b in gethashnode ()
#2  0x0000000100069995 in callhookfunc ()
#3  0x000000010006d075 in preprompt ()
#4  0x000000010002ac33 in loop ()
#5  0x000000010002bf74 in zsh_main ()
#6  0x0000000100001374 in start ()

Is this a zsh bug?

Any ideas how to achieve dynamic prompt theme changing based on some command being run against the directory being entered?

/joel

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

* Re: segfault due to chpwd_function
  2009-12-11 17:36 segfault due to chpwd_function Joel Bernstein
  2009-12-11 17:52 ` Joel Bernstein
@ 2009-12-11 18:11 ` Bart Schaefer
  2009-12-11 18:41 ` Peter Stephenson
  2 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2009-12-11 18:11 UTC (permalink / raw)
  To: Joel Bernstein, zsh-workers

On Dec 11,  5:36pm, Joel Bernstein wrote:
} Subject: segfault due to chpwd_function
}
} I found a scenario leading to a segfault caused by a chpwd_function.
} The function in question runs to completion when called from
} command line directly after the 'cd' command, if it is removed from
} chpwd_functions and called manually. The chpwd_function in question
} calls the "prompt" function.

I suspect the problem is that "prompt" calls "add-zsh-hook -D ..." which
is muddling with the hooks arrays while they're still being walked by
the hook-processing internals.

As a workaround, try setting the prompt theme from the zle-line-init
widget; for ex:

zle-line-init() { prompt wunjo; zle reset-prompt; zle -R }
zle -N zle-line-init

Refine by creating the widget in chpwd hooks and deleting it from within
itself after the zle -R.


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

* Re: segfault due to chpwd_function
  2009-12-11 17:36 segfault due to chpwd_function Joel Bernstein
  2009-12-11 17:52 ` Joel Bernstein
  2009-12-11 18:11 ` Bart Schaefer
@ 2009-12-11 18:41 ` Peter Stephenson
  2009-12-11 19:01   ` Bart Schaefer
  2 siblings, 1 reply; 6+ messages in thread
From: Peter Stephenson @ 2009-12-11 18:41 UTC (permalink / raw)
  To: Joel Bernstein, zsh-workers

Joel Bernstein wrote:
> I found a scenario leading to a segfault caused by a chpwd_function.

Your function run with the current zsh development version gives me
(when I change directory):

prompt_chpwd:2: command not found: zgit_isgit

and an error message from the prompt system.

Are you able to post a self-contained function that shows the problem,
with no external depenencies?

Thanks

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


Member of the CSR plc group of companies. CSR plc registered in England and Wales, registered number 4187346, registered office Churchill House, Cambridge Business Park, Cowley Road, Cambridge, CB4 0WZ, United Kingdom


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

* Re: segfault due to chpwd_function
  2009-12-11 18:41 ` Peter Stephenson
@ 2009-12-11 19:01   ` Bart Schaefer
  2009-12-11 22:16     ` Peter Stephenson
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2009-12-11 19:01 UTC (permalink / raw)
  To: zsh-workers

On Dec 11,  6:41pm, Peter Stephenson wrote:
}
} Are you able to post a self-contained function that shows the problem,
} with no external depenencies?

This seems to do it:

autoload -U promptinit
promptinit
prompt_chpwd() {
  ((RANDOM % 2)) && prompt bart || prompt zefram 
}
precmd_functions=(prompt_chpwd)

Program received signal SIGSEGV, Segmentation fault.
0x0807226c in hasher (str=0x10 <Address 0x10 out of bounds>)
    at ../../zsh-4.0/Src/hashtable.c:85
85          while ((c = *((unsigned char *) str++)))
(gdb) where
#0  0x0807226c in hasher (str=0x10 <Address 0x10 out of bounds>)
    at ../../zsh-4.0/Src/hashtable.c:85
#1  0x080725ab in gethashnode (ht=0xa0bf8e8, 
    nam=0x10 <Address 0x10 out of bounds>) at ../../zsh-4.0/Src/hashtable.c:231
#2  0x080c2580 in getshfunc (nam=0x10 <Address 0x10 out of bounds>)
    at ../../zsh-4.0/Src/utils.c:3007
#3  0x080be9e5 in callhookfunc (name=0x8148760 "precmd", lnklst=0x0, arrayp=1, 
    retval=0x0) at ../../zsh-4.0/Src/utils.c:1185
#4  0x080bebca in preprompt () at ../../zsh-4.0/Src/utils.c:1246
#5  0x0807b1ed in loop (toplevel=1, justonce=0) at ../../zsh-4.0/Src/init.c:121
#6  0x0807e2c6 in zsh_main (argc=2, argv=0xbff0cbd4)
    at ../../zsh-4.0/Src/init.c:1455
#7  0x0804cbea in main (argc=2, argv=0xbff0cbd4) at ../../zsh-4.0/Src/main.c:93


Note that I don't get the crash if I change from precmd_functions to
chpwd_functions, so my suspicion about "add-zsh-hook -D" seems to be
correct.


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

* Re: segfault due to chpwd_function
  2009-12-11 19:01   ` Bart Schaefer
@ 2009-12-11 22:16     ` Peter Stephenson
  0 siblings, 0 replies; 6+ messages in thread
From: Peter Stephenson @ 2009-12-11 22:16 UTC (permalink / raw)
  To: zsh-workers

On Fri, 11 Dec 2009 11:01:58 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Dec 11,  6:41pm, Peter Stephenson wrote:
> }
> } Are you able to post a self-contained function that shows the problem,
> } with no external depenencies?
> 
> This seems to do it:
> 
> autoload -U promptinit
> promptinit
> prompt_chpwd() {
>   ((RANDOM % 2)) && prompt bart || prompt zefram 
> }
> precmd_functions=(prompt_chpwd)

Yes, that's pretty effective.

> Note that I don't get the crash if I change from precmd_functions to
> chpwd_functions, so my suspicion about "add-zsh-hook -D" seems to be
> correct.

Right, this seems to make it go away...

Index: Src/utils.c
===================================================================
RCS file: /cvsroot/zsh/zsh/Src/utils.c,v
retrieving revision 1.234
diff -u -r1.234 utils.c
--- Src/utils.c	3 Nov 2009 10:00:34 -0000	1.234
+++ Src/utils.c	11 Dec 2009 22:14:53 -0000
@@ -1181,6 +1181,7 @@
 	memcpy(arrnam + namlen, HOOK_SUFFIX, HOOK_SUFFIX_LEN);
 
 	if ((arrptr = getaparam(arrnam))) {
+	    arrptr = arrdup(arrptr);
 	    for (; *arrptr; arrptr++) {
 		if ((shfunc = getshfunc(*arrptr))) {
 		    int newret = doshfunc(shfunc, lnklst, 1);

-- 
Peter Stephenson <p.w.stephenson@ntlworld.com>
Web page now at http://homepage.ntlworld.com/p.w.stephenson/


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

end of thread, other threads:[~2009-12-11 22:16 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-11 17:36 segfault due to chpwd_function Joel Bernstein
2009-12-11 17:52 ` Joel Bernstein
2009-12-11 18:11 ` Bart Schaefer
2009-12-11 18:41 ` Peter Stephenson
2009-12-11 19:01   ` Bart Schaefer
2009-12-11 22:16     ` 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).