• fix memory leaks found by coverity

from netbsd via oksh
  we had the NULL pointer deref already fixed
• avoid a bogus not-setting the return value of edit.c:x_file_glob()
  introduced by the above change in oksh
• escape ? as well (but not ] because that’s wrong)
  reminded by cbiere@netbsd via oksh
• Unsetting a non-existent variable is not an error. See
  http://www.opengroup.org/onlinepubs/009695399/utilities/unset.html
  report from Arkadiusz Miskiewicz; fixed based on
  http://cvs.pld-linux.org diff via oksh but modified slightly
• Be more smart waiting for input for non-interactive scripts.  Fix
  based on a diff from debian:  see their bug#296446 (via oksh)
  modified slightly
  this also fixes cnuke@’s “mksh busy loop” problem, for which I never
  received a bug report, but the Debian bug page contains a set of two
  scripts to reproduce this before (and no longer after) this commit
• some KNF
• bump version
This commit is contained in:
tg 2007-09-09 18:06:42 +00:00
parent fe65f616e4
commit c1c939e340
9 changed files with 78 additions and 57 deletions

View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.137 2007/09/07 23:57:14 tg Exp $ # $MirOS: src/bin/mksh/check.t,v 1.138 2007/09/09 18:06:38 tg Exp $
# $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: bksl-nl.t,v 1.2 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $ # $OpenBSD: history.t,v 1.5 2001/01/28 23:04:56 niklas Exp $
# $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $ # $OpenBSD: read.t,v 1.3 2003/03/10 03:48:16 david Exp $
@ -7,7 +7,7 @@
# http://www.research.att.com/~gsf/public/ifs.sh # http://www.research.att.com/~gsf/public/ifs.sh
expected-stdout: expected-stdout:
@(#)MIRBSD KSH R31 2007/08/19 @(#)MIRBSD KSH R31 2007/09/09
description: description:
Check version of shell. Check version of shell.
category: pdksh category: pdksh

28
edit.c
View File

@ -1,11 +1,11 @@
/* $OpenBSD: edit.c,v 1.31 2005/12/11 20:31:21 otto Exp $ */ /* $OpenBSD: edit.c,v 1.33 2007/08/02 10:50:25 fgsch Exp $ */
/* $OpenBSD: edit.h,v 1.8 2005/03/28 21:28:22 deraadt Exp $ */ /* $OpenBSD: edit.h,v 1.8 2005/03/28 21:28:22 deraadt Exp $ */
/* $OpenBSD: emacs.c,v 1.40 2006/07/10 17:12:41 beck Exp $ */ /* $OpenBSD: emacs.c,v 1.41 2007/08/02 10:50:25 fgsch Exp $ */
/* $OpenBSD: vi.c,v 1.23 2006/04/10 14:38:59 jaredy Exp $ */ /* $OpenBSD: vi.c,v 1.23 2006/04/10 14:38:59 jaredy Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.111 2007/08/18 01:20:27 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/edit.c,v 1.112 2007/09/09 18:06:39 tg Exp $");
/* tty driver characters we are interested in */ /* tty driver characters we are interested in */
typedef struct { typedef struct {
@ -325,12 +325,14 @@ x_file_glob(int flags __unused, const char *str, int slen, char ***wordsp)
stat(words[0], &statb) < 0) || stat(words[0], &statb) < 0) ||
words[0][0] == '\0') { words[0][0] == '\0') {
x_free_words(nwords, words); x_free_words(nwords, words);
words = NULL;
nwords = 0; nwords = 0;
} }
} }
afree(toglob, ATEMP); afree(toglob, ATEMP);
*wordsp = nwords ? words : NULL; if ((*wordsp = nwords ? words : NULL) == NULL && words != NULL)
x_free_words(nwords, words);
return nwords; return nwords;
} }
@ -729,7 +731,7 @@ x_escape(const char *s, size_t len, int (*putbuf_func)(const char *, size_t))
int rval = 0; int rval = 0;
while (wlen - add > 0) while (wlen - add > 0)
if (vstrchr("\\$()[{}*&;#|<>\"'`", s[add]) || if (vstrchr("\\$()[?{}*&;#|<>\"'`", s[add]) ||
vstrchr(ifs, s[add])) { vstrchr(ifs, s[add])) {
if (putbuf_func(s, add) != 0) { if (putbuf_func(s, add) != 0) {
rval = -1; rval = -1;
@ -1128,7 +1130,7 @@ static int x_search(char *, int, int);
static int x_match(char *, char *); static int x_match(char *, char *);
static void x_redraw(int); static void x_redraw(int);
static void x_push(int); static void x_push(int);
static char * x_mapin(const char *); static char * x_mapin(const char *, Area *);
static char * x_mapout(int); static char * x_mapout(int);
static void x_mapout2(int, char **); static void x_mapout2(int, char **);
static void x_print(int, int); static void x_print(int, int);
@ -2503,11 +2505,11 @@ x_error(int c __unused)
} }
static char * static char *
x_mapin(const char *cp) x_mapin(const char *cp, Area *ap)
{ {
char *new, *op; char *new, *op;
op = new = str_save(cp, ATEMP); op = new = str_save(cp, ap);
while (*cp) { while (*cp) {
/* XXX -- should handle \^ escape? */ /* XXX -- should handle \^ escape? */
if (*cp == '^') { if (*cp == '^') {
@ -2576,6 +2578,7 @@ x_bind(const char *a1, const char *a2,
int prefix, key; int prefix, key;
char *sp = NULL; char *sp = NULL;
char *m1, *m2; char *m1, *m2;
bool hastilde;
if (x_tab == NULL) { if (x_tab == NULL) {
bi_errorf("cannot bind, not a tty"); bi_errorf("cannot bind, not a tty");
@ -2600,7 +2603,7 @@ x_bind(const char *a1, const char *a2,
} }
return (0); return (0);
} }
m1 = x_mapin(a1); m2 = m1 = x_mapin(a1, ATEMP);
prefix = key = 0; prefix = key = 0;
for (;; m1++) { for (;; m1++) {
key = *m1 & CHARMASK; key = *m1 & CHARMASK;
@ -2621,6 +2624,8 @@ x_bind(const char *a1, const char *a2,
bi_errorf("%s' too long", msg); bi_errorf("%s' too long", msg);
return (1); return (1);
} }
hastilde = *m1;
afree(m2, ATEMP);
if (a2 == NULL) { if (a2 == NULL) {
x_print(prefix, key); x_print(prefix, key);
@ -2639,14 +2644,13 @@ x_bind(const char *a1, const char *a2,
} }
} else { } else {
f = XFUNC_ins_string; f = XFUNC_ins_string;
m2 = x_mapin(a2); sp = x_mapin(a2, AEDIT);
sp = str_save(m2, AEDIT);
} }
if ((x_tab[prefix][key] & 0x7F) == XFUNC_ins_string && if ((x_tab[prefix][key] & 0x7F) == XFUNC_ins_string &&
x_atab[prefix][key]) x_atab[prefix][key])
afree((void *)x_atab[prefix][key], AEDIT); afree((void *)x_atab[prefix][key], AEDIT);
x_tab[prefix][key] = f | ((*m1) ? 0x80 : 0); x_tab[prefix][key] = f | (hastilde ? 0x80 : 0);
x_atab[prefix][key] = sp; x_atab[prefix][key] = sp;
/* Track what the user has bound so x_mode(true) won't toast things */ /* Track what the user has bound so x_mode(true) won't toast things */

5
eval.c
View File

@ -1,8 +1,8 @@
/* $OpenBSD: eval.c,v 1.30 2006/04/10 14:38:59 jaredy Exp $ */ /* $OpenBSD: eval.c,v 1.33 2007/08/02 11:05:54 fgsch Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.34 2007/07/31 10:42:15 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/eval.c,v 1.35 2007/09/09 18:06:39 tg Exp $");
#ifdef MKSH_SMALL #ifdef MKSH_SMALL
#define MKSH_NOPWNAM #define MKSH_NOPWNAM
@ -903,6 +903,7 @@ comsub(Expand *xp, const char *cp)
s->start = s->str = cp; s->start = s->str = cp;
sold = source; sold = source;
t = compile(s); t = compile(s);
afree(s, ATEMP);
source = sold; source = sold;
if (t == NULL) if (t == NULL)

23
exec.c
View File

@ -1,8 +1,8 @@
/* $OpenBSD: exec.c,v 1.46 2006/04/10 14:38:59 jaredy Exp $ */ /* $OpenBSD: exec.c,v 1.48 2007/09/05 19:02:01 otto Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.37 2007/07/24 11:22:04 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/exec.c,v 1.38 2007/09/09 18:06:40 tg Exp $");
static int comexec(struct op *, struct tbl *volatile, const char **, static int comexec(struct op *, struct tbl *volatile, const char **,
int volatile); int volatile);
@ -881,7 +881,7 @@ findcom(const char *name, int flags)
struct tbl *tp = NULL, *tbi; struct tbl *tp = NULL, *tbi;
int insert = Flag(FTRACKALL); /* insert if not found */ int insert = Flag(FTRACKALL); /* insert if not found */
char *fpath; /* for function autoloading */ char *fpath; /* for function autoloading */
const char *npath; union mksh_cchack npath;
if (vstrchr(name, '/')) { if (vstrchr(name, '/')) {
insert = 0; insert = 0;
@ -934,14 +934,21 @@ findcom(const char *name, int flags)
} }
tp->flag = DEFINED; /* make ~ISSET */ tp->flag = DEFINED; /* make ~ISSET */
} }
npath = search(name, flags & FC_DEFPATH ? def_path : path, npath.ro = search(name, flags & FC_DEFPATH ? def_path : path,
X_OK, &tp->u2.errno_); X_OK, &tp->u2.errno_);
if (npath) { if (npath.ro) {
tp->val.s = str_save(npath, APERM); /* XXX unsure about this, oksh does that
if (tp == &temp)
tp->val.s = npath.ro;
else */ {
tp->val.s = str_save(npath.ro, APERM);
if (npath.ro != name)
afree(npath.rw, ATEMP);
}
tp->flag |= ISSET|ALLOC; tp->flag |= ISSET|ALLOC;
} else if ((flags & FC_FUNC) && } else if ((flags & FC_FUNC) &&
(fpath = str_val(global("FPATH"))) != null && (fpath = str_val(global("FPATH"))) != null &&
(npath = search(name, fpath, R_OK, (npath.ro = search(name, fpath, R_OK,
&tp->u2.errno_)) != NULL) { &tp->u2.errno_)) != NULL) {
/* An undocumented feature of at&t ksh is that it /* An undocumented feature of at&t ksh is that it
* searches FPATH if a command is not found, even * searches FPATH if a command is not found, even
@ -951,7 +958,7 @@ findcom(const char *name, int flags)
tp = &temp; tp = &temp;
tp->type = CFUNC; tp->type = CFUNC;
tp->flag = DEFINED; /* make ~ISSET */ tp->flag = DEFINED; /* make ~ISSET */
tp->u.fpath = npath; tp->u.fpath = npath.ro;
} }
} }
return tp; return tp;

41
funcs.c
View File

@ -1,11 +1,11 @@
/* $OpenBSD: c_ksh.c,v 1.29 2006/03/12 00:26:58 deraadt Exp $ */ /* $OpenBSD: c_ksh.c,v 1.30 2007/08/02 10:50:25 fgsch Exp $ */
/* $OpenBSD: c_sh.c,v 1.35 2006/04/10 14:38:59 jaredy Exp $ */ /* $OpenBSD: c_sh.c,v 1.37 2007/09/03 13:54:23 otto Exp $ */
/* $OpenBSD: c_test.c,v 1.17 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: c_test.c,v 1.17 2005/03/30 17:16:37 deraadt Exp $ */
/* $OpenBSD: c_ulimit.c,v 1.16 2006/11/20 21:53:39 miod Exp $ */ /* $OpenBSD: c_ulimit.c,v 1.16 2006/11/20 21:53:39 miod Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.64 2007/08/19 23:12:21 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.65 2007/09/09 18:06:40 tg Exp $");
/* A leading = means assignments before command are kept; /* A leading = means assignments before command are kept;
* a leading * means a POSIX special builtin; * a leading * means a POSIX special builtin;
@ -281,6 +281,7 @@ c_pwd(const char **wp)
int optc; int optc;
int physical = Flag(FPHYSICAL); int physical = Flag(FPHYSICAL);
char *p; char *p;
bool p_ = false;
while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1) while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
switch (optc) { switch (optc) {
@ -303,11 +304,16 @@ c_pwd(const char **wp)
NULL; NULL;
if (p && access(p, R_OK) < 0) if (p && access(p, R_OK) < 0)
p = NULL; p = NULL;
if (!p && !(p = ksh_get_wd(NULL))) { if (!p) {
bi_errorf("can't get current directory - %s", strerror(errno)); if (!(p = ksh_get_wd(NULL))) {
return 1; bi_errorf("can't get current directory - %s",
strerror(errno));
return 1;
}
p_ = true;
} }
shprintf("%s\n", p); shprintf("%s\n", p);
afreechv(p_, p);
return 0; return 0;
} }
@ -1910,6 +1916,7 @@ c_eval(const char **wp)
rv = shell(s, false); rv = shell(s, false);
Flag(FERREXIT) = savef; Flag(FERREXIT) = savef;
source = saves; source = saves;
afree(s, ATEMP);
return (rv); return (rv);
} }
@ -2090,37 +2097,33 @@ int
c_unset(const char **wp) c_unset(const char **wp)
{ {
const char *id; const char *id;
int optc, unset_var = 1; int optc;
int ret = 0; bool unset_var = true;
while ((optc = ksh_getopt(wp, &builtin_opt, "fv")) != -1) while ((optc = ksh_getopt(wp, &builtin_opt, "fv")) != -1)
switch (optc) { switch (optc) {
case 'f': case 'f':
unset_var = 0; unset_var = false;
break; break;
case 'v': case 'v':
unset_var = 1; unset_var = true;
break; break;
case '?': case '?':
return 1; return (1);
} }
wp += builtin_opt.optind; wp += builtin_opt.optind;
for (; (id = *wp) != NULL; wp++) for (; (id = *wp) != NULL; wp++)
if (unset_var) { /* unset variable */ if (unset_var) { /* unset variable */
struct tbl *vp = global(id); struct tbl *vp = global(id);
if (!(vp->flag & ISSET))
ret = 1;
if ((vp->flag&RDONLY)) { if ((vp->flag&RDONLY)) {
bi_errorf("%s is read only", vp->name); bi_errorf("%s is read only", vp->name);
return 1; return (1);
} }
unset(vp, vstrchr(id, '[') ? 1 : 0); unset(vp, vstrchr(id, '[') ? 1 : 0);
} else { /* unset function */ } else /* unset function */
if (define(id, (struct op *) NULL)) define(id, NULL);
ret = 1; return (0);
}
return ret;
} }
static void static void

13
jobs.c
View File

@ -1,8 +1,8 @@
/* $OpenBSD: jobs.c,v 1.35 2006/02/06 16:47:07 jmc Exp $ */ /* $OpenBSD: jobs.c,v 1.36 2007/09/06 19:57:47 otto Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.26 2007/08/12 13:42:21 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.27 2007/09/09 18:06:41 tg Exp $");
/* Order important! */ /* Order important! */
#define PRUNNING 0 #define PRUNNING 0
@ -214,11 +214,14 @@ j_change(void)
int i; int i;
if (Flag(FMONITOR)) { if (Flag(FMONITOR)) {
bool use_tty = Flag(FTALKING);
/* Don't call tcgetattr() 'til we own the tty process group */ /* Don't call tcgetattr() 'til we own the tty process group */
tty_init(false); if (use_tty)
tty_init(false);
/* no controlling tty, no SIGT* */ /* no controlling tty, no SIGT* */
ttypgrp_ok = tty_fd >= 0 && tty_devtty; ttypgrp_ok = use_tty && tty_fd >= 0 && tty_devtty;
if (ttypgrp_ok && (our_pgrp = getpgrp()) < 0) { if (ttypgrp_ok && (our_pgrp = getpgrp()) < 0) {
warningf(false, "j_init: getpgrp() failed: %s", warningf(false, "j_init: getpgrp() failed: %s",
@ -264,7 +267,7 @@ j_change(void)
our_pgrp = kshpid; our_pgrp = kshpid;
} }
} }
if (!ttypgrp_ok) if (use_tty && !ttypgrp_ok)
warningf(false, "warning: won't have full job control"); warningf(false, "warning: won't have full job control");
if (tty_fd >= 0) if (tty_fd >= 0)
tcgetattr(tty_fd, &tty_state); tcgetattr(tty_fd, &tty_state);

4
misc.c
View File

@ -1,4 +1,4 @@
/* $OpenBSD: misc.c,v 1.30 2006/03/12 00:26:58 deraadt Exp $ */ /* $OpenBSD: misc.c,v 1.32 2007/08/02 11:05:54 fgsch Exp $ */
/* $OpenBSD: path.c,v 1.12 2005/03/30 17:16:37 deraadt Exp $ */ /* $OpenBSD: path.c,v 1.12 2005/03/30 17:16:37 deraadt Exp $ */
#include "sh.h" #include "sh.h"
@ -6,7 +6,7 @@
#include <grp.h> #include <grp.h>
#endif #endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.65 2007/08/12 13:42:21 tg Exp $\t" __RCSID("$MirOS: src/bin/mksh/misc.c,v 1.66 2007/09/09 18:06:41 tg Exp $\t"
MKSH_SH_H_ID); MKSH_SH_H_ID);
#undef USE_CHVT #undef USE_CHVT

4
sh.h
View File

@ -8,8 +8,8 @@
/* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */ /* $OpenBSD: c_test.h,v 1.4 2004/12/20 11:34:26 otto Exp $ */
/* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */ /* $OpenBSD: tty.h,v 1.5 2004/12/20 11:34:26 otto Exp $ */
#define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.174 2007/08/20 14:06:10 tg Exp $" #define MKSH_SH_H_ID "$MirOS: src/bin/mksh/sh.h,v 1.175 2007/09/09 18:06:41 tg Exp $"
#define MKSH_VERSION "R31 2007/08/19" #define MKSH_VERSION "R31 2007/09/09"
#if HAVE_SYS_PARAM_H #if HAVE_SYS_PARAM_H
#include <sys/param.h> #include <sys/param.h>

13
var.c
View File

@ -1,8 +1,8 @@
/* $OpenBSD: var.c,v 1.30 2006/05/21 18:40:39 otto Exp $ */ /* $OpenBSD: var.c,v 1.33 2007/08/02 11:05:54 fgsch Exp $ */
#include "sh.h" #include "sh.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.44 2007/08/20 13:57:47 tg Exp $"); __RCSID("$MirOS: src/bin/mksh/var.c,v 1.45 2007/09/09 18:06:42 tg Exp $");
/* /*
* Variables * Variables
@ -338,7 +338,9 @@ intval(struct tbl *vp)
int int
setstr(struct tbl *vq, const char *s, int error_ok) setstr(struct tbl *vq, const char *s, int error_ok)
{ {
char *salloc = NULL;
int no_ro_check = error_ok & 0x4; int no_ro_check = error_ok & 0x4;
error_ok &= ~0x4; error_ok &= ~0x4;
if ((vq->flag & RDONLY) && !no_ro_check) { if ((vq->flag & RDONLY) && !no_ro_check) {
warningf(true, "%s: is read only", vq->name); warningf(true, "%s: is read only", vq->name);
@ -359,7 +361,7 @@ setstr(struct tbl *vq, const char *s, int error_ok)
vq->flag &= ~(ISSET|ALLOC); vq->flag &= ~(ISSET|ALLOC);
vq->type = 0; vq->type = 0;
if (s && (vq->flag & (UCASEV_AL|LCASEV|LJUST|RJUST))) if (s && (vq->flag & (UCASEV_AL|LCASEV|LJUST|RJUST)))
s = formatstr(vq, s); s = salloc = formatstr(vq, s);
if ((vq->flag&EXPORT)) if ((vq->flag&EXPORT))
export(vq, s); export(vq, s);
else { else {
@ -368,12 +370,13 @@ setstr(struct tbl *vq, const char *s, int error_ok)
} }
} else { /* integer dest */ } else { /* integer dest */
if (!v_evaluate(vq, s, error_ok, true)) if (!v_evaluate(vq, s, error_ok, true))
return 0; return (0);
} }
vq->flag |= ISSET; vq->flag |= ISSET;
if ((vq->flag&SPECIAL)) if ((vq->flag&SPECIAL))
setspec(vq); setspec(vq);
return 1; afreechk(salloc);
return (1);
} }
/* set variable to integer */ /* set variable to integer */