From 388d2993ec48ad1c8249b42f4494dfd473723518 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Fri, 8 Mar 2019 14:37:26 +0100 Subject: [PATCH] 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. --- src/shared/bootspec.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) 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( -- 2.47.3