From: Zbigniew Jędrzejewski-Szmek Date: Fri, 8 Mar 2019 13:37:26 +0000 (+0100) Subject: shared/bootspec: avoid going through -1 when calculating array index X-Git-Tag: v242-rc1~154^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=388d2993ec48ad1c8249b42f4494dfd473723518;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/shared/bootspec.c b/src/shared/bootspec.c index afcf6f7ac13..64b2574a182 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(