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

@ -87,11 +87,10 @@ static Namarr_t *array_scope(Namval_t *np, Namarr_t *ap, int flags)
size_t size = ap->hdr.dsize;
if(size==0)
size = ap->hdr.disc->dsize;
if(!(aq=newof(NIL(Namarr_t*),Namarr_t,1,size-sizeof(Namarr_t))))
return(0);
memcpy(aq,ap,size);
aq = sh_newof(NIL(Namarr_t*),Namarr_t,1,size-sizeof(Namarr_t));
memcpy(aq,ap,size);
aq->hdr.nofree &= ~1;
aq->hdr.nofree |= (flags&NV_RDONLY)?1:0;
aq->hdr.nofree |= (flags&NV_RDONLY)?1:0;
if(is_associative(aq))
{
aq->scope = (void*)dtopen(&_Nvdisc,Dtoset);
@ -346,9 +345,7 @@ static Namval_t *array_find(Namval_t *np,Namarr_t *arp, int flag)
{
if(data)
{
fp->data = (char*)malloc(fp->nelem*fp->size);
if(!fp->data)
sh_outofmemory();
fp->data = (char*)sh_malloc(fp->nelem*fp->size);
memcpy(fp->data,data,fp->nelem*fp->size);
}
else
@ -490,7 +487,7 @@ static Namfun_t *array_clone(Namval_t *np, Namval_t *mp, int flags, Namfun_t *fp
mp->nvflag &= NV_MINIMAL;
mp->nvflag |= (np->nvflag&~(NV_MINIMAL|NV_NOFREE));
if(!(nelem&(ARRAY_SCAN|ARRAY_UNDEF)) && (sub=nv_getsub(np)))
sub = strdup(sub);
sub = sh_strdup(sub);
ar = (struct index_array*)ap;
if(!is_associative(ap))
ar->bits = (unsigned char*)&ar->val[ar->maxi];
@ -901,9 +898,7 @@ int nv_atypeindex(Namval_t *np, const char *tname)
errormsg(SH_DICT,ERROR_exit(1),e_notenum,tp->nvname);
if(!ap)
ap = array_grow(np,ap,1);
ap->xp = calloc(NV_MINSZ,1);
if(!ap->xp)
sh_outofmemory();
ap->xp = sh_calloc(NV_MINSZ,1);
np = nv_namptr(ap->xp,0);
np->nvname = tp->nvname;
nv_onattr(np,NV_MINIMAL);
@ -1368,9 +1363,7 @@ static void array_fixed_setdata(Namval_t *np,Namarr_t* ap,struct fixed_array* fp
ap->nelem = 1;
fp->size = fp->ptr?sizeof(void*):nv_datasize(np,0);
ap->nelem = n;
fp->data = (char*)calloc(fp->nelem,fp->size);
if(!fp->data)
sh_outofmemory();
fp->data = (char*)sh_calloc(fp->nelem,fp->size);
if(fp->ptr)
{
char **cp = (char**)fp->data;
@ -1393,8 +1386,7 @@ static int array_fixed_init(Namval_t *np, char *sub, char *cp)
if(*ep)
return(0);
sz = sizeof(struct fixed_array)+ 3*n*sizeof(int);
if(!(ap=newof(NIL(Namarr_t*),Namarr_t,1,sz)))
return(0);
ap = sh_newof(NIL(Namarr_t*),Namarr_t,1,sz);
ap->hdr.disc = &array_disc;
ap->hdr.dsize = sizeof(Namarr_t)+sz;
ap->hdr.nofree &= ~1;
@ -1655,19 +1647,15 @@ void *nv_associative(register Namval_t *np,const char *sp,int mode)
switch(mode)
{
case NV_AINIT:
if(ap = (struct assoc_array*)calloc(1,sizeof(struct assoc_array)))
{
ap->header.table = dtopen(&_Nvdisc,Dtoset);
dtuserdata(ap->header.table,&sh,1);
ap->cur = 0;
ap->pos = 0;
ap->header.hdr.disc = &array_disc;
nv_disc(np,(Namfun_t*)ap, NV_FIRST);
ap->header.hdr.dsize = sizeof(struct assoc_array);
ap->header.hdr.nofree &= ~1;
}
else
sh_outofmemory();
ap = (struct assoc_array*)sh_calloc(1,sizeof(struct assoc_array));
ap->header.table = dtopen(&_Nvdisc,Dtoset);
dtuserdata(ap->header.table,&sh,1);
ap->cur = 0;
ap->pos = 0;
ap->header.hdr.disc = &array_disc;
nv_disc(np,(Namfun_t*)ap, NV_FIRST);
ap->header.hdr.dsize = sizeof(struct assoc_array);
ap->header.hdr.nofree &= ~1;
return((void*)ap);
case NV_ADELETE:
if(ap->cur)