util: exit in string_* and parray_* functions when allocation fails.

master
Miroslav Lichvar 2015-08-28 17:06:31 +02:00 committed by Richard Cochran
parent e4325ee3fb
commit 16bb89d5bf
2 changed files with 31 additions and 26 deletions

21
util.c
View File

@ -426,8 +426,10 @@ char *string_newf(const char *format, ...)
char *s; char *s;
va_start(ap, format); va_start(ap, format);
if (vasprintf(&s, format, ap) < 0) if (vasprintf(&s, format, ap) < 0) {
s = NULL; pr_err("failed to allocate memory");
exit(1);
}
va_end(ap); va_end(ap);
return s; return s;
@ -439,8 +441,7 @@ void string_append(char **s, const char *str)
len1 = strlen(*s); len1 = strlen(*s);
len2 = strlen(str); len2 = strlen(str);
*s = realloc(*s, len1 + len2 + 1); *s = xrealloc(*s, len1 + len2 + 1);
if (*s)
memcpy((*s) + len1, str, len2 + 1); memcpy((*s) + len1, str, len2 + 1);
} }
@ -461,18 +462,18 @@ void string_appendf(char **s, const char *format, ...)
return; return;
} }
*s = realloc(*s, len1 + len2 + 1); *s = xrealloc(*s, len1 + len2 + 1);
if (*s)
memcpy((*s) + len1, s2, len2 + 1); memcpy((*s) + len1, s2, len2 + 1);
free(s2); free(s2);
} }
void **parray_new(void) void **parray_new(void)
{ {
void **a = malloc(sizeof(*a)); void **a;
if (a) a = xmalloc(sizeof(*a));
*a = NULL; *a = NULL;
return a; return a;
} }
@ -502,9 +503,7 @@ void parray_extend(void ***a, ...)
if (alloced < len + ilen) { if (alloced < len + ilen) {
while (alloced < len + ilen) while (alloced < len + ilen)
alloced *= 2; alloced *= 2;
*a = realloc(*a, alloced * sizeof **a); *a = xrealloc(*a, alloced * sizeof **a);
if (!*a)
return;
} }
va_start(ap, a); va_start(ap, a);

30
util.h
View File

@ -292,11 +292,12 @@ void *xrealloc(void *ptr, size_t size);
char *xstrdup(const char *s); 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 format printf() format string.
* @param ... printf() arguments. * @param ... printf() arguments.
* @return Pointer to the allocated string, NULL on error. * @return Pointer to the allocated string.
*/ */
#ifdef __GNUC__ #ifdef __GNUC__
__attribute__ ((format (printf, 1, 2))) __attribute__ ((format (printf, 1, 2)))
@ -304,9 +305,10 @@ __attribute__ ((format (printf, 1, 2)))
char *string_newf(const char *format, ...); 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. * @param str String appended to s.
*/ */
void string_append(char **s, const char *str); 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))) __attribute__ ((format (printf, 2, 3)))
#endif #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 format printf() format string.
* @param ... printf() arguments. * @param ... printf() arguments.
*/ */
void string_appendf(char **s, const char *format, ...); 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); void **parray_new(void);
/** /**
* Append pointer to a NULL-terminated pointer array. The array is reallocated * 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. * @param p Pointer appended to the array.
*/ */
void parray_append(void ***a, void *p); 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 * 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. * @param ... NULL-terminated list of pointers.
*/ */
void parray_extend(void ***a, ...); void parray_extend(void ***a, ...);