be more careful with signals and errorlevels; bounds-check them better;

don’t trust the OS as POSuX comes up with more-than-8-bit exit codes now;
also, one more int → bool
This commit is contained in:
tg 2015-08-13 21:38:19 +00:00
parent 9d30e9c9d6
commit c674e71377
4 changed files with 48 additions and 37 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.703 2015/07/10 19:36:31 tg Exp $ # $MirOS: src/bin/mksh/check.t,v 1.704 2015/08/13 21:38:15 tg Exp $
# -*- mode: sh -*- # -*- mode: sh -*-
#- #-
# Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, # Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
@ -30,7 +30,7 @@
# (2013/12/02 20:39:44) http://openbsd.cs.toronto.edu/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date # (2013/12/02 20:39:44) http://openbsd.cs.toronto.edu/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date
expected-stdout: expected-stdout:
@(#)MIRBSD KSH R51 2015/07/10 @(#)MIRBSD KSH R51 2015/08/13
description: description:
Check version of shell. Check version of shell.
stdin: stdin:
@ -39,7 +39,7 @@ name: KSH_VERSION
category: shell:legacy-no category: shell:legacy-no
--- ---
expected-stdout: expected-stdout:
@(#)LEGACY KSH R51 2015/07/10 @(#)LEGACY KSH R51 2015/08/13
description: description:
Check version of legacy shell. Check version of legacy shell.
stdin: stdin:

10
funcs.c
View File

@ -38,7 +38,7 @@
#endif #endif
#endif #endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.281 2015/08/13 21:04:11 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.282 2015/08/13 21:38:17 tg Exp $");
#if HAVE_KILLPG #if HAVE_KILLPG
/* /*
@ -1374,7 +1374,7 @@ c_kill(const char **wp)
for (; wp[i]; i++) { for (; wp[i]; i++) {
if (!bi_getn(wp[i], &n)) if (!bi_getn(wp[i], &n))
return (1); return (1);
#if (ksh_NSIG < 128) #if (ksh_NSIG <= 128)
if (n > 128 && n < 128 + ksh_NSIG) if (n > 128 && n < 128 + ksh_NSIG)
n -= 128; n -= 128;
#endif #endif
@ -1385,7 +1385,7 @@ c_kill(const char **wp)
} }
} else { } else {
ssize_t w, mess_cols = 0, mess_octs = 0; ssize_t w, mess_cols = 0, mess_octs = 0;
int j = ksh_NSIG; int j = ksh_NSIG - 1;
struct kill_info ki = { 0, 0 }; struct kill_info ki = { 0, 0 };
do { do {
@ -2293,7 +2293,7 @@ int
c_trap(const char **wp) c_trap(const char **wp)
{ {
Trap *p = sigtraps; Trap *p = sigtraps;
int i = ksh_NSIG + 1; int i = ksh_NSIG;
const char *s; const char *s;
if (ksh_getopt(wp, &builtin_opt, null) == '?') if (ksh_getopt(wp, &builtin_opt, null) == '?')
@ -2308,7 +2308,7 @@ c_trap(const char **wp)
shprintf(" %s\n", p->name); shprintf(" %s\n", p->name);
} }
++p; ++p;
} while (--i); } while (i--);
return (0); return (0);
} }

57
jobs.c
View File

@ -23,7 +23,7 @@
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.112 2015/04/19 14:40:09 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.113 2015/08/13 21:38:19 tg Exp $");
#if HAVE_KILLPG #if HAVE_KILLPG
#define mksh_killpg killpg #define mksh_killpg killpg
@ -215,11 +215,14 @@ j_init(void)
static int static int
proc_errorlevel(Proc *p) proc_errorlevel(Proc *p)
{ {
int termsig;
switch (p->state) { switch (p->state) {
case PEXITED: case PEXITED:
return (WEXITSTATUS(p->status)); return ((WEXITSTATUS(p->status)) & 255);
case PSIGNALLED: case PSIGNALLED:
return (128 + WTERMSIG(p->status)); termsig = WTERMSIG(p->status);
return ((termsig < 1 || termsig > 127) ? 255 : 128 + termsig);
default: default:
return (0); return (0);
} }
@ -1223,14 +1226,14 @@ j_waitj(Job *j,
* even when not monitoring, but this doesn't make sense since * even when not monitoring, but this doesn't make sense since
* a tty generated ^C goes to the whole process group) * a tty generated ^C goes to the whole process group)
*/ */
{ if (Flag(FMONITOR) && j->state == PSIGNALLED &&
int status; WIFSIGNALED(j->last_proc->status)) {
int termsig;
status = j->last_proc->status; if ((termsig = WTERMSIG(j->last_proc->status)) > 0 &&
if (Flag(FMONITOR) && j->state == PSIGNALLED && termsig < ksh_NSIG &&
WIFSIGNALED(status) && (sigtraps[termsig].flags & TF_TTY_INTR))
(sigtraps[WTERMSIG(status)].flags & TF_TTY_INTR)) trapsig(termsig);
trapsig(WTERMSIG(status));
} }
#endif #endif
} }
@ -1527,7 +1530,7 @@ j_print(Job *j, int how, struct shf *shf)
Proc *p; Proc *p;
int state; int state;
int status; int status;
int coredumped; bool coredumped;
char jobchar = ' '; char jobchar = ' ';
char buf[64]; char buf[64];
const char *filler; const char *filler;
@ -1551,41 +1554,49 @@ j_print(Job *j, int how, struct shf *shf)
jobchar = '-'; jobchar = '-';
for (p = j->proc_list; p != NULL;) { for (p = j->proc_list; p != NULL;) {
coredumped = 0; coredumped = false;
switch (p->state) { switch (p->state) {
case PRUNNING: case PRUNNING:
memcpy(buf, "Running", 8); memcpy(buf, "Running", 8);
break; break;
case PSTOPPED: case PSTOPPED: {
strlcpy(buf, sigtraps[WSTOPSIG(p->status)].mess, int stopsig = WSTOPSIG(p->status);
sizeof(buf));
strlcpy(buf, stopsig > 0 && stopsig < ksh_NSIG ?
sigtraps[stopsig].mess : "Stopped", sizeof(buf));
break; break;
case PEXITED: }
case PEXITED: {
int exitstatus = (WEXITSTATUS(p->status)) & 255;
if (how == JP_SHORT) if (how == JP_SHORT)
buf[0] = '\0'; buf[0] = '\0';
else if (WEXITSTATUS(p->status) == 0) else if (exitstatus == 0)
memcpy(buf, "Done", 5); memcpy(buf, "Done", 5);
else else
shf_snprintf(buf, sizeof(buf), "Done (%d)", shf_snprintf(buf, sizeof(buf), "Done (%d)",
WEXITSTATUS(p->status)); exitstatus);
break; break;
case PSIGNALLED: }
case PSIGNALLED: {
int termsig = WTERMSIG(p->status);
#ifdef WCOREDUMP #ifdef WCOREDUMP
if (WCOREDUMP(p->status)) if (WCOREDUMP(p->status))
coredumped = 1; coredumped = true;
#endif #endif
/* /*
* kludge for not reporting 'normal termination * kludge for not reporting 'normal termination
* signals' (i.e. SIGINT, SIGPIPE) * signals' (i.e. SIGINT, SIGPIPE)
*/ */
if (how == JP_SHORT && !coredumped && if (how == JP_SHORT && !coredumped &&
(WTERMSIG(p->status) == SIGINT || (termsig == SIGINT || termsig == SIGPIPE)) {
WTERMSIG(p->status) == SIGPIPE)) {
buf[0] = '\0'; buf[0] = '\0';
} else } else
strlcpy(buf, sigtraps[WTERMSIG(p->status)].mess, strlcpy(buf, termsig > 0 && termsig < ksh_NSIG ?
sigtraps[termsig].mess : "Signalled",
sizeof(buf)); sizeof(buf));
break; break;
}
default: default:
buf[0] = '\0'; buf[0] = '\0';
} }

12
sh.h
View File

@ -172,9 +172,9 @@
#endif #endif
#ifdef EXTERN #ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.739 2015/07/10 19:36:37 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/sh.h,v 1.740 2015/08/13 21:38:19 tg Exp $");
#endif #endif
#define MKSH_VERSION "R51 2015/07/10" #define MKSH_VERSION "R51 2015/08/13"
/* arithmetic types: C implementation */ /* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES #if !HAVE_CAN_INTTYPES
@ -337,15 +337,15 @@ struct rusage {
/* determine ksh_NSIG: first, use the traditional definitions */ /* determine ksh_NSIG: first, use the traditional definitions */
#undef ksh_NSIG #undef ksh_NSIG
#if defined(NSIG) #if defined(NSIG)
#define ksh_NSIG NSIG #define ksh_NSIG (NSIG)
#elif defined(_NSIG) #elif defined(_NSIG)
#define ksh_NSIG _NSIG #define ksh_NSIG (_NSIG)
#elif defined(SIGMAX) #elif defined(SIGMAX)
#define ksh_NSIG (SIGMAX + 1) #define ksh_NSIG (SIGMAX + 1)
#elif defined(_SIGMAX) #elif defined(_SIGMAX)
#define ksh_NSIG (_SIGMAX + 1) #define ksh_NSIG (_SIGMAX + 1)
#elif defined(NSIG_MAX) #elif defined(NSIG_MAX)
#define ksh_NSIG NSIG_MAX #define ksh_NSIG (NSIG_MAX)
#else #else
# error Please have your platform define NSIG. # error Please have your platform define NSIG.
#endif #endif
@ -367,7 +367,7 @@ struct rusage {
#else #else
/* since its usable, prefer it */ /* since its usable, prefer it */
#undef ksh_NSIG #undef ksh_NSIG
#define ksh_NSIG NSIG_MAX #define ksh_NSIG (NSIG_MAX)
#endif #endif
/* if NSIG_MAX is now still defined, use sysconf(_SC_NSIG) at runtime */ /* if NSIG_MAX is now still defined, use sysconf(_SC_NSIG) at runtime */
#endif #endif