kconfig: add xrealloc() helper
authorMasahiro Yamada <yamada.masahiro@socionext.com>
Thu, 8 Feb 2018 16:19:07 +0000 (01:19 +0900)
committerMasahiro Yamada <yamada.masahiro@socionext.com>
Sat, 10 Feb 2018 02:26:04 +0000 (11:26 +0900)
We already have xmalloc(), xcalloc().  Add xrealloc() as well
to save tedious error handling.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
scripts/kconfig/confdata.c
scripts/kconfig/lkc.h
scripts/kconfig/nconf.gui.c
scripts/kconfig/symbol.c
scripts/kconfig/util.c
scripts/kconfig/zconf.l

index f792739..5c12dc9 100644 (file)
@@ -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;
 
index 16cb62b..4e23feb 100644 (file)
@@ -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;
index a64b1c3..88874ac 100644 (file)
@@ -6,6 +6,7 @@
  *
  */
 #include "nconf.h"
+#include "lkc.h"
 
 /* a list of all the different widgets we use */
 attributes_t attributes[ATTR_MAX+1] = {0};
@@ -374,7 +375,7 @@ int dialog_inputbox(WINDOW *main_window,
 
        if (strlen(init)+1 > *result_len) {
                *result_len = strlen(init)+1;
-               *resultp = result = realloc(result, *result_len);
+               *resultp = result = xrealloc(result, *result_len);
        }
 
        /* find the widest line of msg: */
index c4409ee..60a76f9 100644 (file)
@@ -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);
index 0e76042..138894e 100644 (file)
@@ -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);
+}
index 0ba4900..02de6fe 100644 (file)
@@ -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);