Skip to content

Commit 177acf7

Browse files
Alan Coxmichal42
authored andcommitted
kconfig: Fix malloc handling in conf tools
(and get them out of the noise in the audit work) Signed-off-by: Alan Cox <alan@linux.intel.com> Signed-off-by: Michal Marek <mmarek@suse.cz>
1 parent 9a926d4 commit 177acf7

6 files changed

Lines changed: 40 additions & 19 deletions

File tree

‎scripts/kconfig/expr.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@
1313

1414
struct expr *expr_alloc_symbol(struct symbol *sym)
1515
{
16-
struct expr *e = calloc(1, sizeof(*e));
16+
struct expr *e = xcalloc(1, sizeof(*e));
1717
e->type = E_SYMBOL;
1818
e->left.sym = sym;
1919
return e;
2020
}
2121

2222
struct expr *expr_alloc_one(enum expr_type type, struct expr *ce)
2323
{
24-
struct expr *e = calloc(1, sizeof(*e));
24+
struct expr *e = xcalloc(1, sizeof(*e));
2525
e->type = type;
2626
e->left.expr = ce;
2727
return e;
2828
}
2929

3030
struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e2)
3131
{
32-
struct expr *e = calloc(1, sizeof(*e));
32+
struct expr *e = xcalloc(1, sizeof(*e));
3333
e->type = type;
3434
e->left.expr = e1;
3535
e->right.expr = e2;
@@ -38,7 +38,7 @@ struct expr *expr_alloc_two(enum expr_type type, struct expr *e1, struct expr *e
3838

3939
struct expr *expr_alloc_comp(enum expr_type type, struct symbol *s1, struct symbol *s2)
4040
{
41-
struct expr *e = calloc(1, sizeof(*e));
41+
struct expr *e = xcalloc(1, sizeof(*e));
4242
e->type = type;
4343
e->left.sym = s1;
4444
e->right.sym = s2;
@@ -66,7 +66,7 @@ struct expr *expr_copy(const struct expr *org)
6666
if (!org)
6767
return NULL;
6868

69-
e = malloc(sizeof(*org));
69+
e = xmalloc(sizeof(*org));
7070
memcpy(e, org, sizeof(*org));
7171
switch (org->type) {
7272
case E_SYMBOL:

‎scripts/kconfig/lkc.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ void menu_set_type(int type);
122122
/* util.c */
123123
struct file *file_lookup(const char *name);
124124
int file_write_dep(const char *name);
125+
void *xmalloc(size_t size);
126+
void *xcalloc(size_t nmemb, size_t size);
125127

126128
struct gstr {
127129
size_t len;

‎scripts/kconfig/menu.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ void menu_add_entry(struct symbol *sym)
4848
{
4949
struct menu *menu;
5050

51-
menu = malloc(sizeof(*menu));
51+
menu = xmalloc(sizeof(*menu));
5252
memset(menu, 0, sizeof(*menu));
5353
menu->sym = sym;
5454
menu->parent = current_menu;
@@ -531,7 +531,7 @@ static void get_prompt_str(struct gstr *r, struct property *prop,
531531
location = menu;
532532
}
533533
if (head && location) {
534-
jump = malloc(sizeof(struct jump_key));
534+
jump = xmalloc(sizeof(struct jump_key));
535535

536536
if (menu_is_visible(prop->menu)) {
537537
/*

‎scripts/kconfig/symbol.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -656,11 +656,11 @@ bool sym_set_string_value(struct symbol *sym, const char *newval)
656656
size = strlen(newval) + 1;
657657
if (sym->type == S_HEX && (newval[0] != '0' || (newval[1] != 'x' && newval[1] != 'X'))) {
658658
size += 2;
659-
sym->def[S_DEF_USER].val = val = malloc(size);
659+
sym->def[S_DEF_USER].val = val = xmalloc(size);
660660
*val++ = '0';
661661
*val++ = 'x';
662662
} else if (!oldval || strcmp(oldval, newval))
663-
sym->def[S_DEF_USER].val = val = malloc(size);
663+
sym->def[S_DEF_USER].val = val = xmalloc(size);
664664
else
665665
return true;
666666

@@ -812,7 +812,7 @@ struct symbol *sym_lookup(const char *name, int flags)
812812
hash = 0;
813813
}
814814

815-
symbol = malloc(sizeof(*symbol));
815+
symbol = xmalloc(sizeof(*symbol));
816816
memset(symbol, 0, sizeof(*symbol));
817817
symbol->name = new_name;
818818
symbol->type = S_UNKNOWN;
@@ -863,7 +863,7 @@ const char *sym_expand_string_value(const char *in)
863863
size_t reslen;
864864

865865
reslen = strlen(in) + 1;
866-
res = malloc(reslen);
866+
res = xmalloc(reslen);
867867
res[0] = '\0';
868868

869869
while ((src = strchr(in, '$'))) {
@@ -921,7 +921,7 @@ const char *sym_escape_string_value(const char *in)
921921
p++;
922922
}
923923

924-
res = malloc(reslen);
924+
res = xmalloc(reslen);
925925
res[0] = '\0';
926926

927927
strcat(res, "\"");
@@ -1228,7 +1228,7 @@ struct property *prop_alloc(enum prop_type type, struct symbol *sym)
12281228
struct property *prop;
12291229
struct property **propp;
12301230

1231-
prop = malloc(sizeof(*prop));
1231+
prop = xmalloc(sizeof(*prop));
12321232
memset(prop, 0, sizeof(*prop));
12331233
prop->type = type;
12341234
prop->sym = sym;

‎scripts/kconfig/util.c‎

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ struct file *file_lookup(const char *name)
2323
}
2424
}
2525

26-
file = malloc(sizeof(*file));
26+
file = xmalloc(sizeof(*file));
2727
memset(file, 0, sizeof(*file));
2828
file->name = file_name;
2929
file->next = file_list;
@@ -81,7 +81,7 @@ int file_write_dep(const char *name)
8181
struct gstr str_new(void)
8282
{
8383
struct gstr gs;
84-
gs.s = malloc(sizeof(char) * 64);
84+
gs.s = xmalloc(sizeof(char) * 64);
8585
gs.len = 64;
8686
gs.max_width = 0;
8787
strcpy(gs.s, "\0");
@@ -138,3 +138,22 @@ const char *str_get(struct gstr *gs)
138138
return gs->s;
139139
}
140140

141+
void *xmalloc(size_t size)
142+
{
143+
void *p = malloc(size);
144+
if (p)
145+
return p;
146+
fprintf(stderr, "Out of memory.\n");
147+
exit(1);
148+
}
149+
150+
void *xcalloc(size_t nmemb, size_t size)
151+
{
152+
void *p = calloc(nmemb, size);
153+
if (p)
154+
return p;
155+
fprintf(stderr, "Out of memory.\n");
156+
exit(1);
157+
}
158+
159+

‎scripts/kconfig/zconf.l‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ static void zconf_endfile(void);
4040

4141
static void new_string(void)
4242
{
43-
text = malloc(START_STRSIZE);
43+
text = xmalloc(START_STRSIZE);
4444
text_asize = START_STRSIZE;
4545
text_size = 0;
4646
*text = 0;
@@ -62,7 +62,7 @@ static void append_string(const char *str, int size)
6262

6363
static void alloc_string(const char *str, int size)
6464
{
65-
text = malloc(size + 1);
65+
text = xmalloc(size + 1);
6666
memcpy(text, str, size);
6767
text[size] = 0;
6868
}
@@ -288,7 +288,7 @@ void zconf_initscan(const char *name)
288288
exit(1);
289289
}
290290

291-
current_buf = malloc(sizeof(*current_buf));
291+
current_buf = xmalloc(sizeof(*current_buf));
292292
memset(current_buf, 0, sizeof(*current_buf));
293293

294294
current_file = file_lookup(name);
@@ -299,7 +299,7 @@ void zconf_nextfile(const char *name)
299299
{
300300
struct file *iter;
301301
struct file *file = file_lookup(name);
302-
struct buffer *buf = malloc(sizeof(*buf));
302+
struct buffer *buf = xmalloc(sizeof(*buf));
303303
memset(buf, 0, sizeof(*buf));
304304

305305
current_buf->state = YY_CURRENT_BUFFER;

0 commit comments

Comments
 (0)