Blame SOURCES/0568-font-Fix-integer-underflow-in-binary-search-of-char-.patch

530103
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
530103
From: Zhang Boyang <zhangboyang.id@gmail.com>
530103
Date: Sun, 14 Aug 2022 18:09:38 +0800
530103
Subject: [PATCH] font: Fix integer underflow in binary search of char index
530103
530103
If search target is less than all entries in font->index then "hi"
530103
variable is set to -1, which translates to SIZE_MAX and leads to errors.
530103
530103
This patch fixes the problem by replacing the entire binary search code
530103
with the libstdc++'s std::lower_bound() implementation.
530103
530103
Signed-off-by: Zhang Boyang <zhangboyang.id@gmail.com>
530103
Reviewed-by: Daniel Kiper <daniel.kiper@oracle.com>
530103
(cherry picked from commit c140a086838e7c9af87842036f891b8393a8c4bc)
530103
(cherry picked from commit e110997335b1744464ea232d57a7d86e16ca8dee)
530103
(cherry picked from commit 403053a5116ae945f9515a82c37ff8cfb927362c)
530103
---
530103
 grub-core/font/font.c | 40 ++++++++++++++++++++++------------------
530103
 1 file changed, 22 insertions(+), 18 deletions(-)
530103
530103
diff --git a/grub-core/font/font.c b/grub-core/font/font.c
530103
index 059c23dff7..31786ab339 100644
530103
--- a/grub-core/font/font.c
530103
+++ b/grub-core/font/font.c
530103
@@ -688,12 +688,12 @@ read_be_int16 (grub_file_t file, grub_int16_t * value)
530103
 static inline struct char_index_entry *
530103
 find_glyph (const grub_font_t font, grub_uint32_t code)
530103
 {
530103
-  struct char_index_entry *table;
530103
-  grub_size_t lo;
530103
-  grub_size_t hi;
530103
-  grub_size_t mid;
530103
+  struct char_index_entry *table, *first, *end;
530103
+  grub_size_t len;
530103
 
530103
   table = font->char_index;
530103
+  if (table == NULL)
530103
+    return NULL;
530103
 
530103
   /* Use BMP index if possible.  */
530103
   if (code < 0x10000 && font->bmp_idx)
530103
@@ -706,25 +706,29 @@ find_glyph (const grub_font_t font, grub_uint32_t code)
530103
        */
530103
     }
530103
 
530103
-  /* Do a binary search in `char_index', which is ordered by code point.  */
530103
-  lo = 0;
530103
-  hi = font->num_chars - 1;
530103
+  /*
530103
+   * Do a binary search in char_index which is ordered by code point.
530103
+   * The code below is the same as libstdc++'s std::lower_bound().
530103
+   */
530103
+  first = table;
530103
+  len = font->num_chars;
530103
+  end = first + len;
530103
 
530103
-  if (!table)
530103
-    return 0;
530103
-
530103
-  while (lo <= hi)
530103
+  while (len > 0)
530103
     {
530103
-      mid = lo + (hi - lo) / 2;
530103
-      if (code < table[mid].code)
530103
-	hi = mid - 1;
530103
-      else if (code > table[mid].code)
530103
-	lo = mid + 1;
530103
+      grub_size_t half = len >> 1;
530103
+      struct char_index_entry *middle = first + half;
530103
+
530103
+      if (middle->code < code)
530103
+	{
530103
+	  first = middle + 1;
530103
+	  len = len - half - 1;
530103
+	}
530103
       else
530103
-	return &table[mid];
530103
+	len = half;
530103
     }
530103
 
530103
-  return 0;
530103
+  return (first < end && first->code == code) ? first : NULL;
530103
 }
530103
 
530103
 /* Get a glyph for the Unicode character CODE in FONT.  The glyph is loaded