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

@ -761,11 +761,7 @@ void ed_setup(register Edit_t *ep, int fd, int reedit)
/* can't use output buffer when reading from stderr */
static char *buff;
if(!buff)
{
buff = (char*)malloc(MAXLINE);
if(!buff)
sh_outofmemory();
}
buff = (char*)sh_malloc(MAXLINE);
ep->e_outbase = ep->e_outptr = buff;
ep->e_outlast = ep->e_outptr + MAXLINE;
return;
@ -1885,7 +1881,7 @@ void ed_histlist(Edit_t *ep,int n)
void *ed_open(Shell_t *shp)
{
Edit_t *ed = newof(0,Edit_t,1,0);
Edit_t *ed = sh_newof(0,Edit_t,1,0);
ed->sh = shp;
strcpy(ed->e_macro,"_??");
return((void*)ed);

View file

@ -196,7 +196,7 @@ int ed_emacsread(void *context, int fd,char *buff,int scend, int reedit)
memset(Screen,0,sizeof(Screen));
if(!ep)
{
ep = ed->e_emacs = newof(0,Emacs_t,1,0);
ep = ed->e_emacs = sh_newof(0,Emacs_t,1,0);
ep->ed = ed;
ep->prevdirection = 1;
location.hist_command = -5;
@ -220,9 +220,7 @@ int ed_emacsread(void *context, int fd,char *buff,int scend, int reedit)
#endif /* SHOPT_MULTIBYTE */
if(!kstack)
{
kstack = (genchar*)malloc(CHARSIZE*MAXLINE);
if(!kstack)
sh_outofmemory();
kstack = (genchar*)sh_malloc(CHARSIZE*MAXLINE);
kstack[0] = '\0';
}
drawbuff = out;

View file

@ -73,7 +73,7 @@ static char *parse_subst(const char *s, struct subst *sb)
/* init "new" with empty string */
if(sb->str[1])
free(sb->str[1]);
sb->str[1] = strdup("");
sb->str[1] = sh_strdup("");
/* get delimiter */
del = *s;
@ -91,7 +91,7 @@ static char *parse_subst(const char *s, struct subst *sb)
stakputc('\0');
if(sb->str[n])
free(sb->str[n]);
sb->str[n] = strdup(stakptr(off));
sb->str[n] = sh_strdup(stakptr(off));
stakseek(off);
}
n++;
@ -241,7 +241,7 @@ int hist_expand(const char *ln, char **xp)
cp++;
n = staktell(); /* terminate string and dup */
stakputc('\0');
cc = strdup(stakptr(0));
cc = sh_strdup(stakptr(0));
stakseek(n); /* remove null byte again */
ref = sfopen(ref, cc, "s"); /* open as file */
n = 0; /* skip history file referencing */
@ -587,7 +587,7 @@ getsel:
{
/* preset old with match from !?string? */
if(!sb.str[0] && wm)
sb.str[0] = strdup(sfsetbuf(wm, (Void_t*)1, 0));
sb.str[0] = sh_strdup(sfsetbuf(wm, (Void_t*)1, 0));
cp = parse_subst(cp, &sb);
}
@ -713,7 +713,7 @@ done:
/* error? */
if(staktell() && !(flag & HIST_ERROR))
*xp = strdup(stakfreeze(1));
*xp = sh_strdup(stakfreeze(1));
/* restore shell stack */
if(off)

View file

@ -75,8 +75,8 @@
#include "FEATURE/time"
#include <error.h>
#include <ls.h>
#include "defs.h"
#if KSHELL
# include "defs.h"
# include "variables.h"
# include "path.h"
# include "builtins.h"
@ -87,7 +87,6 @@
#include "history.h"
#if !KSHELL
# define new_of(type,x) ((type*)malloc((unsigned)sizeof(type)+(x)))
# define NIL(type) ((type)0)
# define path_relative(s,x) (s,x)
# ifdef __STDC__
@ -98,7 +97,7 @@
# define e_unknown "unknown"
# define sh_translate(x) (x)
char login_sh = 0;
char hist_fname[] = "/.history";
const char hist_fname[] = "/.history";
#endif /* KSHELL */
#ifndef O_BINARY
@ -145,7 +144,7 @@ static History_t *hist_ptr;
else
cp = "unknown";
}
logname = strdup(cp);
logname = sh_strdup(cp);
if((acctfd=sh_open(acctfile,
O_BINARY|O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR))>=0 &&
(unsigned)acctfd < 10)
@ -306,11 +305,7 @@ retry:
else
maxlines = HIST_DFLT;
for(histmask=16;histmask <= maxlines; histmask <<=1 );
if(!(hp=new_of(History_t,(--histmask)*sizeof(off_t))))
{
close(fd);
return(0);
}
hp = new_of(History_t,(--histmask)*sizeof(off_t));
shgd->hist_ptr = hist_ptr = hp;
hp->histshell = (void*)shp;
hp->histsize = maxlines;
@ -320,7 +315,7 @@ retry:
hp->histind = 1;
hp->histcmds[1] = 2;
hp->histcnt = 2;
hp->histname = strdup(histname);
hp->histname = sh_strdup(histname);
hp->histdisc = hist_disc;
if(hsize==0)
{
@ -395,7 +390,7 @@ retry:
if(fd>=0)
{
fcntl(fd,F_SETFD,FD_CLOEXEC);
hp->tty = strdup(isatty(2)?ttyname(2):"notty");
hp->tty = sh_strdup(isatty(2)?ttyname(2):"notty");
hp->auditfp = sfnew((Sfio_t*)0,NULL,-1,fd,SF_WRITE);
}
}
@ -471,9 +466,7 @@ static History_t* hist_trim(History_t *hp, int n)
int fd;
char *last, *name=hist_old->histname;
close(sffileno(hist_old->histfp));
tmpname = (char*)malloc(strlen(name)+14);
if(!tmpname)
sh_outofmemory();
tmpname = (char*)sh_malloc(strlen(name)+14);
if(last = strrchr(name,'/'))
{
*last = 0;

View file

@ -231,10 +231,8 @@ int ed_viread(void *context, int fd, register char *shbuf, int nchar, int reedit
#endif /* SHOPT_RAWONLY */
if(!vp)
{
ed->e_vi = vp = newof(0,Vi_t,1,0);
vp->lastline = (genchar*)malloc(MAXLINE*CHARSIZE);
if(!vp->lastline)
sh_outofmemory();
ed->e_vi = vp = sh_newof(0,Vi_t,1,0);
vp->lastline = (genchar*)sh_malloc(MAXLINE*CHARSIZE);
vp->direction = -1;
vp->ed = ed;
}
@ -383,17 +381,9 @@ int ed_viread(void *context, int fd, register char *shbuf, int nchar, int reedit
window[0] = '\0';
if(!yankbuf)
{
yankbuf = (genchar*)malloc(MAXLINE*CHARSIZE);
if(!yankbuf)
sh_outofmemory();
}
yankbuf = (genchar*)sh_malloc(MAXLINE*CHARSIZE);
if(!vp->lastline)
{
vp->lastline = (genchar*)malloc(MAXLINE*CHARSIZE);
if(!vp->lastline)
sh_outofmemory();
}
vp->lastline = (genchar*)sh_malloc(MAXLINE*CHARSIZE);
if( vp->last_cmd == '\0' )
{
/*** first time for this shell ***/