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

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