From 97054a769fb4d9614090ac3e2b979e089c5b84c6 Mon Sep 17 00:00:00 2001 From: Nate Lawson Date: Thu, 12 Feb 2004 05:03:37 +0000 Subject: [PATCH] Handle proper formatting and a buffer overrun when running an old sh on a system that has sys_nsig > NSIG (i.e. when libc is upgraded.) Submitted by: Matt Dillon Reviewed by: bde --- bin/sh/trap.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) diff --git a/bin/sh/trap.c b/bin/sh/trap.c index f00c6d516cab..23f1885af4d5 100644 --- a/bin/sh/trap.c +++ b/bin/sh/trap.c @@ -89,6 +89,8 @@ static int getsigaction(int, sig_t *); /* * Map a string to a signal number. + * + * Note: the signal number may exceed NSIG. */ static int sigstring_to_signum(char *sig) @@ -120,14 +122,24 @@ sigstring_to_signum(char *sig) static void printsignals(void) { - int n; + int n, outlen; + outlen = 0; for (n = 1; n < sys_nsig; n++) { - out1fmt("%s", sys_signame[n]); - if (n == (NSIG / 2) || n == (NSIG - 1)) + if (sys_signame[n]) { + out1fmt("%s", sys_signame[n]); + outlen += strlen(sys_signame[n]); + } else { + out1fmt("%d", n); + outlen += 3; /* good enough */ + } + ++outlen; + if (outlen > 70 || n == sys_nsig - 1) { out1str("\n"); - else + outlen = 0; + } else { out1c(' '); + } } } @@ -143,9 +155,18 @@ trapcmd(int argc, char **argv) if (argc <= 1) { for (signo = 0 ; signo < sys_nsig ; signo++) { - if (trap[signo] != NULL) - out1fmt("trap -- '%s' %s\n", trap[signo], - (signo) ? sys_signame[signo] : "exit"); + if (signo < NSIG && trap[signo] != NULL) { + if (signo == 0) { + out1fmt("trap -- '%s' %s\n", + trap[signo], "exit"); + } else if (sys_signame[signo]) { + out1fmt("trap -- '%s' %s\n", + trap[signo], sys_signame[signo]); + } else { + out1fmt("trap -- '%s' %d\n", + trap[signo], signo); + } + } } return 0; }