[git commit] ash: allow "trap NUM [SIG]..." syntax

Denys Vlasenko vda.linux at googlemail.com
Tue Jul 25 18:06:17 UTC 2017


commit: https://git.busybox.net/busybox/commit/?id=86981e3ad2d03e77d1f668ac1603a041be448dae
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

While at it, make get_signum() return -1 for numeric strings >= NSIG.

function                                             old     new   delta
trapcmd                                              292     306     +14
get_signum                                           295     300      +5
builtin_trap                                         413     412      -1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 19/-1)              Total: 18 bytes

Signed-off-by: Denys Vlasenko <vda.linux at googlemail.com>
---
 libbb/u_signal_names.c | 2 +-
 procps/kill.c          | 2 +-
 shell/ash.c            | 9 +++++++--
 shell/hush.c           | 2 +-
 4 files changed, 10 insertions(+), 5 deletions(-)

diff --git a/libbb/u_signal_names.c b/libbb/u_signal_names.c
index bf984a4..b82a706 100644
--- a/libbb/u_signal_names.c
+++ b/libbb/u_signal_names.c
@@ -143,7 +143,7 @@ int FAST_FUNC get_signum(const char *name)
 	unsigned i;
 
 	i = bb_strtou(name, NULL, 10);
-	if (!errno)
+	if (!errno && i < NSIG) /* for shells, we allow 0 too */
 		return i;
 	if (strncasecmp(name, "SIG", 3) == 0)
 		name += 3;
diff --git a/procps/kill.c b/procps/kill.c
index 5cff244..09beefb 100644
--- a/procps/kill.c
+++ b/procps/kill.c
@@ -188,7 +188,7 @@ int kill_main(int argc UNUSED_PARAM, char **argv)
 		arg = *++argv;
 	} /* else it must be -SIG */
 	signo = get_signum(arg);
-	if (signo < 0) { /* || signo > MAX_SIGNUM ? */
+	if (signo < 0) {
 		bb_error_msg("bad signal name '%s'", arg);
 		return EXIT_FAILURE;
 	}
diff --git a/shell/ash.c b/shell/ash.c
index b4b0d52..42e14cb 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -12981,13 +12981,18 @@ trapcmd(int argc UNUSED_PARAM, char **argv UNUSED_PARAM)
 		return 0;
 	}
 
+	/* Why the second check?
+	 * "trap NUM [sig2]..." is the same as "trap - NUM [sig2]..."
+	 * In this case, NUM is signal no, not an action.
+	 */
 	action = NULL;
-	if (ap[1])
+	if (ap[1] && !is_number(ap[0]))
 		action = *ap++;
+
 	exitcode = 0;
 	while (*ap) {
 		signo = get_signum(*ap);
-		if (signo < 0 || signo >= NSIG) {
+		if (signo < 0) {
 			/* Mimic bash message exactly */
 			ash_msg("%s: invalid signal specification", *ap);
 			exitcode = 1;
diff --git a/shell/hush.c b/shell/hush.c
index f9dad07..11b33f4 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -9745,7 +9745,7 @@ static int FAST_FUNC builtin_trap(char **argv)
 			sighandler_t handler;
 
 			sig = get_signum(*argv++);
-			if (sig < 0 || sig >= NSIG) {
+			if (sig < 0) {
 				ret = EXIT_FAILURE;
 				/* Mimic bash message exactly */
 				bb_error_msg("trap: %s: invalid signal specification", argv[-1]);


More information about the busybox-cvs mailing list