kconfig: make file::name a flexible array member

Call malloc() just once to allocate needed memory.

Signed-off-by: Masahiro Yamada <masahiroy@kernel.org>
This commit is contained in:
Masahiro Yamada 2024-02-03 00:58:15 +09:00
parent 8facc5f319
commit 6676c5bc15
2 changed files with 6 additions and 3 deletions

View File

@ -19,7 +19,7 @@ extern "C" {
struct file {
struct file *next;
const char *name;
char name[];
};
typedef enum tristate {

View File

@ -13,6 +13,7 @@
struct file *file_lookup(const char *name)
{
struct file *file;
size_t len;
for (file = file_list; file; file = file->next) {
if (!strcmp(name, file->name)) {
@ -20,9 +21,11 @@ struct file *file_lookup(const char *name)
}
}
file = xmalloc(sizeof(*file));
len = strlen(name);
file = xmalloc(sizeof(*file) + len + 1);
memset(file, 0, sizeof(*file));
file->name = xstrdup(name);
memcpy(file->name, name, len);
file->name[len] = '\0';
file->next = file_list;
file_list = file;