diff --git a/util.c b/util.c index 284657b..e7245d6 100644 --- a/util.c +++ b/util.c @@ -426,8 +426,10 @@ char *string_newf(const char *format, ...) char *s; va_start(ap, format); - if (vasprintf(&s, format, ap) < 0) - s = NULL; + if (vasprintf(&s, format, ap) < 0) { + pr_err("failed to allocate memory"); + exit(1); + } va_end(ap); return s; @@ -439,9 +441,8 @@ void string_append(char **s, const char *str) len1 = strlen(*s); len2 = strlen(str); - *s = realloc(*s, len1 + len2 + 1); - if (*s) - memcpy((*s) + len1, str, len2 + 1); + *s = xrealloc(*s, len1 + len2 + 1); + memcpy((*s) + len1, str, len2 + 1); } void string_appendf(char **s, const char *format, ...) @@ -461,18 +462,18 @@ void string_appendf(char **s, const char *format, ...) return; } - *s = realloc(*s, len1 + len2 + 1); - if (*s) - memcpy((*s) + len1, s2, len2 + 1); + *s = xrealloc(*s, len1 + len2 + 1); + memcpy((*s) + len1, s2, len2 + 1); free(s2); } void **parray_new(void) { - void **a = malloc(sizeof(*a)); + void **a; + + a = xmalloc(sizeof(*a)); + *a = NULL; - if (a) - *a = NULL; return a; } @@ -502,9 +503,7 @@ void parray_extend(void ***a, ...) if (alloced < len + ilen) { while (alloced < len + ilen) alloced *= 2; - *a = realloc(*a, alloced * sizeof **a); - if (!*a) - return; + *a = xrealloc(*a, alloced * sizeof **a); } va_start(ap, a); diff --git a/util.h b/util.h index 8c3aaff..cc521b6 100644 --- a/util.h +++ b/util.h @@ -292,11 +292,12 @@ void *xrealloc(void *ptr, size_t size); char *xstrdup(const char *s); /** - * Get an allocated and formatted string. This is a wrapper around asprintf(). + * Get an allocated and formatted string. This is a wrapper around asprintf() + * that terminates the process on errors. * * @param format printf() format string. * @param ... printf() arguments. - * @return Pointer to the allocated string, NULL on error. + * @return Pointer to the allocated string. */ #ifdef __GNUC__ __attribute__ ((format (printf, 1, 2))) @@ -304,9 +305,10 @@ __attribute__ ((format (printf, 1, 2))) char *string_newf(const char *format, ...); /** - * Reallocate a string and append another string to it. + * Reallocate a string and append another string to it. The process is + * terminated when the allocation fails. * - * @param s String that should be extended, set to NULL on error. + * @param s String that should be extended. * @param str String appended to s. */ void string_append(char **s, const char *str); @@ -314,26 +316,29 @@ void string_append(char **s, const char *str); __attribute__ ((format (printf, 2, 3))) #endif /** - * Reallocate a string and append a formatted string to it. + * Reallocate a string and append a formatted string to it. The process is + * terminated when the allocation fails. * - * @param s String that should be extended, set to NULL on error. + * @param s String that should be extended. * @param format printf() format string. * @param ... printf() arguments. */ void string_appendf(char **s, const char *format, ...); /** - * Get an empty array of pointers terminated by NULL. + * Get an empty array of pointers terminated by NULL. The process is terminated + * when the allocation fails. * - * @return Pointer to the allocated array, NULL on error. + * @return Pointer to the allocated array. */ void **parray_new(void); /** * Append pointer to a NULL-terminated pointer array. The array is reallocated - * in exponentially increasing sizes. + * in exponentially increasing sizes. The process is terminated when the + * allocation fails. * - * @param a Pointer to pointer array, set to NULL on error. + * @param a Pointer to pointer array. * @param p Pointer appended to the array. */ void parray_append(void ***a, void *p); @@ -341,9 +346,10 @@ void parray_append(void ***a, void *p); /** * Append pointers to a NULL-terminated pointer array. The array is reallocated - * in exponentially increasing sizes. + * in exponentially increasing sizes. The process is terminated when the + * allocation fails. * - * @param a Pointer to pointer array, set to NULL on error. + * @param a Pointer to pointer array. * @param ... NULL-terminated list of pointers. */ void parray_extend(void ***a, ...);