From 19cb99e74ac98c8aec317cca2d50a4afd30ad9ac Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 14 May 2024 15:50:09 +0800 Subject: [PATCH 1/5] shared/bootspec: add missing assertions --- src/shared/bootspec.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 0799191a99..9e8064b2be 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -880,19 +880,22 @@ static int insert_boot_entry_addon( char *location, char *cmdline) { + assert(addons); + if (!GREEDY_REALLOC(addons->items, addons->n_items + 1)) return log_oom(); - addons->items[addons->n_items] = (BootEntryAddon) { + addons->items[addons->n_items++] = (BootEntryAddon) { .location = location, .cmdline = cmdline, }; - addons->n_items++; return 0; } static void boot_entry_addons_done(BootEntryAddons *addons) { + assert(addons); + FOREACH_ARRAY(addon, addons->items, addons->n_items) { free(addon->cmdline); free(addon->location); @@ -1542,7 +1545,11 @@ static int json_addon( int r; - r = json_variant_append_arrayb(array, + assert(addon); + assert(addon_str); + + r = json_variant_append_arrayb( + array, JSON_BUILD_OBJECT( JSON_BUILD_PAIR(addon_str, JSON_BUILD_STRING(addon->location)), JSON_BUILD_PAIR("options", JSON_BUILD_STRING(addon->cmdline)))); From 73a8d8b0ca4569568aa1bc5883d4953b3f69d1f1 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 14 May 2024 15:48:50 +0800 Subject: [PATCH 2/5] shared/bootspec: inline iterator var Also, do not bump 'line' until the end of the loop. Otherwise, log_syntax() below logs about the wrong line number. --- src/shared/bootspec.c | 18 ++++++------------ 1 file changed, 6 insertions(+), 12 deletions(-) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 9e8064b2be..882d026a40 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -289,7 +289,6 @@ static int boot_entry_load_type1( BootEntry *entry) { _cleanup_(boot_entry_free) BootEntry tmp = BOOT_ENTRY_INIT(BOOT_ENTRY_CONF); - unsigned line = 1; char *c; int r; @@ -324,18 +323,16 @@ static int boot_entry_load_type1( if (!tmp.root) return log_oom(); - for (;;) { + for (unsigned line = 1;; line++) { _cleanup_free_ char *buf = NULL, *field = NULL; r = read_stripped_line(f, LONG_LINE_MAX, &buf); - if (r == 0) - break; if (r == -ENOBUFS) return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Line too long."); if (r < 0) return log_syntax(NULL, LOG_ERR, tmp.path, line, r, "Error while reading: %m"); - - line++; + if (r == 0) + break; if (IN_SET(buf[0], '#', '\0')) continue; @@ -436,25 +433,22 @@ void boot_config_free(BootConfig *config) { } int boot_loader_read_conf(BootConfig *config, FILE *file, const char *path) { - unsigned line = 1; int r; assert(config); assert(file); assert(path); - for (;;) { + for (unsigned line = 1;; line++) { _cleanup_free_ char *buf = NULL, *field = NULL; r = read_stripped_line(file, LONG_LINE_MAX, &buf); - if (r == 0) - break; if (r == -ENOBUFS) return log_syntax(NULL, LOG_ERR, path, line, r, "Line too long."); if (r < 0) return log_syntax(NULL, LOG_ERR, path, line, r, "Error while reading: %m"); - - line++; + if (r == 0) + break; if (IN_SET(buf[0], '#', '\0')) continue; From 94e9ae6f533133d2055726aa3223e3da419b4164 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 14 May 2024 15:48:15 +0800 Subject: [PATCH 3/5] shared/bootspec: use path_make_absolute where appropriate --- src/shared/bootspec.c | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 882d026a40..188613fb9e 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -79,10 +79,7 @@ static int mangle_path( assert(ret); /* Spec leaves open if prefixed with "/" or not, let's normalize that */ - if (path_is_absolute(p)) - c = strdup(p); - else - c = strjoin("/", p); + c = path_make_absolute(p, "/"); if (!c) return -ENOMEM; From 4ea554e6816da2eb595f91bf4c61a7d6e32def52 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 14 May 2024 15:49:21 +0800 Subject: [PATCH 4/5] shared/bootspec: use isempty where appropriate --- src/shared/bootspec.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 188613fb9e..5bf3464657 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -787,7 +787,7 @@ static int find_cmdline_section( /* Quick test to check if there is actual content in the addon cmdline */ t = delete_chars(word, NULL); - if (t[0] == 0) + if (isempty(t)) *ret_cmdline = NULL; else *ret_cmdline = TAKE_PTR(cmdline); From 97dfed142ddbe27202e4295d7b68de45e60dafc5 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 14 May 2024 15:49:54 +0800 Subject: [PATCH 5/5] shared/bootspec: use FOREACH_ARRAY more --- src/shared/bootspec.c | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index 5bf3464657..4bc3ae73ab 100644 --- a/src/shared/bootspec.c +++ b/src/shared/bootspec.c @@ -421,8 +421,8 @@ void boot_config_free(BootConfig *config) { free(config->entry_default); free(config->entry_selected); - for (size_t i = 0; i < config->n_entries; i++) - boot_entry_free(config->entries + i); + FOREACH_ARRAY(i, config->entries, config->n_entries) + boot_entry_free(i); free(config->entries); free(config->global_addons.items); @@ -586,8 +586,8 @@ static int boot_entries_find_type1( if (r < 0) return log_error_errno(r, "Failed to read directory '%s': %m", full); - for (size_t i = 0; i < dentries->n_entries; i++) { - const struct dirent *de = dentries->entries[i]; + FOREACH_ARRAY(i, dentries->entries, dentries->n_entries) { + const struct dirent *de = *i; _cleanup_fclose_ FILE *f = NULL; if (!dirent_is_file(de)) @@ -610,7 +610,7 @@ static int boot_entries_find_type1( r = boot_config_load_type1(config, f, root, full, de->d_name); if (r == -ENOMEM) /* ignore all other errors */ - return r; + return log_oom(); } return 0; @@ -1781,7 +1781,7 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) { } return json_variant_dump(array, json_format | JSON_FORMAT_EMPTY_ARRAY, NULL, NULL); - } else { + } else for (size_t n = 0; n < config->n_entries; n++) { r = show_boot_entry( config->entries + n, @@ -1795,7 +1795,6 @@ int show_boot_entries(const BootConfig *config, JsonFormatFlags json_format) { if (n+1 < config->n_entries) putchar('\n'); } - } return 0; }