• 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:
parent
b41a72ac2e
commit
f17b8b1c8b
4
edit.c
4
edit.c
@ -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 {
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
2
eval.c
2
eval.c
@ -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
|
||||
|
2
exec.c
2
exec.c
@ -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);
|
||||
|
53
funcs.c
53
funcs.c
@ -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;
|
||||
}
|
||||
|
||||
@ -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
|
||||
|
@ -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.
|
||||
|
2
jobs.c
2
jobs.c
@ -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
|
||||
|
5
lex.c
5
lex.c
@ -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';
|
||||
|
2
main.c
2
main.c
@ -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;
|
||||
|
||||
|
2
misc.c
2
misc.c
@ -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)
|
||||
|
15
sh.h
15
sh.h
@ -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"
|
||||
|
||||
@ -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);
|
||||
|
2
shf.c
2
shf.c
@ -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 */
|
||||
|
5
syn.c
5
syn.c
@ -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) */
|
||||
@ -588,7 +588,8 @@ 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 = (const char **)alloc(sizeof (char *) * 2,
|
||||
ATEMP);
|
||||
t->left->args[0] = tv = alloc(sizeof (char) * 3, ATEMP);
|
||||
tv[0] = CHAR;
|
||||
tv[1] = ':';
|
||||
|
10
tree.c
10
tree.c
@ -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
|
||||
|
||||
@ -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;
|
||||
|
4
var.c
4
var.c
@ -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
|
||||
@ -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);
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user