• alloc() can’t fail, afree() can take NULL

‣ macro afreechk() is superfluous
• get rid of macro afreechv() by re-doing the “don’t leak that much” code
• some KNF (mostly, whitespace and 80c) while here
This commit is contained in:
tg 2008-05-17 18:47:03 +00:00
parent b41a72ac2e
commit f17b8b1c8b
14 changed files with 77 additions and 93 deletions

8
edit.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.126 2008/05/02 18:55:35 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/edit.c,v 1.127 2008/05/17 18:46:57 tg Exp $");
/* tty driver characters we are interested in */
typedef struct {
@ -396,7 +396,7 @@ x_command_glob(int flags, const char *str, int slen, char ***wordsp)
int i, path_order = 0;
info = (struct path_order_info *)
alloc(sizeof(struct path_order_info) * nwords, ATEMP);
alloc(sizeof (struct path_order_info) * nwords, ATEMP);
for (i = 0; i < nwords; i++) {
info[i].word = words[i];
info[i].base = x_basename(words[i], NULL);
@ -586,7 +586,7 @@ static void
x_free_words(int nwords, char **words)
{
while (nwords)
afreechk(words[--nwords]);
afree(words[--nwords], ATEMP);
afree(words, ATEMP);
}
@ -4705,7 +4705,7 @@ save_edstate(struct edstate *old)
{
struct edstate *new;
new = (struct edstate *)alloc(sizeof(struct edstate), APERM);
new = (struct edstate *)alloc(sizeof (struct edstate), APERM);
new->cbuf = alloc(old->cbufsize, APERM);
memcpy(new->cbuf, old->cbuf, old->linelen);
new->cbufsize = old->cbufsize;

10
eval.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.45 2008/03/01 22:49:37 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/eval.c,v 1.46 2008/05/17 18:46:58 tg Exp $");
#ifdef MKSH_SMALL
#define MKSH_NOPWNAM
@ -297,8 +297,8 @@ expand(const char *cp, /* input word */
if (!st->next) {
SubType *newst;
newst = (SubType *) alloc(
sizeof(SubType), ATEMP);
newst = (SubType *)alloc(
sizeof (SubType), ATEMP);
newst->next = NULL;
newst->prev = st;
st->next = newst;
@ -589,7 +589,7 @@ expand(const char *cp, /* input word */
*/
len = strlen(dp) + 1;
setstr(st->var,
debunk((char *) alloc(len, ATEMP),
debunk((char *)alloc(len, ATEMP),
dp, len), KSH_UNWIND_ERROR);
x.str = str_val(st->var);
type = XSUB;
@ -1474,7 +1474,7 @@ alt_expand(XPtrV *wp, char *start, char *exp_start, char *end, int fdo)
l1 = brace_start - start;
l2 = (p - 1) - field_start;
l3 = end - brace_end;
new = (char *) alloc(l1 + l2 + l3 + 1, ATEMP);
new = (char *)alloc(l1 + l2 + l3 + 1, ATEMP);
memcpy(new, start, l1);
memcpy(new + l1, field_start, l2);
memcpy(new + l1 + l2, brace_end, l3);

4
exec.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.43 2008/04/19 22:15:02 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/exec.c,v 1.44 2008/05/17 18:46:58 tg Exp $");
static int comexec(struct op *, struct tbl *volatile, const char **,
int volatile);
@ -72,7 +72,7 @@ execute(struct op *volatile t,
flags &= ~XTIME;
if (t->ioact != NULL || t->type == TPIPE || t->type == TCOPROC) {
e->savefd = (short *) alloc(sizeofN(short, NUFILE), ATEMP);
e->savefd = (short *)alloc(sizeofN(short, NUFILE), ATEMP);
/* initialise to not redirected */
memset(e->savefd, 0, sizeofN(short, NUFILE));
}

55
funcs.c
View File

@ -5,7 +5,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.80 2008/05/17 18:27:55 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.81 2008/05/17 18:46:58 tg Exp $");
/* A leading = means assignments before command are kept;
* a leading * means a POSIX special builtin;
@ -138,8 +138,7 @@ c_cd(const char **wp)
bool printpath = false; /* print where we cd'd? */
struct tbl *pwd_s, *oldpwd_s;
XString xs;
char *dir, *try, *pwd, *cdpath;
bool dir_ = false;
char *dir, *allocd = NULL, *try, *pwd, *cdpath;
while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
switch (optc) {
@ -170,17 +169,16 @@ c_cd(const char **wp)
}
} else if (!wp[1]) {
/* One argument: - or dir */
dir = str_save(wp[0], ATEMP);
if (ksh_isdash(dir)) {
afree(dir, ATEMP);
if (ksh_isdash((dir = allocd = str_save(wp[0], ATEMP)))) {
afree(allocd, ATEMP);
allocd = NULL;
dir = str_val(oldpwd_s);
if (dir == null) {
bi_errorf("no OLDPWD");
return 1;
}
printpath = true;
} else
dir_ = true;
}
} else if (!wp[2]) {
/* Two arguments - substitute arg1 in PWD for arg2 */
int ilen, olen, nlen, elen;
@ -203,8 +201,7 @@ c_cd(const char **wp)
olen = strlen(wp[0]);
nlen = strlen(wp[1]);
elen = strlen(current_wd + ilen + olen) + 1;
dir = alloc(ilen + nlen + elen, ATEMP);
dir_ = true;
dir = allocd = alloc(ilen + nlen + elen, ATEMP);
memcpy(dir, current_wd, ilen);
memcpy(dir + ilen, wp[1], nlen);
memcpy(dir + ilen + nlen, current_wd + ilen + olen, elen);
@ -232,7 +229,7 @@ c_cd(const char **wp)
bi_errorf("%s: bad directory", dir);
else
bi_errorf("%s - %s", try, strerror(errno));
afreechv(dir_, dir);
afree(allocd, ATEMP);
return 1;
}
@ -266,7 +263,7 @@ c_cd(const char **wp)
if (printpath || cdnode)
shprintf("%s\n", pwd);
afreechv(dir_, dir);
afree(allocd, ATEMP);
return 0;
}
@ -275,8 +272,7 @@ c_pwd(const char **wp)
{
int optc;
bool physical = Flag(FPHYSICAL) ? true : false;
char *p;
bool p_ = false;
char *p, *allocd = NULL;
while ((optc = ksh_getopt(wp, &builtin_opt, "LP")) != -1)
switch (optc) {
@ -295,20 +291,16 @@ c_pwd(const char **wp)
bi_errorf("too many arguments");
return 1;
}
p = current_wd[0] ? (physical ? get_phys_path(current_wd) : current_wd) :
NULL;
p = current_wd[0] ? (physical ? get_phys_path(current_wd) :
current_wd) : NULL;
if (p && access(p, R_OK) < 0)
p = NULL;
if (!p) {
if (!(p = ksh_get_wd(NULL))) {
bi_errorf("can't get current directory - %s",
strerror(errno));
return 1;
}
p_ = true;
if (!p && !(p = allocd = ksh_get_wd(NULL))) {
bi_errorf("can't get current directory - %s", strerror(errno));
return (1);
}
shprintf("%s\n", p);
afreechv(p_, p);
afree(allocd, ATEMP);
return 0;
}
@ -1124,7 +1116,7 @@ c_alias(const char **wp)
ap->flag &= ~xflag;
else
ap->flag |= xflag;
afreechk(xalias);
afree(xalias, ATEMP);
}
return rv;
@ -1513,7 +1505,7 @@ c_bind(const char **wp)
}
if (x_bind(up ? up : *wp, cp, macro, 0))
rv = 1;
afreechk(up);
afree(up, ATEMP);
}
return rv;
@ -1886,14 +1878,14 @@ c_read(const char **wp)
if (vp->flag & RDONLY) {
shf_flush(shf);
bi_errorf("%s is read only", *wp);
afreechk(wpalloc);
afree(wpalloc, ATEMP);
return 1;
}
if (Flag(FEXPORT))
typeset(*wp, EXPORT, 0, 0, 0);
if (!setstr(vp, Xstring(cs, ccp), KSH_RETURN_ERROR)) {
shf_flush(shf);
afreechk(wpalloc);
afree(wpalloc, ATEMP);
return 1;
}
}
@ -1912,7 +1904,7 @@ c_read(const char **wp)
if (c == EOF && !ecode)
coproc_read_close(fd);
afreechk(wpalloc);
afree(wpalloc, ATEMP);
return ecode ? ecode : c == EOF;
}
@ -2121,7 +2113,7 @@ c_set(const char **wp)
while (*++wp != NULL)
*wp = str_save(*wp, &l->area);
l->argc = wp - owp - 1;
l->argv = (const char **) alloc(sizeofN(char *, l->argc+2),
l->argv = (const char **)alloc(sizeofN(char *, l->argc+2),
&l->area);
for (wp = l->argv; (*wp++ = *owp++) != NULL; )
;
@ -3076,8 +3068,7 @@ c_realpath(const char **wp)
else {
char *buf;
buf = alloc(PATH_MAX, ATEMP);
if (realpath(*wp, buf) == NULL) {
if (realpath(*wp, (buf = alloc(PATH_MAX, ATEMP))) == NULL) {
rv = errno;
bi_errorf("%s: %s", *wp, strerror(rv));
} else

View File

@ -3,7 +3,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.61 2008/04/19 17:21:53 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/histrap.c,v 1.62 2008/05/17 18:46:59 tg Exp $");
/*-
* MirOS: This is the default mapping type, and need not be specified.
@ -554,7 +554,7 @@ init_histvec(void)
{
if (history == (char **)NULL) {
histsize = HISTORYSIZE;
history = (char **)alloc(histsize*sizeof (char *), APERM);
history = (char **)alloc(histsize * sizeof (char *), APERM);
histptr = history - 1;
}
}

6
jobs.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.34 2008/05/15 15:24:09 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/jobs.c,v 1.35 2008/05/17 18:46:59 tg Exp $");
/* Order important! */
#define PRUNNING 0
@ -1402,7 +1402,7 @@ new_job(void)
newj = free_jobs;
free_jobs = free_jobs->next;
} else
newj = (Job *) alloc(sizeof(Job), APERM);
newj = (Job *)alloc(sizeof (Job), APERM);
/* brute force method */
for (i = 1; ; i++) {
@ -1429,7 +1429,7 @@ new_proc(void)
p = free_procs;
free_procs = free_procs->next;
} else
p = (Proc *) alloc(sizeof(Proc), APERM);
p = (Proc *)alloc(sizeof (Proc), APERM);
return p;
}

9
lex.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.59 2008/05/04 01:51:30 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/lex.c,v 1.60 2008/05/17 18:46:59 tg Exp $");
/*
* states while lexing word
@ -774,7 +774,8 @@ yylex(int cf)
if ((c == '<' || c == '>') && state == SBASE &&
((c2 = Xlength(ws, wp)) == 0 ||
(c2 == 2 && dp[0] == CHAR && ksh_isdigit(dp[1])))) {
struct ioword *iop = (struct ioword *) alloc(sizeof(*iop), ATEMP);
struct ioword *iop = (struct ioword *)alloc(sizeof (*iop),
ATEMP);
if (c2 == 2)
iop->unit = dp[1] - '0';
@ -1012,7 +1013,7 @@ pushs(int type, Area *areap)
{
Source *s;
s = (Source *)alloc(sizeof(Source), areap);
s = (Source *)alloc(sizeof (Source), areap);
s->type = type;
s->str = null;
s->start = NULL;
@ -1501,7 +1502,7 @@ getsc_bn(void)
static Lex_state *
push_state_(State_info *si, Lex_state *old_end)
{
Lex_state *new = alloc(sizeof(Lex_state) * STATE_BSIZE, ATEMP);
Lex_state *new = alloc(sizeof (Lex_state) * STATE_BSIZE, ATEMP);
new[0].ls_info.base = old_end;
si->base = &new[0];

6
main.c
View File

@ -13,7 +13,7 @@
#include <locale.h>
#endif
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.97 2008/05/15 15:24:10 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/main.c,v 1.98 2008/05/17 18:47:00 tg Exp $");
extern char **environ;
@ -592,7 +592,7 @@ newenv(int type)
{
struct env *ep;
ep = (struct env *) alloc(sizeof(*ep), ATEMP);
ep = (struct env *)alloc(sizeof (*ep), ATEMP);
ep->type = type;
ep->flags = 0;
ainit(&ep->area);
@ -1133,7 +1133,7 @@ maketemp(Area *ap, Temp_type type, struct temp **tlist)
pathname = tempnam(dir, "mksh.");
len = ((pathname == NULL) ? 0 : strlen(pathname)) + 1;
#endif
tp = (struct temp *) alloc(sizeof(struct temp) + len, ap);
tp = (struct temp *)alloc(sizeof (struct temp) + len, ap);
tp->name = (char *)&tp[1];
#if !HAVE_MKSTEMP
if (pathname == NULL)

6
misc.c
View File

@ -6,7 +6,7 @@
#include <grp.h>
#endif
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.78 2008/05/04 02:02:32 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/misc.c,v 1.79 2008/05/17 18:47:00 tg Exp $");
#undef USE_CHVT
#if defined(TIOCSCTTY) && !defined(MKSH_SMALL)
@ -513,7 +513,7 @@ gmatchx(const char *s, const char *p, bool isfile)
size_t len = pe - p + 1;
char tbuf[64];
char *t = len <= sizeof(tbuf) ? tbuf :
(char *) alloc(len, ATEMP);
(char *)alloc(len, ATEMP);
debunk(t, p, len);
return !strcmp(t, s);
}
@ -953,7 +953,7 @@ print_columns(struct shf *shf, int n,
char *(*func) (const void *, int, char *, int),
const void *arg, int max_width, int prefcol)
{
char *str = (char *) alloc(max_width + 1, ATEMP);
char *str = (char *)alloc(max_width + 1, ATEMP);
int i, r, c, rows, cols, nspace;
/* max_width + 1 for the space. Note that no space

17
sh.h
View File

@ -96,7 +96,7 @@
#define __SCCSID(x) __IDSTRING(sccsid,x)
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.216 2008/05/17 18:27:57 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.217 2008/05/17 18:47:01 tg Exp $");
#endif
#define MKSH_VERSION "R34 2008/05/17"
@ -1073,7 +1073,7 @@ typedef struct XPtrV {
#define XPinit(x, n) do { \
void **vp__; \
vp__ = (void**) alloc(sizeofN(void*, (n)), ATEMP); \
vp__ = (void**)alloc(sizeofN(void*, (n)), ATEMP); \
(x).cur = (x).beg = vp__; \
(x).end = vp__ + (n); \
} while (0)
@ -1209,18 +1209,9 @@ EXTERN struct timeval j_usrtime, j_systime;
/* alloc.c */
Area *ainit(Area *);
void afreeall(Area *);
void *alloc(size_t, Area *);
void *alloc(size_t, Area *); /* cannot fail */
void *aresize(void *, size_t, Area *);
void afree(void *, Area *);
#define afreechk(s) do { \
void *afree_t = (s); \
if (afree_t) \
afree(afree_t, ATEMP); \
} while (0)
#define afreechv(v,s) do { \
if (v) \
afree(s, ATEMP); \
} while (0)
void afree(void *, Area *); /* can take NULL */
/* edit.c */
void x_init(void);
int x_read(char *, size_t);

8
shf.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.20 2008/05/02 18:55:37 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/shf.c,v 1.21 2008/05/17 18:47:02 tg Exp $");
/* flags to shf_emptybuf() */
#define EB_READSW 0x01 /* about to switch to reading */
@ -29,7 +29,7 @@ shf_open(const char *name, int oflags, int mode, int sflags)
int fd;
/* Done before open so if alloca fails, fd won't be lost. */
shf = (struct shf *) alloc(sizeof(struct shf) + bsize, ATEMP);
shf = (struct shf *)alloc(sizeof (struct shf) + bsize, ATEMP);
shf->areap = ATEMP;
shf->buf = (unsigned char *)&shf[1];
shf->bsize = bsize;
@ -97,7 +97,7 @@ shf_fdopen(int fd, int sflags, struct shf *shf)
} else
shf->buf = NULL;
} else {
shf = (struct shf *)alloc(sizeof(struct shf) + bsize, ATEMP);
shf = (struct shf *)alloc(sizeof (struct shf) + bsize, ATEMP);
shf->buf = (unsigned char *)&shf[1];
sflags |= SHF_ALLOCS;
}
@ -180,7 +180,7 @@ shf_sopen(char *buf, int bsize, int sflags, struct shf *shf)
internal_errorf("shf_sopen: flags 0x%x", sflags);
if (!shf) {
shf = (struct shf *) alloc(sizeof(struct shf), ATEMP);
shf = (struct shf *)alloc(sizeof (struct shf), ATEMP);
sflags |= SHF_ALLOCS;
}
shf->areap = ATEMP;

