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

@ -185,9 +185,7 @@ static pid_t path_xargs(Shell_t *shp,const char *path, char *argv[],char *const
if(xv==&argv[shp->xargmin])
{
n = nlast*sizeof(char*);
saveargs = (char**)malloc(n);
if(!saveargs)
sh_outofmemory();
saveargs = (char**)sh_malloc(n);
memcpy((void*)saveargs, (void*)av, n);
memcpy((void*)av,(void*)avlast,n);
}
@ -567,9 +565,7 @@ char *path_fullname(Shell_t *shp,const char *name)
pwd = path_pwd(shp,1);
dirlen = strlen(pwd)+1;
}
path = (char*)malloc(len+dirlen);
if(!path)
sh_outofmemory();
path = (char*)sh_malloc(len+dirlen);
if(dirlen)
{
memcpy((void*)path,(void*)pwd,dirlen);
@ -1180,9 +1176,7 @@ pid_t path_spawn(Shell_t *shp,const char *opath,register char **argv, char **env
* The following code because execv(foo,) and execv(./foo,)
* may not yield the same results
*/
char *sp = (char*)malloc(strlen(path)+3);
if(!sp)
sh_outofmemory();
char *sp = (char*)sh_malloc(strlen(path)+3);
sp[0] = '.';
sp[1] = '/';
strcpy(sp+2,path);
@ -1358,7 +1352,7 @@ static void exscript(Shell_t *shp,register char *path,register char *argv[],char
sh_accbegin(path) ; /* reset accounting */
#endif /* SHOPT_ACCT */
shp->arglist = sh_argcreate(argv);
shp->lastarg = strdup(path);
shp->lastarg = sh_strdup(path);
/* save name of calling command */
shp->readscript = error_info.id;
/* close history file if name has changed */
@ -1495,7 +1489,7 @@ static Pathcomp_t *path_addcomp(Shell_t *shp,Pathcomp_t *first, Pathcomp_t *old,
}
}
for(pp=first, oldpp=0; pp; oldpp=pp, pp=pp->next);
pp = newof((Pathcomp_t*)0,Pathcomp_t,1,len+1);
pp = sh_newof((Pathcomp_t*)0,Pathcomp_t,1,len+1);
pp->shp = shp;
pp->refcount = 1;
memcpy((char*)(pp+1),name,len+1);
@ -1510,9 +1504,7 @@ static Pathcomp_t *path_addcomp(Shell_t *shp,Pathcomp_t *first, Pathcomp_t *old,
{
pp->dev = 1;
pp->flags |= PATH_BUILTIN_LIB;
pp->blib = pp->bbuf = malloc(sizeof(LIBCMD));
if(!pp->blib)
sh_outofmemory();
pp->blib = pp->bbuf = sh_malloc(sizeof(LIBCMD));
strcpy(pp->blib,LIBCMD);
return(first);
}
@ -1578,13 +1570,11 @@ static int path_chkpaths(Shell_t *shp,Pathcomp_t *first, Pathcomp_t* old,Pathcom
{
if(pp->bbuf)
free(pp->bbuf);
pp->blib = pp->bbuf = strdup(ep);
pp->blib = pp->bbuf = sh_strdup(ep);
}
else if(m)
{
pp->lib = (char*)malloc(cp-sp+pp->len+2);
if(!pp->lib)
sh_outofmemory();
pp->lib = (char*)sh_malloc(cp-sp+pp->len+2);
memcpy((void*)pp->lib,(void*)sp,m);
memcpy((void*)&pp->lib[m],stakptr(offset),pp->len);
pp->lib[k=m+pp->len] = '/';