]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
bootctl: fix case-sensitive comparisons in reporting bootloader entries
authorksaleem <ksaleem@digitalocean.com>
Wed, 6 Dec 2023 16:44:24 +0000 (11:44 -0500)
committerLuca Boccassi <luca.boccassi@gmail.com>
Mon, 11 Dec 2023 10:51:56 +0000 (10:51 +0000)
Fixes #30159

src/shared/bootspec.c
src/test/test-bootspec.c

index a38911603d0ec74a3added780300371da1e904c4..f4b2fdc5d142b02889269b4ad1b1c94c6a317df3 100644 (file)
@@ -1205,8 +1205,8 @@ BootEntry* boot_config_find_entry(BootConfig *config, const char *id) {
         assert(id);
 
         for (size_t j = 0; j < config->n_entries; j++)
-                if (streq_ptr(config->entries[j].id, id) ||
-                    streq_ptr(config->entries[j].id_old, id))
+                if (strcaseeq_ptr(config->entries[j].id, id) ||
+                    strcaseeq_ptr(config->entries[j].id_old, id))
                         return config->entries + j;
 
         return NULL;
index 67fa8beea91ed3e4092b836394c32b35ef438c04..18611fc051fdd6fb54e9b9e4d31b05b10320abc5 100644 (file)
@@ -149,4 +149,63 @@ TEST_RET(bootspec_extract_tries) {
         return 0;
 }
 
+TEST_RET(bootspec_boot_config_find_entry) {
+
+        static const struct {
+                const char *fname;
+                const char *contents;
+        } entries[] = {
+                {
+                        .fname = "a-10.conf",
+                        .contents =
+                        "title A\n"
+                        "version 10\n"
+                        "machine-id dd235d00696545768f6f693bfd23b15f\n",
+                },
+                {
+                        .fname = "a-05.conf",
+                        .contents =
+                        "title A\n"
+                        "version 10\n"
+                        "machine-id dd235d00696545768f6f693bfd23b15f\n",
+                },
+        };
+
+        _cleanup_(rm_rf_physical_and_freep) char *d = NULL;
+        _cleanup_(boot_config_free) BootConfig config = BOOT_CONFIG_NULL;
+
+        assert_se(mkdtemp_malloc("/tmp/bootspec-testXXXXXX", &d) >= 0);
+
+        for (size_t i = 0; i < ELEMENTSOF(entries); i++) {
+                _cleanup_free_ char *j = NULL;
+
+                j = path_join(d, "/loader/entries/", entries[i].fname);
+                assert_se(j);
+
+                assert_se(write_string_file(j, entries[i].contents, WRITE_STRING_FILE_CREATE|WRITE_STRING_FILE_MKDIR_0755) >= 0);
+        }
+
+        assert_se(boot_config_load(&config, d, NULL) >= 0);
+        assert_se(config.n_entries == 2);
+
+        // Test finding the first entry
+        BootEntry *entry = boot_config_find_entry(&config, "a-10.conf");
+        assert_se(entry && streq(entry->id, "a-10.conf"));
+
+        // Test finding the second entry
+        entry = boot_config_find_entry(&config, "a-05.conf");
+        assert_se(entry && streq(entry->id, "a-05.conf"));
+
+        // Test finding a non-existent entry
+        entry = boot_config_find_entry(&config, "nonexistent.conf");
+        assert_se(entry == NULL);
+
+        // Test case-insensitivity
+        entry = boot_config_find_entry(&config, "A-10.CONF");
+        assert_se(entry && streq(entry->id, "a-10.conf"));
+
+
+        return 0;
+}
+
 DEFINE_TEST_MAIN(LOG_INFO);