Argv[0]);
exit(1);
}
- if ((ptr = (VOID *) malloc(nmemb * size)) == NULL) {
+ size *= nmemb;
+ if ((ptr = (VOID *) malloc(size)) == NULL) {
(void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
exit(1);
}
return(ptr);
}
+/*
+ * erealloc3() realloc(3)s nmemb * size bytes and exits with an error
+ * if overflow would occur or if the system malloc(3)/realloc(3) fails.
+ * You can call erealloc() with a NULL pointer even if the system realloc(3)
+ * does not support this.
+ */
+VOID *
+erealloc3(ptr, nmemb, size)
+ VOID *ptr;
+ size_t nmemb;
+ size_t size;
+{
+
+ if (nmemb == 0 || size == 0) {
+ (void) fprintf(stderr, "%s: internal error, tried to realloc(0)\n",
+ Argv[0]);
+ exit(1);
+ }
+ if (nmemb >= SIZE_MAX / size) {
+ (void) fprintf(stderr, "%s: internal error, erealloc3() overflow\n",
+ Argv[0]);
+ exit(1);
+ }
+ size *= nmemb;
+ ptr = ptr ? (VOID *) realloc(ptr, size) : (VOID *) malloc(size);
+ if (ptr == NULL) {
+ (void) fprintf(stderr, "%s: cannot allocate memory!\n", Argv[0]);
+ exit(1);
+ }
+ return(ptr);
+}
+
/*
* estrdup() is like strdup(3) except that it exits with an error if
* malloc(3) fails. NOTE: unlike strdup(3), estrdup(NULL) is legal.
VOID *emalloc __P((size_t));
VOID *emalloc2 __P((size_t, size_t));
VOID *erealloc __P((VOID *, size_t));
+VOID *erealloc3 __P((VOID *, size_t, size_t));
char *estrdup __P((const char *));
int easprintf __P((char **, const char *, ...));
int evasprintf __P((char **, const char *, va_list));