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

@ -181,8 +181,7 @@ getaddrinfo(const char* node, const char* service, const struct addrinfo* hint,
errno = EADDRNOTAVAIL;
return EAI_SYSTEM;
}
if (!(ap = newof(0, struct addrinfo, 1, sizeof(struct sockaddr_in))))
return EAI_SYSTEM;
ap = sh_newof(0, struct addrinfo, 1, sizeof(struct sockaddr_in));
if (hint)
*ap = *hint;
ap->ai_family = hp->h_addrtype;
@ -264,13 +263,12 @@ inetopen(const char* path, int flags, Inetintr_f onintr, void* handle)
}
if(flags==O_NONBLOCK)
return 1;
if (!(s = strdup(path)))
return -1;
s = sh_strdup(path);
if (t = strchr(s, '/'))
{
*t++ = 0;
if (streq(s, "local"))
s = strdup("localhost");
s = sh_strdup("localhost");
fd = getaddrinfo(s, t, &hint, &addr);
}
else
@ -413,9 +411,7 @@ int sh_iovalidfd(Shell_t *shp, int fd)
if(n > max)
n = max;
max = shp->gd->lim.open_max;
shp->sftable = (Sfio_t**)calloc((n+1)*(sizeof(int*)+sizeof(Sfio_t*)+1),1);
if(!shp->sftable)
sh_outofmemory();
shp->sftable = (Sfio_t**)sh_calloc((n+1)*(sizeof(int*)+sizeof(Sfio_t*)+1),1);
if(max)
memcpy(shp->sftable,sftable,max*sizeof(Sfio_t*));
shp->fdptrs = (int**)(&shp->sftable[n]);
@ -462,9 +458,7 @@ int sh_inuse(Shell_t *shp, int fd)
void sh_ioinit(Shell_t *shp)
{
filemapsize = 8;
filemap = (struct fdsave*)malloc(filemapsize*sizeof(struct fdsave));
if(!filemap)
sh_outofmemory();
filemap = (struct fdsave*)sh_malloc(filemapsize*sizeof(struct fdsave));
sh_iovalidfd(shp,16);
shp->sftable[0] = sfstdin;
shp->sftable[1] = sfstdout;
@ -474,10 +468,8 @@ void sh_ioinit(Shell_t *shp)
sh_iostream(shp,1);
/* all write streams are in the same pool and share outbuff */
shp->outpool = sfopen(NIL(Sfio_t*),NIL(char*),"sw"); /* pool identifier */
shp->outbuff = (char*)malloc(IOBSIZE+4);
shp->errbuff = (char*)malloc(IOBSIZE/4);
if(!shp->outbuff || !shp->errbuff)
sh_outofmemory();
shp->outbuff = (char*)sh_malloc(IOBSIZE+4);
shp->errbuff = (char*)sh_malloc(IOBSIZE/4);
sfsetbuf(sfstderr,shp->errbuff,IOBSIZE/4);
sfsetbuf(sfstdout,shp->outbuff,IOBSIZE);
sfpool(sfstdout,shp->outpool,SF_WRITE);
@ -557,8 +549,7 @@ Sfio_t *sh_iostream(Shell_t *shp, register int fd)
}
if(status&IOREAD)
{
if(!(bp = (char *)malloc(IOBSIZE+1)))
return(NIL(Sfio_t*));
bp = (char *)sh_malloc(IOBSIZE+1);
flags |= SF_READ;
if(!(status&IOWRITE))
flags &= ~SF_WRITE;
@ -575,7 +566,7 @@ Sfio_t *sh_iostream(Shell_t *shp, register int fd)
}
else if(!(iop=sfnew((fd<=2?iop:0),bp,IOBSIZE,fd,flags)))
return(NIL(Sfio_t*));
dp = newof(0,struct Iodisc,1,0);
dp = sh_newof(0,struct Iodisc,1,0);
dp->sh = shp;
if(status&IOREAD)
{
@ -1573,9 +1564,7 @@ static int io_heredoc(Shell_t *shp,register struct ionod *iop, const char *name,
sfclose(infile);
if(sffileno(tmp)>0)
{
sfsetbuf(tmp,malloc(IOBSIZE+1),IOBSIZE);
if(!tmp)
sh_outofmemory();
sfsetbuf(tmp,sh_malloc(IOBSIZE+1),IOBSIZE);
sfset(tmp,SF_MALLOC,1);
}
sfseek(shp->heredocs,off,SEEK_SET);
@ -1639,8 +1628,7 @@ void sh_iosave(Shell_t *shp, register int origfd, int oldtop, char *name)
char *oldend = (char*)&filemap[filemapsize];
long moved;
filemapsize += 8;
if(!(filemap = (struct fdsave*)realloc(filemap,filemapsize*sizeof(struct fdsave))))
sh_outofmemory();
filemap = (struct fdsave*)sh_realloc(filemap,filemapsize*sizeof(struct fdsave));
if(moved = (char*)filemap - oldptr)
{
for(savefd=shp->gd->lim.open_max; --savefd>=0; )
@ -2294,8 +2282,7 @@ Sfio_t *sh_sfeval(register char *argv[])
if(argv[1])
{
register struct eval *ep;
if(!(ep = new_of(struct eval,0)))
return(NIL(Sfio_t*));
ep = new_of(struct eval,0);
ep->disc = eval_disc;
ep->argv = argv;
ep->slen = -1;
@ -2354,8 +2341,7 @@ static Sfio_t *subopen(Shell_t *shp,Sfio_t* sp, off_t offset, long size)
register struct subfile *disp;
if(sfseek(sp,offset,SEEK_SET) <0)
return(NIL(Sfio_t*));
if(!(disp = (struct subfile*)malloc(sizeof(struct subfile)+IOBSIZE+1)))
return(NIL(Sfio_t*));
disp = (struct subfile*)sh_malloc(sizeof(struct subfile)+IOBSIZE+1);
disp->disc = sub_disc;
disp->oldsp = sp;
disp->offset = offset;