24fce8
From 46843e71656264d98ceda79985e5d341a8d58aa7 Mon Sep 17 00:00:00 2001
24fce8
From: Robert Marshall <rmarshall@redhat.com>
24fce8
Date: Thu, 22 Dec 2016 17:52:14 -0500
24fce8
Subject: [PATCH 37/55] Fix findTemplate index logic (#1285601)
24fce8
24fce8
The fallback case where findTemplate has to look for the first entry
24fce8
contained a logic flaw that could return an incorrect index. This
24fce8
discovered index should be reduced by one for each boot entry that will
24fce8
be skipped in the final output. The flaw occurred because the index
24fce8
variable was used for the loop upper bound at the same time as it was
24fce8
being decremented within the actual loop body. The loop would thus fail
24fce8
to examine a number of boot entries equal to the total number of
24fce8
iterations the loop performed.
24fce8
24fce8
Related: rhbz#1285601
24fce8
---
24fce8
 grubby.c | 7 +++++--
24fce8
 1 file changed, 5 insertions(+), 2 deletions(-)
24fce8
24fce8
diff --git a/grubby.c b/grubby.c
24fce8
index fcca6364887..692c351ccb6 100644
24fce8
--- a/grubby.c
24fce8
+++ b/grubby.c
24fce8
@@ -2436,8 +2436,11 @@ struct singleEntry *findTemplate(struct grubConfig *cfg, const char *prefix,
24fce8
 	index = 0;
24fce8
 	while ((entry = findEntryByIndex(cfg, index))) {
24fce8
 		if (suitableImage(entry, prefix, skipRemoved, flags)) {
24fce8
-			int j;
24fce8
-			for (j = 0; j < index; j++) {
24fce8
+			int j, unmodifiedIndex;
24fce8
+
24fce8
+			unmodifiedIndex = index;
24fce8
+
24fce8
+			for (j = 0; j < unmodifiedIndex; j++) {
24fce8
 				entry2 = findEntryByIndex(cfg, j);
24fce8
 				if (entry2->skip)
24fce8
 					index--;
24fce8
-- 
24fce8
2.17.1
24fce8