Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/bin/sh When we are about to execute something, and the traps...



details:   https://anonhg.NetBSD.org/src/rev/9b2fefaa4b2d
branches:  trunk
changeset: 447689:9b2fefaa4b2d
user:      kre <kre%NetBSD.org@localhost>
date:      Mon Jan 21 14:18:59 2019 +0000

description:
When we are about to execute something, and the traps are invalid
(which means this is the very first execution in a new subshell)
clear the traps completely, unless the command is "trap".   We were
allowing any special builtin, which was probably harmless, but not
intended.

Also (though not required) permit "command trap" and "eval trap"
and combinations thereof, because they might be useful, and there is
no particular reason why not.   This is all a part of making t=$(trap)
work as POSIX requires, but almost nothing beyond that.  The "trap"
command must be alone (modulo eval and command) in the subshell for
the exception to apply, no t=$(trap; echo) or anything like that.

Martijn Dekker asked for "command trap" to work (no idea why though,
it converts "trap" from being a special builtin, to a normal one,
which means an error won't cause the shell to exit ... if there's
an error, the "trap" command won't do anything useful, and as we
permit no more commands (for this special treatment) the shell is
going to exit anyway, this difference is not really significant.

diffstat:

 bin/sh/eval.c |  27 ++++++++++++++++++++++++---
 1 files changed, 24 insertions(+), 3 deletions(-)

diffs (48 lines):

diff -r 1a0ea8c871ec -r 9b2fefaa4b2d bin/sh/eval.c
--- a/bin/sh/eval.c     Mon Jan 21 14:09:24 2019 +0000
+++ b/bin/sh/eval.c     Mon Jan 21 14:18:59 2019 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: eval.c,v 1.169 2019/01/09 10:57:43 kre Exp $   */
+/*     $NetBSD: eval.c,v 1.170 2019/01/21 14:18:59 kre Exp $   */
 
 /*-
  * Copyright (c) 1993
@@ -37,7 +37,7 @@
 #if 0
 static char sccsid[] = "@(#)eval.c     8.9 (Berkeley) 6/8/95";
 #else
-__RCSID("$NetBSD: eval.c,v 1.169 2019/01/09 10:57:43 kre Exp $");
+__RCSID("$NetBSD: eval.c,v 1.170 2019/01/21 14:18:59 kre Exp $");
 #endif
 #endif /* not lint */
 
@@ -1031,7 +1031,28 @@
                        cmdentry.cmdtype = CMDBUILTIN;
        }
 
-       if (traps_invalid && cmdentry.cmdtype != CMDSPLBLTIN)
+       /*
+        * When traps are invalid, we permit the following:
+        *      trap
+        *      command trap
+        *      eval trap
+        *      command eval trap
+        *      eval command trap
+        * without zapping the traps completely, in all other cases we do.
+        *
+        * The test here permits eval "anything" but when evalstring() comes
+        * back here again, the "anything" will be validated.
+        * This means we can actually do:
+        *      eval eval eval command eval eval command trap
+        * as long as we end up with just "trap"
+        *
+        * We permit "command" by allowing CMDBUILTIN as well as CMDSPLBLTIN
+        *
+        * trapcmd() takes care of doing free_traps() if it is needed there.
+        */
+       if (traps_invalid &&
+           ((cmdentry.cmdtype!=CMDSPLBLTIN && cmdentry.cmdtype!=CMDBUILTIN) ||
+            (cmdentry.u.bltin != trapcmd && cmdentry.u.bltin != evalcmd)))
                free_traps();
 
        /* Fork off a child process if necessary. */



Home | Main Index | Thread Index | Old Index