Blame SOURCES/0211-blscfg-Don-t-attempt-to-sort-by-version-if-not-prese.patch

d9d99f
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
d9d99f
From: Javier Martinez Canillas <javierm@redhat.com>
d9d99f
Date: Wed, 18 Jul 2018 00:58:44 +0200
d9d99f
Subject: [PATCH] blscfg: Don't attempt to sort by version if not present in
d9d99f
 all BLS files
d9d99f
d9d99f
Commit a16805341cc ("blscfg: sort BLS entries by 'version' field") made to
d9d99f
sort by the version field take precedence over the BLS fragment file name.
d9d99f
d9d99f
But it also uses the lack of the version field in one BLS fragment as sort
d9d99f
criterion, which means that entries could be wrongly sorted if one of them
d9d99f
doesn't have a version field and others do.
d9d99f
d9d99f
So only sort by version if all the BLS entries have this field defined,
d9d99f
otherwise just fallback to sorting by the BLS file name.
d9d99f
d9d99f
Reported-by: Hans de Goede <hdegoede@redhat.com>
d9d99f
Suggested-by: Will Thompson <wjt@endlessm.com>
d9d99f
Signed-off-by: Javier Martinez Canillas <javierm@redhat.com>
d9d99f
---
d9d99f
 grub-core/commands/blscfg.c | 27 ++++++++++++++++-----------
d9d99f
 1 file changed, 16 insertions(+), 11 deletions(-)
d9d99f
d9d99f
diff --git a/grub-core/commands/blscfg.c b/grub-core/commands/blscfg.c
d9d99f
index 321c93069f2..69bfb5db295 100644
d9d99f
--- a/grub-core/commands/blscfg.c
d9d99f
+++ b/grub-core/commands/blscfg.c
d9d99f
@@ -324,23 +324,21 @@ finish:
d9d99f
 /* return 1: p0 is newer than p1 */
d9d99f
 /*        0: p0 and p1 are the same version */
d9d99f
 /*       -1: p1 is newer than p0 */
d9d99f
-static int bls_cmp(const void *p0, const void *p1, void *state UNUSED)
d9d99f
+static int bls_cmp(const void *p0, const void *p1, void *state)
d9d99f
 {
d9d99f
   struct bls_entry * e0 = *(struct bls_entry **)p0;
d9d99f
   struct bls_entry * e1 = *(struct bls_entry **)p1;
d9d99f
+  bool use_version = *(bool *)state;
d9d99f
   const char *v0, *v1;
d9d99f
   int r;
d9d99f
 
d9d99f
-  v0 = bls_get_val(e0, "version", NULL);
d9d99f
-  v1 = bls_get_val(e1, "version", NULL);
d9d99f
+  if (use_version) {
d9d99f
+    v0 = bls_get_val(e0, "version", NULL);
d9d99f
+    v1 = bls_get_val(e1, "version", NULL);
d9d99f
 
d9d99f
-  if (v0 && !v1)
d9d99f
-    return -1;
d9d99f
-  if (!v0 && v1)
d9d99f
-    return 1;
d9d99f
-
d9d99f
-  if ((r = vercmp(v0, v1)) != 0)
d9d99f
-    return r;
d9d99f
+    if ((r = vercmp(v0, v1)) != 0)
d9d99f
+      return r;
d9d99f
+  }
d9d99f
 
d9d99f
   return vercmp(e0->filename, e1->filename);
d9d99f
 }
d9d99f
@@ -692,6 +690,7 @@ static int find_entry (const char *filename,
d9d99f
   grub_device_t blsdir_dev = NULL;
d9d99f
   const char *blsdir = NULL;
d9d99f
   char *saved_env_buf = NULL;
d9d99f
+  bool use_version = true;
d9d99f
   int fallback = 0;
d9d99f
   int r = 0;
d9d99f
   const char *devid = grub_env_get ("boot");
d9d99f
@@ -819,7 +818,13 @@ read_fallback:
d9d99f
   }
d9d99f
 
d9d99f
   grub_dprintf ("blscfg", "Sorting %d entries\n", nentries);
d9d99f
-  grub_qsort(&entries[0], nentries, sizeof (struct bls_entry *), bls_cmp, NULL);
d9d99f
+
d9d99f
+  for (r = 0; r < nentries && use_version; r++) {
d9d99f
+    if (!bls_get_val(entries[r], "version", NULL))
d9d99f
+      use_version = false;
d9d99f
+  }
d9d99f
+
d9d99f
+  grub_qsort(&entries[0], nentries, sizeof (struct bls_entry *), bls_cmp, &use_version);
d9d99f
 
d9d99f
   grub_dprintf ("blscfg", "%s Creating %d entries from bls\n", __func__, nentries);
d9d99f
   for (r = nentries - 1; r >= 0; r--)