kconfig: add xstrdup() helper

We already have xmalloc(), xcalloc(), and xrealloc(().  Add xstrdup()
as well to save tedious error handling.

Signed-off-by: Masahiro Yamada <yamada.masahiro@socionext.com>
This commit is contained in:
Masahiro Yamada 2018-02-17 03:38:31 +09:00
parent 6c49f359ca
commit cd81fc82b9
6 changed files with 17 additions and 5 deletions

View File

@ -178,7 +178,7 @@ static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p)
case S_HEX: case S_HEX:
done: done:
if (sym_string_valid(sym, p)) { if (sym_string_valid(sym, p)) {
sym->def[def].val = strdup(p); sym->def[def].val = xstrdup(p);
sym->flags |= def_flags; sym->flags |= def_flags;
} else { } else {
if (def != S_DEF_AUTO) if (def != S_DEF_AUTO)

View File

@ -101,7 +101,7 @@ static struct message *message__new(const char *msg, char *option,
if (self->files == NULL) if (self->files == NULL)
goto out_fail; goto out_fail;
self->msg = strdup(msg); self->msg = xstrdup(msg);
if (self->msg == NULL) if (self->msg == NULL)
goto out_fail_msg; goto out_fail_msg;

View File

@ -115,6 +115,7 @@ int file_write_dep(const char *name);
void *xmalloc(size_t size); void *xmalloc(size_t size);
void *xcalloc(size_t nmemb, size_t size); void *xcalloc(size_t nmemb, size_t size);
void *xrealloc(void *p, size_t size); void *xrealloc(void *p, size_t size);
char *xstrdup(const char *s);
struct gstr { struct gstr {
size_t len; size_t len;

View File

@ -183,7 +183,7 @@ static void sym_validate_range(struct symbol *sym)
sprintf(str, "%lld", val2); sprintf(str, "%lld", val2);
else else
sprintf(str, "0x%llx", val2); sprintf(str, "0x%llx", val2);
sym->curr.val = strdup(str); sym->curr.val = xstrdup(str);
} }
static void sym_set_changed(struct symbol *sym) static void sym_set_changed(struct symbol *sym)
@ -849,7 +849,7 @@ struct symbol *sym_lookup(const char *name, int flags)
: !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE)))) : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE))))
return symbol; return symbol;
} }
new_name = strdup(name); new_name = xstrdup(name);
} else { } else {
new_name = NULL; new_name = NULL;
hash = 0; hash = 0;

View File

@ -154,3 +154,14 @@ void *xrealloc(void *p, size_t size)
fprintf(stderr, "Out of memory.\n"); fprintf(stderr, "Out of memory.\n");
exit(1); exit(1);
} }
char *xstrdup(const char *s)
{
char *p;
p = strdup(s);
if (p)
return p;
fprintf(stderr, "Out of memory.\n");
exit(1);
}

View File

@ -127,7 +127,7 @@ no_mainmenu_stmt: /* empty */
* later regardless of whether it comes from the 'prompt' in * later regardless of whether it comes from the 'prompt' in
* mainmenu_stmt or here * mainmenu_stmt or here
*/ */
menu_add_prompt(P_MENU, strdup("Linux Kernel Configuration"), NULL); menu_add_prompt(P_MENU, xstrdup("Linux Kernel Configuration"), NULL);
}; };