13
syn.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.20 2008/04/01 22:20:20 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/syn.c,v 1.21 2008/05/17 18:47:02 tg Exp $");
struct nesting_state {
int start_token; /* token than began nesting (eg, FOR) */
@ -186,7 +186,7 @@ get_command(int cf)
XPtrV args, vars;
struct nesting_state old_nesting;
iops = (struct ioword **) alloc(sizeofN(struct ioword *, NUFILE+1),
iops = (struct ioword **)alloc(sizeofN(struct ioword *, NUFILE+1),
ATEMP);
XPinit(args, 16);
XPinit(vars, 16);
@ -588,13 +588,14 @@ function_body(char *name,
* be used as input), we pretend there is a colon here.
*/
t->left = newtp(TCOM);
t->left->args = (const char **)alloc(sizeof(char *) * 2, ATEMP);
t->left->args[0] = tv = alloc(sizeof(char) * 3, ATEMP);
t->left->args = (const char **)alloc(sizeof (char *) * 2,
ATEMP);
t->left->args[0] = tv = alloc(sizeof (char) * 3, ATEMP);
tv[0] = CHAR;
tv[1] = ':';
tv[2] = EOS;
t->left->args[1] = NULL;
t->left->vars = (char **)alloc(sizeof(char *), ATEMP);
t->left->vars = (char **)alloc(sizeof (char *), ATEMP);
t->left->vars[0] = NULL;
t->left->lineno = 1;
}
@ -771,7 +772,7 @@ newtp(int type)
{
struct op *t;
t = (struct op *)alloc(sizeof(*t), ATEMP);
t = (struct op *)alloc(sizeof (*t), ATEMP);
t->type = type;
t->u.evalflags = 0;
t->args = NULL;

16
tree.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.16 2008/04/19 22:15:06 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/tree.c,v 1.17 2008/05/17 18:47:02 tg Exp $");
#define INDENT 4
@ -421,7 +421,7 @@ tcopy(struct op *t, Area *ap)
if (t == NULL)
return NULL;
r = (struct op *) alloc(sizeof(struct op), ap);
r = (struct op *)alloc(sizeof (struct op), ap);
r->type = t->type;
r->u.evalflags = t->u.evalflags;
@ -433,8 +433,8 @@ tcopy(struct op *t, Area *ap)
else {
for (tw = (const char **)t->vars; *tw++ != NULL; )
;
rw = r->vars = (char **)
alloc((tw - (const char **)t->vars + 1) * sizeof(*tw), ap);
rw = r->vars = (char **)alloc((tw -
(const char **)t->vars + 1) * sizeof (*tw), ap);
for (tw = (const char **)t->vars; *tw != NULL; )
*rw++ = wdcopy(*tw++, ap);
*rw = NULL;
@ -445,8 +445,8 @@ tcopy(struct op *t, Area *ap)
else {
for (tw = t->args; *tw++ != NULL; )
;
r->args = (const char **)(rw = (char **)
alloc((tw - t->args + 1) * sizeof(*tw), ap));
r->args = (const char **)(rw = (char **)alloc((tw -
t->args + 1) * sizeof (*tw), ap));
for (tw = t->args; *tw != NULL; )
*rw++ = wdcopy(*tw++, ap);
*rw = NULL;
@ -615,13 +615,13 @@ iocopy(struct ioword **iow, Area *ap)
for (ior = iow; *ior++ != NULL; )
;
ior = (struct ioword **) alloc((ior - iow + 1) * sizeof(*ior), ap);
ior = (struct ioword **)alloc((ior - iow + 1) * sizeof (*ior), ap);
for (i = 0; iow[i] != NULL; i++) {
struct ioword *p, *q;
p = iow[i];
q = (struct ioword *) alloc(sizeof(*p), ap);
q = (struct ioword *)alloc(sizeof (*p), ap);
ior[i] = q;
*q = *p;
if (p->name != NULL)

8
var.c
View File

@ -2,7 +2,7 @@
#include "sh.h"
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.55 2008/05/02 18:55:37 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/var.c,v 1.56 2008/05/17 18:47:03 tg Exp $");
/*
* Variables
@ -37,7 +37,7 @@ newblock(void)
struct block *l;
static const char *empty[] = { null };
l = (struct block *) alloc(sizeof(struct block), ATEMP);
l = (struct block *)alloc(sizeof (struct block), ATEMP);
l->flags = 0;
ainit(&l->area); /* todo: could use e->area (l->area => l->areap) */
if (!e->loc) {
@ -386,7 +386,7 @@ setstr(struct tbl *vq, const char *s, int error_ok)
vq->flag |= ISSET;
if ((vq->flag&SPECIAL))
setspec(vq);
afreechk(salloc);
afree(salloc, ATEMP);
return (1);
}
@ -1199,7 +1199,7 @@ arraysearch(struct tbl *vp, uint32_t val)
else
new = curr;
} else
new = (struct tbl *)alloc(sizeof(struct tbl) + namelen,
new = (struct tbl *)alloc(sizeof (struct tbl) + namelen,
vp->areap);
strlcpy(new->name, vp->name, namelen);
new->flag = vp->flag & ~(ALLOC|DEFINED|ISSET|SPECIAL);