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

@ -125,6 +125,11 @@ static const char usage[] =
#define CMIN 1
#endif
static void outofmemory(void)
{
error(ERROR_SYSTEM|ERROR_PANIC, "out of memory");
}
#if !_lib_openpty && !_lib__getpty && !defined(_pty_clone)
# if !_lib_grantpt || !_lib_unlock
# if !_lib_ptsname
@ -645,10 +650,7 @@ masterline(Sfio_t* mp, Sfio_t* lp, char* prompt, int must, int timeout, Master_t
a = roundof(bp->max - bp->buf + n, SF_BUFSIZE);
r = bp->buf;
if (!(bp->buf = vmnewof(bp->vm, bp->buf, char, a, 0)))
{
error(ERROR_SYSTEM|2, "out of space");
return 0;
}
outofmemory();
bp->max = bp->buf + a;
if (bp->buf != r)
{
@ -784,12 +786,7 @@ dialogue(Sfio_t* mp, Sfio_t* lp, int delay, int timeout)
!(cond = vmnewof(vm, 0, Cond_t, 1, 0)) ||
!(master = vmnewof(vm, 0, Master_t, 1, 0)) ||
!(master->buf = vmnewof(vm, 0, char, 2 * SF_BUFSIZE, 0)))
{
error(ERROR_SYSTEM|2, "out of space");
id = 0;
line = 0;
goto done;
}
outofmemory();
master->vm = vm;
master->cur = master->end = master->buf;
master->max = master->buf + 2 * SF_BUFSIZE - 1;
@ -840,10 +837,7 @@ dialogue(Sfio_t* mp, Sfio_t* lp, int delay, int timeout)
break;
case 'i':
if (!cond->next && !(cond->next = vmnewof(vm, 0, Cond_t, 1, 0)))
{
error(ERROR_SYSTEM|2, "out of space");
goto done;
}
outofmemory();
cond = cond->next;
cond->flags = IF;
if ((cond->prev->flags & SKIP) && !(cond->text = 0) || !(cond->text = masterline(mp, lp, 0, 0, timeout, master)))
@ -1080,7 +1074,7 @@ b_pty(int argc, char** argv, Shbltin_t* context)
if (isspace(*s))
n++;
if (!(ap = newof(0, Argv_t, 1, (n + 2) * sizeof(char*) + (s - stty + 1))))
error(ERROR_system(1), "out of space");
outofmemory();
ap->argc = n + 1;
ap->argv = (char**)(ap + 1);
ap->args = (char*)(ap->argv + n + 2);