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 -*-
#-
# 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
expected-stdout:
@(#)MIRBSD KSH R51 2015/07/10
@(#)MIRBSD KSH R51 2015/08/13
description:
Check version of shell.
stdin:
@ -39,7 +39,7 @@ name: KSH_VERSION
category: shell:legacy-no
---
expected-stdout:
@(#)LEGACY KSH R51 2015/07/10
@(#)LEGACY KSH R51 2015/08/13
description:
Check version of legacy shell.
stdin:

10
funcs.c
View File

@ -38,7 +38,7 @@
#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
/*
@ -1374,7 +1374,7 @@ c_kill(const char **wp)
for (; wp[i]; i++) {
if (!bi_getn(wp[i], &n))
return (1);
#if (ksh_NSIG < 128)
#if (ksh_NSIG <= 128)
if (n > 128 && n < 128 + ksh_NSIG)
n -= 128;
#endif
@ -1385,7 +1385,7 @@ c_kill(const char **wp)
}
} else {
ssize_t w, mess_cols = 0, mess_octs = 0;
int j = ksh_NSIG;
int j = ksh_NSIG - 1;
struct kill_info ki = { 0, 0 };
do {
@ -2293,7 +2293,7 @@ int
c_trap(const char **wp)
{
Trap *p = sigtraps;
int i = ksh_NSIG + 1;
int i = ksh_NSIG;
const char *s;
if (ksh_getopt(wp, &builtin_opt, null) == '?')
@ -2308,7 +2308,7 @@ c_trap(const char **wp)
shprintf(" %s\n", p->name);
}
++p;
} while (--i);
} while (i--);
return (0);
}

57
jobs.c
View File

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

12
sh.h
View File

@ -172,9 +172,9 @@
#endif
#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
#define MKSH_VERSION "R51 2015/07/10"
#define MKSH_VERSION "R51 2015/08/13"
/* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES
@ -337,15 +337,15 @@ struct rusage {
/* determine ksh_NSIG: first, use the traditional definitions */
#undef ksh_NSIG
#if defined(NSIG)
#define ksh_NSIG NSIG
#define ksh_NSIG (NSIG)
#elif defined(_NSIG)
#define ksh_NSIG _NSIG
#define ksh_NSIG (_NSIG)
#elif defined(SIGMAX)
#define ksh_NSIG (SIGMAX + 1)
#elif defined(_SIGMAX)
#define ksh_NSIG (_SIGMAX + 1)
#elif defined(NSIG_MAX)
#define ksh_NSIG NSIG_MAX
#define ksh_NSIG (NSIG_MAX)
#else
# error Please have your platform define NSIG.
#endif
@ -367,7 +367,7 @@ struct rusage {
#else
/* since its usable, prefer it */
#undef ksh_NSIG
#define ksh_NSIG NSIG_MAX
#define ksh_NSIG (NSIG_MAX)
#endif
/* if NSIG_MAX is now still defined, use sysconf(_SC_NSIG) at runtime */
#endif