Add emalloc2() -- like calloc() but w/o the bzero and with error/oflow

checking.
This commit is contained in:
Todd C. Miller
2003-03-12 21:41:22 +00:00
parent 8e041c3f35
commit 8b4248529e
2 changed files with 42 additions and 0 deletions

41
alloc.c
View File

@@ -55,6 +55,7 @@
#if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS) #if defined(HAVE_MALLOC_H) && !defined(STDC_HEADERS)
# include <malloc.h> # include <malloc.h>
#endif /* HAVE_MALLOC_H && !STDC_HEADERS */ #endif /* HAVE_MALLOC_H && !STDC_HEADERS */
#include <limits.h>
#include "sudo.h" #include "sudo.h"
@@ -62,6 +63,18 @@
static const char rcsid[] = "$Sudo$"; static const char rcsid[] = "$Sudo$";
#endif /* lint */ #endif /* lint */
#ifndef SIZE_MAX
# ifdef SIZE_T_MAX
# define SIZE_MAX SIZE_T_MAX
# else
# ifdef ULONG_MAX
# define SIZE_MAX ULONG_MAX
# else
# define SIZE_MAX ((unsigned long)-1)
# endif /* ULONG_MAX */
# endif /* SIZE_T_MAX */
#endif /* SIZE_MAX */
extern char **Argv; /* from sudo.c */ extern char **Argv; /* from sudo.c */
/* /*
@@ -86,6 +99,34 @@ emalloc(size)
return(ptr); return(ptr);
} }
/*
* emalloc2() allocates nmemb * size bytes and exits with an error
* if overflow would occur or if the system malloc(3) fails.
*/
VOID *
emalloc2(nmemb, size)
size_t nmemb;
size_t size;
{
VOID *ptr;
if (nmemb == 0 || size == 0) {
(void) fprintf(stderr, "%s: internal error, tried to malloc(0)\n",
Argv[0]);
exit(1);
}
if (nmemb >= SIZE_MAX / size) {
(void) fprintf(stderr, "%s: internal error, emalloc2() overflow\n",
Argv[0]);
exit(1);
}
if ((ptr = (VOID *) malloc(nmemb * size)) == NULL) {
(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
exit(1);
}
return(ptr);
}
/* /*
* erealloc() calls the system realloc(3) and exits with an error if * erealloc() calls the system realloc(3) and exits with an error if
* realloc(3) fails. You can call erealloc() with a NULL pointer even * realloc(3) fails. You can call erealloc() with a NULL pointer even

1
sudo.h
View File

@@ -212,6 +212,7 @@ void pam_attempt_auth __P((void));
int yyparse __P((void)); int yyparse __P((void));
void pass_warn __P((FILE *)); void pass_warn __P((FILE *));
VOID *emalloc __P((size_t)); VOID *emalloc __P((size_t));
VOID *emalloc2 __P((size_t, size_t));
VOID *erealloc __P((VOID *, size_t)); VOID *erealloc __P((VOID *, size_t));
char *estrdup __P((const char *)); char *estrdup __P((const char *));
int easprintf __P((char **, const char *, ...)); int easprintf __P((char **, const char *, ...));