]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
shared/bootspec: avoid going through -1 when calculating array index 11933/head
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Fri, 8 Mar 2019 13:37:26 +0000 (14:37 +0100)
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>
Tue, 12 Mar 2019 12:32:45 +0000 (13:32 +0100)
Coverity was complaining in CID#1399407 that config->entries might be used
while NULL. Let's add an assert to make sure it's not.

Also, let's quit early if we have no entries to loop through. The code was
not incorrect, but it's cleaner to avoid any negative indices.

src/shared/bootspec.c

index afcf6f7ac13eaabcf9fb7a2207487fe59e908e50..64b2574a182910f443fe23738504facb5c10b9f5 100644 (file)
@@ -584,6 +584,12 @@ static int boot_entries_select_default(const BootConfig *config) {
         int i;
 
         assert(config);
+        assert(config->entries || config->n_entries == 0);
+
+        if (config->n_entries == 0) {
+                log_debug("Found no default boot entry :(");
+                return -1; /* -1 means "no default" */
+        }
 
         if (config->entry_oneshot)
                 for (i = config->n_entries - 1; i >= 0; i--)
@@ -609,12 +615,8 @@ static int boot_entries_select_default(const BootConfig *config) {
                                 return i;
                         }
 
-        if (config->n_entries > 0)
-                log_debug("Found default: last entry \"%s\"", config->entries[config->n_entries - 1].id);
-        else
-                log_debug("Found no default boot entry :(");
-
-        return config->n_entries - 1; /* -1 means "no default" */
+        log_debug("Found default: last entry \"%s\"", config->entries[config->n_entries - 1].id);
+        return config->n_entries - 1;
 }
 
 int boot_entries_load_config(