zsh-workers
 help / color / mirror / code / Atom feed
From: Daniel Shahaf <d.s@daniel.shahaf.name>
To: Zsh hackers list <zsh-workers@zsh.org>
Subject: Re: local_traps doesn't restore traps set from functions
Date: Sat, 9 May 2020 19:51:02 +0000	[thread overview]
Message-ID: <20200509195102.6d6f5095@tarpaulin.shahaf.local2> (raw)
In-Reply-To: <20200509154210.401d82d9@tarpaulin.shahaf.local2>

Daniel Shahaf wrote on Sat, 09 May 2020 15:42 +0000:
> Peter Stephenson wrote on Sat, 09 May 2020 16:24 +0100:
> > > On 07 May 2020 at 22:21 Daniel Shahaf <d.s@daniel.shahaf.name> wrote:
> > > Roman Perepelitsa wrote on Wed, 06 May 2020 15:31 +0200:    
> > > > It appears that local_traps doesn't restore traps that were originally
> > > > set from functions.    
> > > 
> > > Precisely.
> > > 
> > > The following patch fixes it:    
> > 
> > OK, so the point here is that in this case the trap we're fiddling with
> > is not going out of scope, nor having something else restored over it,
> > it just happens to have been created inside a function.  At this
> > point, that fact becomes irrelevant, so we simply massage the information
> > to make it behave as if it had been created at the current (new) function
> > depth.
> > 
> > That seems fine --- my only comment would be it would probably be sensible
> > to insert some edited version of the previous paragraph as I for one
> > am almost certain to wonder what's going on if I encounter this code
> > in the future.  
> 
> Will do.  Thanks for the review!

Here's an interdiff, to be applied on top of 45790.

Test will follow separately.

diff --git a/Src/signals.c b/Src/signals.c
index 5d0bae7f5..56b3071de 100644
--- a/Src/signals.c
+++ b/Src/signals.c
@@ -30,8 +30,16 @@
 #include "zsh.mdh"
 #include "signals.pro"
  
-/* Array describing the state of each signal: an element contains *
- * 0 for the default action or some ZSIG_* flags ored together.   */
+/* 
+ * Array describing the state of each signal: an element contains
+ * 0 for the default action or some ZSIG_* flags ored together.
+ *
+ * The high bits (see ZSIG_SHIFT) identify the locallevel that owns the trap.
+ * Whenever execution returns to that locallevel (which is never larger/deeper
+ * than the current locallevel) from deeper levels, any function-scoped traps
+ * that shadow this trap will have gone out of scope.  See the LOCALTRAPS
+ * condition in removetrap().
+ */
 
 /**/
 mod_export int sigtrapped[VSIGCOUNT];
@@ -997,9 +1005,12 @@ removetrap(int sig)
     queue_signals();
     trapped = sigtrapped[sig];
     /*
+     * Save the trap if we should restore it at the end of the current function.
+     *
      * Note that we save the trap here even if there isn't an existing
-     * one, to aid in removing this one.  However, if there's
-     * already one at the current locallevel we just overwrite it.
+     * one, to aid in removing this one.  We also save the trap if it's owned
+     * by someone up the callstack.  However, if the trap is owned by the
+     * current locallevel we just overwrite it.
      *
      * Note we save EXIT traps based on the *current* setting of
      * POSIXTRAPS --- so if there is POSIX EXIT trap set but
@@ -1062,6 +1073,16 @@ removetrap(int sig)
     return NULL;
 }
 
+/*
+ * Return the value of ARG after setting all but the N least significant bits
+ * to zero.
+ */
+static unsigned int
+low_N_bits_of(unsigned int arg, unsigned char N)
+{
+    return arg & ((1u << N)-1u);
+}
+
 /**/
 void
 starttrapscope(void)
@@ -1114,6 +1135,9 @@ endtrapscope(void)
 	sigtrapped[SIGEXIT] = 0;
     }
 
+    /*
+     * Restore traps from savetraps to sigtrapped.
+     */
     if (savetraps) {
 	while ((ln = firstnode(savetraps)) &&
 	       (st = (struct savetrap *) ln->dat) &&
@@ -1122,6 +1146,8 @@ endtrapscope(void)
 
 	    remnode(savetraps, ln);
 
+	    /* ### This doesn't mask ZSIG_IGNORED out of the boolean check,
+	     * ### presumably because checking st->list is sufficient? */
 	    if (st->flags && (st->list != NULL)) {
 		/* prevent settrap from saving this */
 		dontsavetrap++;
@@ -1154,7 +1180,11 @@ endtrapscope(void)
 	}
     }
 
-    /* Fixup locallevel of signals. */
+    /*
+     * If the function that's in the process of returning set a global trap,
+     * that trap is now owned by that function's caller.  Update sigtrapped
+     * accordingly.
+     */
     {
 	int i;
 	for (i = 0; i < VSIGCOUNT; ++i) {
@@ -1170,7 +1200,7 @@ endtrapscope(void)
 		 * Keep the low ZSIG_SHIFT bits unchanged.
 		 */
 		sigtrapped[i] =
-		    (sigtrapped[i] & ((1u << ZSIG_SHIFT) - 1u))
+		    low_N_bits_of(sigtrapped[i], ZSIG_SHIFT)
 		    |
 		    (locallevel << ZSIG_SHIFT);
 	    }

      reply	other threads:[~2020-05-09 19:51 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-06 13:31 Roman Perepelitsa
2020-05-07 21:21 ` Daniel Shahaf
2020-05-08  6:51   ` Roman Perepelitsa
2020-05-09 14:14     ` Daniel Shahaf
2020-05-09 15:24   ` Peter Stephenson
2020-05-09 15:42     ` Daniel Shahaf
2020-05-09 19:51       ` Daniel Shahaf [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=20200509195102.6d6f5095@tarpaulin.shahaf.local2 \
    --to=d.s@daniel.shahaf.name \
    --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).