Skip to content

Commit

Permalink
kconfig: add xrealloc() helper
Browse files Browse the repository at this point in the history
We already have xmalloc(), xcalloc().  Add xrealloc() as well
to save tedious error handling.

Signed-off-by: Masahiro Yamada <[email protected]>
  • Loading branch information
masahir0y committed Feb 10, 2018
1 parent 9e3e10c commit d717f24
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 5 deletions.
2 changes: 1 addition & 1 deletion scripts/kconfig/confdata.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
1 change: 1 addition & 0 deletions scripts/kconfig/lkc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion scripts/kconfig/nconf.gui.c
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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: */
Expand Down
2 changes: 1 addition & 1 deletion scripts/kconfig/symbol.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
11 changes: 10 additions & 1 deletion scripts/kconfig/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
}
2 changes: 1 addition & 1 deletion scripts/kconfig/zconf.l
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit d717f24

Please sign in to comment.