* [BUG] crash when I unset and redefine a function within itself
@ 2024-10-16 17:55 Gilles
2024-10-16 19:54 ` Bart Schaefer
0 siblings, 1 reply; 2+ messages in thread
From: Gilles @ 2024-10-16 17:55 UTC (permalink / raw)
To: zsh-workers
[-- Attachment #1: Type: text/plain, Size: 747 bytes --]
With zsh 5.9 from Ubuntu 24.04 x86_64, or with the latest version from git
(383526da422cf1c962d9be7e9e6ac166e226bf2b) compiled locally, the shell
segfaults when I run a function that undefines then redefines itself.
zsh -c 'foo () { unset -f foo; foo () { echo redefined; }; }; foo'
[1] 89793 segmentation fault (core dumped) zsh -c 'foo () { unset -f
foo; foo () { echo redefined; }; }; foo'
This works with zsh 5.8.1 on Ubuntu 22.04 and older versions.
Workaround: if you redefine a function within itself, make sure not to
unset it first.
This is simplified from real code in my .zshrc that decides on first use
whether the command name foo should invoke the external command directly or
be a function that wraps around it.
--
Gilles
[-- Attachment #2: Type: text/html, Size: 938 bytes --]
^ permalink raw reply [flat|nested] 2+ messages in thread
* Re: [BUG] crash when I unset and redefine a function within itself
2024-10-16 17:55 [BUG] crash when I unset and redefine a function within itself Gilles
@ 2024-10-16 19:54 ` Bart Schaefer
0 siblings, 0 replies; 2+ messages in thread
From: Bart Schaefer @ 2024-10-16 19:54 UTC (permalink / raw)
To: Gilles; +Cc: zsh-workers
On Wed, Oct 16, 2024 at 10:56 AM Gilles <gilles.usenet@gmail.com> wrote:
>
> zsh -c 'foo () { unset -f foo; foo () { echo redefined; }; }; foo'
> [1] 89793 segmentation fault (core dumped) zsh -c 'foo () { unset -f foo; foo () { echo redefined; }; }; foo'
>
> This works with zsh 5.8.1 on Ubuntu 22.04 and older versions.
This is due to commit ca6f4466e661f185d083e09c55fb93d16e0736cc:
45131: Make a function that redefines itself preserve its tracedness.
Fix is straightforward; "tracedness" can't be preserved across unfunction:
diff --git a/Src/exec.c b/Src/exec.c
index 8aa7466f5..bc07e8c39 100644
--- a/Src/exec.c
+++ b/Src/exec.c
@@ -5504,7 +5504,8 @@ execfuncdef(Estate state, Eprog redir_prog)
if (funcstack && funcstack->tp == FS_FUNC &&
!strcmp(s, funcstack->name)) {
Shfunc old = ((Shfunc)shfunctab->getnode(shfunctab, s));
- shf->node.flags |= old->node.flags & (PM_TAGGED|PM_TAGGED_LOCAL);
+ if (old)
+ shf->node.flags |= old->node.flags & (PM_TAGGED|PM_TAGGED_LOCAL);
}
shfunctab->addnode(shfunctab, ztrdup(s), shf);
}
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2024-10-16 19:55 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-10-16 17:55 [BUG] crash when I unset and redefine a function within itself Gilles
2024-10-16 19:54 ` Bart Schaefer
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).