summaryrefslogtreecommitdiffstats
path: root/src/shared/bootspec.c
diff options
context:
space:
mode:
authorZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-03-08 14:37:26 +0100
committerZbigniew Jędrzejewski-Szmek <zbyszek@in.waw.pl>2019-03-12 13:32:45 +0100
commit388d2993ec48ad1c8249b42f4494dfd473723518 (patch)
treed61ca82c7b4582c0ff02cb6b993125c83e1bf38e /src/shared/bootspec.c
parentboot: avoid 32-bit calculation for a 64-bit lvalue (diff)
downloadsystemd-388d2993ec48ad1c8249b42f4494dfd473723518.tar.xz
systemd-388d2993ec48ad1c8249b42f4494dfd473723518.zip
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.
Diffstat (limited to '')
-rw-r--r--src/shared/bootspec.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c
index afcf6f7ac1..64b2574a18 100644
--- a/src/shared/bootspec.c
+++ b/src/shared/bootspec.c
@@ -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(