nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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