1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Add more out of memory checks (re: 18529b88) (#192)

The referenced commit neglected to add checks for strdup() calls.
That calls malloc() as well, and is used a lot.

This commit switches to another strategy: it adds wrapper functions
for all the allocation macros that check if the allocation
succeeded, so those checks don't need to be done manually.

src/cmd/ksh93/include/defs.h,
src/cmd/ksh93/sh/init.c:
- Add sh_malloc(), sh_realloc(), sh_calloc(), sh_strdup(),
  sh_memdup() wrapper functions with success checks. Call nospace()
  to error out if allocation fails.
- Update new_of() macro to use sh_malloc().
- Define new sh_newof() macro to replace newof(); it uses
  sh_realloc().

All other changed files:
- Replace the relevant calls with the wrappers.
- Remove now-redundant success checks from 18529b88.
- The ERROR_PANIC error message calls are updated to inclusive-or
  ERROR_SYSTEM into the exit code argument, so libast's error()
  appends the human-readable version of errno in square brackets.
  See src/lib/libast/man/error.3

src/cmd/ksh93/edit/history.c:
- Include "defs.h" to get access to the wrappers even if KSHELL is
  not defined.
- Since we're here, fix a compile error that occurred with KSHELL
  undefined by updating the type definition of hist_fname[] to
  match that of history.h.

src/cmd/ksh93/bltins/enum.c:
- To get access to sh_newof(), include "defs.h" instead of
  <shell.h> (note that "defs.h" includes <shell.h> itself).

src/cmd/ksh93/Mamfile:
- enum.c: depend on defs.h instead of shell.h.
- enum.o: add an -I. flag in the compiler invocation so that defs.h
  can find its subsequent includes.

src/cmd/builtin/pty.c:
- Define one outofmemory() function and call that instead of
  repeating the error message call.
- outofmemory() never returns, so remove superfluous exit handling.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
This commit is contained in:
Johnothan King 2021-02-27 13:21:58 -08:00 committed by GitHub
parent c928046aa9
commit 7ad274f8b6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
33 changed files with 261 additions and 371 deletions

View file

@ -124,11 +124,9 @@ static char *getbuf(size_t len)
if(buflen < len)
{
if(buflen==0)
buf = (char*)malloc(len);
buf = (char*)sh_malloc(len);
else
buf = (char*)realloc(buf,len);
if(!buf)
sh_outofmemory();
buf = (char*)sh_realloc(buf,len);
buflen = len;
}
return(buf);
@ -230,9 +228,7 @@ Namval_t *nv_addnode(Namval_t* np, int remove)
if(sp->numnodes==sp->maxnodes)
{
sp->maxnodes += 20;
sp->nodes = (Namval_t**)realloc(sp->nodes,sizeof(Namval_t*)*sp->maxnodes);
if(!sp->nodes)
sh_outofmemory();
sp->nodes = (Namval_t**)sh_realloc(sp->nodes,sizeof(Namval_t*)*sp->maxnodes);
}
sp->nodes[sp->numnodes++] = np;
return(np);
@ -285,9 +281,7 @@ void nv_setlist(register struct argnod *arg,register int flags, Namval_t *typ)
shtp.numnodes=0;
shtp.maxnodes = 20;
shtp.rp = 0;
shtp.nodes =(Namval_t**)malloc(shtp.maxnodes*sizeof(Namval_t*));
if(!shtp.nodes)
sh_outofmemory();
shtp.nodes = (Namval_t**)sh_malloc(shtp.maxnodes*sizeof(Namval_t*));
}
#endif /* SHOPT_TYPEDEF*/
#if SHOPT_NAMESPACE
@ -1431,11 +1425,9 @@ Namval_t *nv_open(const char *name, Dt_t *root, int flags)
if(c > xp->size)
{
if(xp->size==0)
xp->name = malloc(c);
xp->name = sh_malloc(c);
else
xp->name = realloc(xp->name,c);
if(!xp->name)
sh_outofmemory();
xp->name = sh_realloc(xp->name,c);
xp->size = c;
}
memcpy(xp->name,name,xp->len);
@ -1905,9 +1897,7 @@ void nv_putval(register Namval_t *np, const char *string, int flags)
size = nv_size(np);
if(size==0)
size = oldsize + (3*dot/4);
cp = (char*)malloc(size+1);
if(!cp)
sh_outofmemory();
cp = (char*)sh_malloc(size+1);
*cp = 0;
nv_offattr(np,NV_NOFREE);
if(oldsize)
@ -1966,13 +1956,11 @@ void nv_putval(register Namval_t *np, const char *string, int flags)
{
if(tofree && tofree!=Empty && tofree!=Null)
{
cp = (char*)realloc((void*)tofree, dot+append+1);
cp = (char*)sh_realloc((void*)tofree, dot+append+1);
tofree = 0;
}
else
cp = (char*)malloc(dot+append+1);
if(!cp)
sh_outofmemory();
cp = (char*)sh_malloc(dot+append+1);
cp[dot+append] = 0;
nv_offattr(np,NV_NOFREE);
}
@ -2364,7 +2352,7 @@ void sh_envnolocal (register Namval_t *np, void *data)
{
nv_putsub(np,NIL(char*),0);
if(cp = nv_getval(np))
cp = strdup(cp);
cp = sh_strdup(cp);
}
if(nv_isattr(np,NV_EXPORT|NV_NOFREE))
{
@ -2414,7 +2402,7 @@ static void table_unset(Shell_t *shp, register Dt_t *root, int flags, Dt_t *oroo
nv_putval(nq,(char*)&d,NV_LDOUBLE);
}
else if(shp->test&4)
nv_putval(nq, strdup(nv_getval(nq)), NV_RDONLY);
nv_putval(nq, sh_strdup(nv_getval(nq)), NV_RDONLY);
else
nv_putval(nq, nv_getval(nq), NV_RDONLY);
shp->subshell = subshell;
@ -2694,7 +2682,7 @@ void nv_optimize(Namval_t *np)
if(op = opt_free)
opt_free = op->next;
else
op=(struct optimize*)calloc(1,sizeof(struct optimize));
op=(struct optimize*)sh_calloc(1,sizeof(struct optimize));
op->ptr = shp->argaddr;
op->np = np;
if(xp)
@ -3049,25 +3037,19 @@ void nv_newattr (register Namval_t *np, unsigned newatts, int size)
if(size==0 || (newatts&(NV_INTEGER|NV_BINARY)))
{
/* allocate to match existing value for numerics and auto length assignment for -L/R/Z */
cp = (char*)malloc((size_t)n + 1);
if(!cp)
sh_outofmemory();
cp = (char*)sh_malloc((size_t)n + 1);
strcpy(cp, sp);
}
else if(size>=n)
{
/* growing string */
cp = (char*)malloc((size_t)size + 1);
if(!cp)
sh_outofmemory();
cp = (char*)sh_malloc((size_t)size + 1);
strcpy(cp, sp);
}
else
{
/* shrinking string */
cp = (char*)malloc((size_t)size + 1);
if(!cp)
sh_outofmemory();
cp = (char*)sh_malloc((size_t)size + 1);
if(newatts&NV_RJUST)
strncpy(cp, n - size + sp, size);
else
@ -3329,7 +3311,7 @@ int nv_rename(register Namval_t *np, int flags)
if(index<0)
return(0);
if(cp = nv_getval(np))
cp = strdup(cp);
cp = sh_strdup(cp);
}
_nv_unset(np,NV_EXPORT);
if(nr==np)
@ -3488,7 +3470,7 @@ void nv_setref(register Namval_t *np, Dt_t *hp, int flags)
shp->last_root = root;
_nv_unset(np,0);
nv_delete(np,(Dt_t*)0,0);
np->nvalue.nrp = newof(0,struct Namref,1,sizeof(Dtlink_t));
np->nvalue.nrp = sh_newof(0,struct Namref,1,sizeof(Dtlink_t));
np->nvalue.nrp->np = nq;
np->nvalue.nrp->root = hp;
if(ep)
@ -3498,7 +3480,7 @@ void nv_setref(register Namval_t *np, Dt_t *hp, int flags)
np->nvalue.nrp->curi = ARRAY_FIXED|nv_arrfixed(nq,(Sfio_t*)0,1,&np->nvalue.nrp->dim);
else
#endif /* SHOPT_FIXEDARRAY */
np->nvalue.nrp->sub = strdup(ep);
np->nvalue.nrp->sub = sh_strdup(ep);
}
np->nvalue.nrp->table = shp->last_table;
nv_onattr(np,NV_REF|NV_NOFREE);
@ -3594,7 +3576,7 @@ void nv_unref(register Namval_t *np)
dtdelete(Refdict,(void*)np->nvalue.nrp);
}
free((void*)np->nvalue.nrp);
np->nvalue.cp = strdup(nv_name(nq));
np->nvalue.cp = sh_strdup(nv_name(nq));
#if SHOPT_OPTIMIZE
{
Namfun_t *fp;