shared/bootspec: avoid going through -1 when calculating array index

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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek 2019-03-08 14:37:26 +01:00
parent 7a2cb0228c
commit 388d2993ec

View 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(