From 839ef0702efec60a7a5c624740998a137d6156ef Mon Sep 17 00:00:00 2001 From: Yevhenii Reizner Date: Tue, 1 Oct 2024 08:27:59 +0300 Subject: [PATCH] Check `Buffer::ensure` result. Closes #135 --- src/hb/buffer.rs | 19 +++++++++++-------- src/hb/ot_shaper_arabic.rs | 6 ++++-- 2 files changed, 15 insertions(+), 10 deletions(-) diff --git a/src/hb/buffer.rs b/src/hb/buffer.rs index f1853242..2da6e5d6 100644 --- a/src/hb/buffer.rs +++ b/src/hb/buffer.rs @@ -537,7 +537,9 @@ impl hb_buffer_t { } fn add(&mut self, codepoint: u32, cluster: u32) { - self.ensure(self.len + 1); + if !self.ensure(self.len + 1) { + return; + } let i = self.len; self.info[i] = hb_glyph_info_t { @@ -1193,6 +1195,7 @@ impl hb_buffer_t { true } + #[must_use] pub fn ensure(&mut self, size: usize) -> bool { if size < self.len { return true; @@ -1208,11 +1211,7 @@ impl hb_buffer_t { true } - pub fn set_len(&mut self, len: usize) { - self.ensure(len); - self.len = len; - } - + #[must_use] fn make_room_for(&mut self, num_in: usize, num_out: usize) -> bool { if !self.ensure(self.out_len + num_out) { return false; @@ -1232,7 +1231,9 @@ impl hb_buffer_t { fn shift_forward(&mut self, count: usize) { assert!(self.have_output); - self.ensure(self.len + count); + if !self.ensure(self.len + count) { + return; + } for i in (0..(self.len - self.idx)).rev() { self.info[self.idx + count + i] = self.info[self.idx + i]; @@ -1415,7 +1416,9 @@ impl hb_buffer_t { } fn push_str(&mut self, text: &str) { - self.ensure(self.len + text.chars().count()); + if !self.ensure(self.len + text.chars().count()) { + return; + } for (i, c) in text.char_indices() { self.add(c as u32, i as u32); diff --git a/src/hb/ot_shaper_arabic.rs b/src/hb/ot_shaper_arabic.rs index 4a6d79b9..4477b6bd 100644 --- a/src/hb/ot_shaper_arabic.rs +++ b/src/hb/ot_shaper_arabic.rs @@ -584,10 +584,12 @@ fn apply_stch(face: &hb_font_t, buffer: &mut hb_buffer_t) { } if step == MEASURE { - buffer.ensure(buffer.len + extra_glyphs_needed); + if !buffer.ensure(buffer.len + extra_glyphs_needed) { + break; + } } else { debug_assert_eq!(j, 0); - buffer.set_len(new_len); + buffer.len = new_len; } }