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