nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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