From d44655a5f52f765a5f5aa86cebed06abbe64a51b Mon Sep 17 00:00:00 2001 From: Masahiro Yamada Date: Fri, 9 Feb 2018 01:19:07 +0900 Subject: [PATCH] kconfig: add xrealloc() helper We already have xmalloc(), xcalloc(). Add xrealloc() as well to save tedious error handling. Signed-off-by: Masahiro Yamada Signed-off-by: Christian Lamparter --- config/confdata.c | 2 +- config/lkc.h | 1 + config/symbol.c | 2 +- config/util.c | 11 ++++++++++- config/zconf.l | 2 +- 5 files changed, 14 insertions(+), 4 deletions(-) diff --git a/config/confdata.c b/config/confdata.c index 2e030b4..3a0fb60 100644 --- a/config/confdata.c +++ b/config/confdata.c @@ -201,7 +201,7 @@ static int add_byte(int c, char **lineptr, size_t slen, size_t *n) if (new_size > *n) { new_size += LINE_GROWTH - 1; new_size *= 2; - nline = realloc(*lineptr, new_size); + nline = xrealloc(*lineptr, new_size); if (!nline) return -1; diff --git a/config/lkc.h b/config/lkc.h index 16cb62b..4e23feb 100644 --- a/config/lkc.h +++ b/config/lkc.h @@ -114,6 +114,7 @@ struct file *file_lookup(const char *name); int file_write_dep(const char *name); void *xmalloc(size_t size); void *xcalloc(size_t nmemb, size_t size); +void *xrealloc(void *p, size_t size); struct gstr { size_t len; diff --git a/config/symbol.c b/config/symbol.c index c4409ee..60a76f9 100644 --- a/config/symbol.c +++ b/config/symbol.c @@ -936,7 +936,7 @@ const char *sym_expand_string_value(const char *in) newlen = strlen(res) + strlen(symval) + strlen(src) + 1; if (newlen > reslen) { reslen = newlen; - res = realloc(res, reslen); + res = xrealloc(res, reslen); } strcat(res, symval); diff --git a/config/util.c b/config/util.c index 0e76042..138894e 100644 --- a/config/util.c +++ b/config/util.c @@ -104,7 +104,7 @@ void str_append(struct gstr *gs, const char *s) if (s) { l = strlen(gs->s) + strlen(s) + 1; if (l > gs->len) { - gs->s = realloc(gs->s, l); + gs->s = xrealloc(gs->s, l); gs->len = l; } strcat(gs->s, s); @@ -145,3 +145,12 @@ void *xcalloc(size_t nmemb, size_t size) fprintf(stderr, "Out of memory.\n"); exit(1); } + +void *xrealloc(void *p, size_t size) +{ + p = realloc(p, size); + if (p) + return p; + fprintf(stderr, "Out of memory.\n"); + exit(1); +} diff --git a/config/zconf.l b/config/zconf.l index 5d9b060..7578853 100644 --- a/config/zconf.l +++ b/config/zconf.l @@ -52,7 +52,7 @@ static void append_string(const char *str, int size) if (new_size > text_asize) { new_size += START_STRSIZE - 1; new_size &= -START_STRSIZE; - text = realloc(text, new_size); + text = xrealloc(text, new_size); text_asize = new_size; } memcpy(text + text_size, str, size); -- 2.31.1