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

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