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

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