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

@ -626,7 +626,7 @@ static const Namdisc_t level_disc = { sizeof(struct Level), put_level };
static struct Level *init_level(Shell_t *shp,int level)
{
struct Level *lp = newof(NiL,struct Level,1,0);
struct Level *lp = sh_newof(NiL,struct Level,1,0);
lp->maxlevel = level;
_nv_unset(SH_LEVELNOD,0);
nv_onattr(SH_LEVELNOD,NV_INT16|NV_NOFREE);
@ -833,7 +833,7 @@ static int set_instance(Shell_t *shp,Namval_t *nq, Namval_t *node, struct Namref
#endif /* SHOPT_NAMESPACE */
shp->instance = 1;
if((ap=nv_arrayptr(nq)) && (sp = nv_getsub(nq)))
sp = strdup(sp);
sp = sh_strdup(sp);
shp->instance = 0;
if(shp->var_tree!=shp->var_base && !nv_search((char*)nq,nr->root,HASH_BUCKET|HASH_NOSCOPE))
{
@ -1119,7 +1119,7 @@ int sh_exec(register const Shnode_t *t, int flags)
if(!shp->strbuf2)
shp->strbuf2 = sfstropen();
sfprintf(shp->strbuf2,"%s%s%c",NV_CLASS,nv_name(shp->namespace),0);
shp->prefix = strdup(sfstruse(shp->strbuf2));
shp->prefix = sh_strdup(sfstruse(shp->strbuf2));
nv_open(shp->prefix,shp->var_base,NV_VARNAME);
}
else
@ -1866,9 +1866,7 @@ int sh_exec(register const Shnode_t *t, int flags)
if((nsig=shp->st.trapmax*sizeof(char*))>0 || shp->st.trapcom[0])
{
nsig += sizeof(char*);
savsig = malloc(nsig);
if(!savsig)
sh_outofmemory();
savsig = sh_malloc(nsig);
memcpy(savsig,(char*)&shp->st.trapcom[0],nsig);
shp->st.otrapcom = (char**)savsig;
}
@ -2686,7 +2684,7 @@ int sh_exec(register const Shnode_t *t, int flags)
else
{
nv_offattr(L_ARGNOD,NV_NOFREE);
shp->lastarg = strdup(comn);
shp->lastarg = sh_strdup(comn);
}
}
if(!skipexitset)
@ -3112,12 +3110,10 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg)
/* save trap table */
if((nsig=shp->st.trapmax)>0 || shp->st.trapcom[0])
{
savsig = malloc(nsig * sizeof(char*));
if(!savsig)
sh_outofmemory();
savsig = sh_malloc(nsig * sizeof(char*));
/*
* the data is, usually, modified in code like:
* tmp = buf[i]; buf[i] = strdup(tmp); free(tmp);
* tmp = buf[i]; buf[i] = sh_strdup(tmp); free(tmp);
* so shp->st.trapcom needs a "deep copy" to properly save/restore pointers.
*/
for (isig = 0; isig < nsig; ++isig)
@ -3125,7 +3121,7 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg)
if(shp->st.trapcom[isig] == Empty)
savsig[isig] = Empty;
else if(shp->st.trapcom[isig])
savsig[isig] = strdup(shp->st.trapcom[isig]);
savsig[isig] = sh_strdup(shp->st.trapcom[isig]);
else
savsig[isig] = NULL;
}
@ -3169,7 +3165,7 @@ int sh_funscope(int argn, char *argv[],int(*fun)(void*),void *arg,int execflg)
np = nv_search(arg[r],shp->var_tree,HASH_NOSCOPE|NV_ADD);
if(np && (nq=*nref++))
{
np->nvalue.nrp = newof(0,struct Namref,1,0);
np->nvalue.nrp = sh_newof(0,struct Namref,1,0);
np->nvalue.nrp->np = nq;
nv_onattr(np,NV_REF|NV_NOFREE);
}