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

@ -118,7 +118,7 @@ static int charlen(const char*,int);
void *sh_macopen(Shell_t *shp)
{
void *addr = newof(0,Mac_t,1,0);
void *addr = sh_newof(0,Mac_t,1,0);
Mac_t *mp = (Mac_t*)addr;
mp->shp = shp;
return(addr);
@ -971,9 +971,7 @@ static char *prefix(Shell_t *shp, char *id)
if(sub)
nv_putsub(np,sub,0L);
}
id = (char*)malloc(strlen(cp)+1+(n=strlen(sp=nv_name(np)))+ (sub?strlen(sub)+3:1));
if(!id)
sh_outofmemory();
id = (char*)sh_malloc(strlen(cp)+1+(n=strlen(sp=nv_name(np)))+ (sub?strlen(sub)+3:1));
memcpy(id,sp,n);
if(sub)
{
@ -986,7 +984,7 @@ static char *prefix(Shell_t *shp, char *id)
return(id);
}
}
return(strdup(id));
return(sh_strdup(id));
}
/*
@ -1041,7 +1039,7 @@ int sh_macfun(Shell_t *shp, const char *name, int offset)
t.node.com.comarg = &d.arg;
t.node.com.comline = shp->inlineno;
d.dol.dolnum = 1;
d.dol.dolval[0] = strdup(name);
d.dol.dolval[0] = sh_strdup(name);
stkseek(shp->stk,offset);
comsubst((Mac_t*)shp->mac_context,&t.node,2);
free(d.dol.dolval[0]);
@ -1363,7 +1361,7 @@ retry1:
v = stkptr(stkp,mp->dotdot);
dolmax =1;
if(array_assoc(ap))
arrmax = strdup(v);
arrmax = sh_strdup(v);
else if((dolmax = (int)sh_arith(mp->shp,v))<0)
dolmax += array_maxindex(np);
if(type==M_SUBNAME)
@ -1782,7 +1780,7 @@ retry1:
else
type = 0;
}
pattern = strdup(argp);
pattern = sh_strdup(argp);
if((type=='/' || c=='/') && (repstr = mac_getstring(pattern)))
{
Mac_t savemac;
@ -1794,7 +1792,7 @@ retry1:
mp->split = 0;
copyto(mp,0,0);
sfputc(stkp,0);
repstr = strdup(stkptr(stkp,n));
repstr = sh_strdup(stkptr(stkp,n));
replen = strlen(repstr);
stkseek(stkp,n);
*mp = savemac;
@ -2165,9 +2163,7 @@ static void comsubst(Mac_t *mp,register Shnode_t* t, int type)
}
if(!(sp=mp->shp->sftable[fd]))
{
char *cp = (char*)malloc(IOBSIZE+1);
if(!cp)
sh_outofmemory();
char *cp = (char*)sh_malloc(IOBSIZE+1);
sp = sfnew(NIL(Sfio_t*),cp,IOBSIZE,fd,SF_READ|SF_MALLOC);
}
type = 3;