zsh-workers
 help / color / mirror / code / Atom feed
* Glitch with "eval" and "autoload"
@ 2016-01-23 18:03 Bart Schaefer
  2016-01-24 15:22 ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2016-01-23 18:03 UTC (permalink / raw)
  To: zsh-workers

torch% oops() { eval autoload -X }
torch% oops 
(eval):1: parse error near `"$@"'
torch% functions
'(eval)' () {
        # undefined
        builtin autoload -X
}
oops () {
        eval autoload -X
}


This happens because bin_typeset() gets the name to be autoloaded from
the "scriptname" global, which at the time autoload was invented was not
modified during intervening "eval" and other internal scopes.

The ancient behavior is restored if the "eval_line_no" option is unset.

What's the right way to get the actual function name?  Climbing up the
fstack looking for strcmp(fsptr->name, "(eval)") != 0 seems pretty ugly,
but the "ineval" global isn't sufficient for e.g. "eval eval autoload -X".


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

* Re: Glitch with "eval" and "autoload"
  2016-01-23 18:03 Glitch with "eval" and "autoload" Bart Schaefer
@ 2016-01-24 15:22 ` Peter Stephenson
  2016-01-24 17:17   ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2016-01-24 15:22 UTC (permalink / raw)
  To: zsh-workers

On Sat, 23 Jan 2016 10:03:11 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> What's the right way to get the actual function name?  Climbing up the
> fstack looking for strcmp(fsptr->name, "(eval)") != 0 seems pretty ugly,
> but the "ineval" global isn't sufficient for e.g. "eval eval autoload -X".

You shouldn't need to check the name, rather look for (fstpr->tp ==
FS_FUNC).  That ought to be fairly robust.

pws


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

* Re: Glitch with "eval" and "autoload"
  2016-01-24 15:22 ` Peter Stephenson
@ 2016-01-24 17:17   ` Peter Stephenson
  2016-01-25 16:14     ` Bart Schaefer
  0 siblings, 1 reply; 5+ messages in thread
From: Peter Stephenson @ 2016-01-24 17:17 UTC (permalink / raw)
  To: zsh-workers

On Sun, 24 Jan 2016 15:22:35 +0000
Peter Stephenson <p.w.stephenson@ntlworld.com> wrote:
> You shouldn't need to check the name, rather look for (fstpr->tp ==
> FS_FUNC).  That ought to be fairly robust.

This is a little bit paranoid; the dupstring probably isn't needed
because we already take evasive action for cases like this, and I
don't know of a case where funcname will be NULL.

pws

diff --git a/Src/builtin.c b/Src/builtin.c
index 98ecb09..63f964d 100644
--- a/Src/builtin.c
+++ b/Src/builtin.c
@@ -3142,15 +3142,33 @@ bin_functions(char *name, char **argv, Options ops, int func)
 
 	queue_signals();
 	if (OPT_MINUS(ops,'X')) {
-	    if ((shf = (Shfunc) shfunctab->getnode(shfunctab, scriptname))) {
-		DPUTS(!shf->funcdef,
-		      "BUG: Calling autoload from empty function");
+	    Funcstack fs;
+	    char *funcname = NULL;
+	    for (fs = funcstack; fs; fs = fs->prev) {
+		if (fs->tp == FS_FUNC) {
+		    /*
+		     * dupstring here is paranoia but unlikely to be
+		     * problematic
+		     */
+		    funcname = dupstring(fs->name);
+		    break;
+		}
+	    }
+	    if (!funcname)
+	    {
+		zerrnam(name, "bad autoload");
+		ret = 1;
 	    } else {
-		shf = (Shfunc) zshcalloc(sizeof *shf);
-		shfunctab->addnode(shfunctab, ztrdup(scriptname), shf);
+		if ((shf = (Shfunc) shfunctab->getnode(shfunctab, funcname))) {
+		    DPUTS(!shf->funcdef,
+			  "BUG: Calling autoload from empty function");
+		} else {
+		    shf = (Shfunc) zshcalloc(sizeof *shf);
+		    shfunctab->addnode(shfunctab, ztrdup(funcname), shf);
+		}
+		shf->node.flags = on;
+		ret = eval_autoload(shf, funcname, ops, func);
 	    }
-	    shf->node.flags = on;
-	    ret = eval_autoload(shf, scriptname, ops, func);
 	} else {
 	    if (OPT_ISSET(ops,'U') && !OPT_ISSET(ops,'u'))
 		on &= ~PM_UNDEFINED;
diff --git a/Test/C04funcdef.ztst b/Test/C04funcdef.ztst
index 0951e2c..496577f 100644
--- a/Test/C04funcdef.ztst
+++ b/Test/C04funcdef.ztst
@@ -308,6 +308,19 @@
 ?(eval):6: command not found: firstfn1
 ?(eval):7: command not found: secondfn1
 
+  (
+    fpath=(.)
+    print  "print oops was successfully autoloaded" >oops
+    oops() { eval autoload -X }
+    oops
+    which -x2 oops
+  )
+0:autoload containing eval
+>oops was successfully autoloaded
+>oops () {
+>  print oops was successfully autoloaded
+>}
+
 %clean
 
  rm -f file.in file.out


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

* Re: Glitch with "eval" and "autoload"
  2016-01-24 17:17   ` Peter Stephenson
@ 2016-01-25 16:14     ` Bart Schaefer
  2016-01-25 16:21       ` Peter Stephenson
  0 siblings, 1 reply; 5+ messages in thread
From: Bart Schaefer @ 2016-01-25 16:14 UTC (permalink / raw)
  To: zsh-workers

On Jan 24,  5:17pm, Peter Stephenson wrote:
}
} > You shouldn't need to check the name, rather look for (fstpr->tp ==
} > FS_FUNC).  That ought to be fairly robust.

The patch in 37765 looks fine to me, do you plan to push it?


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

* Re: Glitch with "eval" and "autoload"
  2016-01-25 16:14     ` Bart Schaefer
@ 2016-01-25 16:21       ` Peter Stephenson
  0 siblings, 0 replies; 5+ messages in thread
From: Peter Stephenson @ 2016-01-25 16:21 UTC (permalink / raw)
  To: zsh-workers

On Mon, 25 Jan 2016 08:14:35 -0800
Bart Schaefer <schaefer@brasslantern.com> wrote:
> On Jan 24,  5:17pm, Peter Stephenson wrote:
> }
> } > You shouldn't need to check the name, rather look for (fstpr->tp ==
> } > FS_FUNC).  That ought to be fairly robust.
> 
> The patch in 37765 looks fine to me, do you plan to push it?

Yes, I'll do it here otherwise it'll be tomorrow evening...

pws


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

end of thread, other threads:[~2016-01-25 16:21 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-01-23 18:03 Glitch with "eval" and "autoload" Bart Schaefer
2016-01-24 15:22 ` Peter Stephenson
2016-01-24 17:17   ` Peter Stephenson
2016-01-25 16:14     ` Bart Schaefer
2016-01-25 16:21       ` 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).