nalika / rpms / grub2

Forked from rpms/grub2 2 years ago
Clone

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

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