We already have xmalloc(), xcalloc(), and xrealloc((). Add xstrdup() as well to save tedious error handling.
Showing the changes to scripts (6 of 6 files). Show all
Unified · Split
| @@ -178,7 +178,7 @@ static int conf_set_sym_val(struct symbol *sym, int def, int def_flags, char *p) · 177 unmodified lines | ||
| 178 | 178 | case S_HEX: |
| 179 | 179 | done: |
| 180 | 180 | if (sym_string_valid(sym, p)) { |
| 181 | − sym->def[def].val = strdup(p); | |
| 181 | + sym->def[def].val = xstrdup(p); | |
| 182 | 182 | sym->flags |= def_flags; |
| 183 | 183 | } else { |
| 184 | 184 | if (def != S_DEF_AUTO) |
| @@ -101,7 +101,7 @@ static struct message *message__new(const char *msg, char *option, · 100 unmodified lines | ||
| 101 | 101 | if (self->files == NULL) |
| 102 | 102 | goto out_fail; |
| 103 | 103 | |
| 104 | − self->msg = strdup(msg); | |
| 104 | + self->msg = xstrdup(msg); | |
| 105 | 105 | if (self->msg == NULL) |
| 106 | 106 | goto out_fail_msg; |
| 107 | 107 | |
| @@ -115,6 +115,7 @@ int file_write_dep(const char *name); · 114 unmodified lines | ||
| 115 | 115 | void *xmalloc(size_t size); |
| 116 | 116 | void *xcalloc(size_t nmemb, size_t size); |
| 117 | 117 | void *xrealloc(void *p, size_t size); |
| 118 | +char *xstrdup(const char *s); | |
| 118 | 119 | |
| 119 | 120 | struct gstr { |
| 120 | 121 | size_t len; |
| @@ -183,7 +183,7 @@ static void sym_validate_range(struct symbol *sym) · 182 unmodified lines | ||
| 183 | 183 | sprintf(str, "%lld", val2); |
| 184 | 184 | else |
| 185 | 185 | sprintf(str, "0x%llx", val2); |
| 186 | − sym->curr.val = strdup(str); | |
| 186 | + sym->curr.val = xstrdup(str); | |
| 187 | 187 | } |
| 188 | 188 | |
| 189 | 189 | static void sym_set_changed(struct symbol *sym) |
| @@ -849,7 +849,7 @@ struct symbol *sym_lookup(const char *name, int flags) · 659 unmodified lines | ||
| 849 | 849 | : !(symbol->flags & (SYMBOL_CONST|SYMBOL_CHOICE)))) |
| 850 | 850 | return symbol; |
| 851 | 851 | } |
| 852 | − new_name = strdup(name); | |
| 852 | + new_name = xstrdup(name); | |
| 853 | 853 | } else { |
| 854 | 854 | new_name = NULL; |
| 855 | 855 | hash = 0; |
| @@ -154,3 +154,14 @@ void *xrealloc(void *p, size_t size) · 153 unmodified lines | ||
| 154 | 154 | fprintf(stderr, "Out of memory.\n"); |
| 155 | 155 | exit(1); |
| 156 | 156 | } |
| 157 | + | |
| 158 | +char *xstrdup(const char *s) | |
| 159 | +{ | |
| 160 | + char *p; | |
| 161 | + | |
| 162 | + p = strdup(s); | |
| 163 | + if (p) | |
| 164 | + return p; | |
| 165 | + fprintf(stderr, "Out of memory.\n"); | |
| 166 | + exit(1); | |
| 167 | +} | |
| @@ -127,7 +127,7 @@ no_mainmenu_stmt: /* empty */ · 126 unmodified lines | ||
| 127 | 127 | * later regardless of whether it comes from the 'prompt' in |
| 128 | 128 | * mainmenu_stmt or here |
| 129 | 129 | */ |
| 130 | − menu_add_prompt(P_MENU, strdup("Linux Kernel Configuration"), NULL); | |
| 130 | + menu_add_prompt(P_MENU, xstrdup("Linux Kernel Configuration"), NULL); | |
| 131 | 131 | }; |
| 132 | 132 | |
| 133 | 133 | |