Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix arithmetic overflow panics #138

Merged
merged 2 commits into from
Jul 29, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions src/grapheme.rs
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ impl GraphemeCursor {
/// ```
pub fn provide_context(&mut self, chunk: &str, chunk_start: usize) {
use crate::tables::grapheme as gr;
assert!(chunk_start + chunk.len() == self.pre_context_offset.unwrap());
assert!(chunk_start.saturating_add(chunk.len()) == self.pre_context_offset.unwrap());
self.pre_context_offset = None;
if self.is_extended && chunk_start + chunk.len() == self.offset {
let ch = chunk.chars().next_back().unwrap();
Expand Down Expand Up @@ -598,15 +598,15 @@ impl GraphemeCursor {
if self.state == GraphemeState::NotBreak {
return Ok(false);
}
if (self.offset < chunk_start || self.offset >= chunk_start + chunk.len())
&& (self.offset > chunk_start + chunk.len() || self.cat_after.is_none())
if (self.offset < chunk_start || self.offset >= chunk_start.saturating_add(chunk.len()))
&& (self.offset > chunk_start.saturating_add(chunk.len()) || self.cat_after.is_none())
{
return Err(GraphemeIncomplete::InvalidOffset);
}
if let Some(pre_context_offset) = self.pre_context_offset {
return Err(GraphemeIncomplete::PreContext(pre_context_offset));
}
let offset_in_chunk = self.offset - chunk_start;
let offset_in_chunk = self.offset.saturating_sub(chunk_start);
if self.cat_after.is_none() {
let ch = chunk[offset_in_chunk..].chars().next().unwrap();
self.cat_after = Some(self.grapheme_category(ch));
Expand Down Expand Up @@ -702,7 +702,7 @@ impl GraphemeCursor {
self.cat_after = Some(self.grapheme_category(ch));
}
} else {
self.offset += ch.len_utf8();
self.offset = self.offset.saturating_add(ch.len_utf8());
self.state = GraphemeState::Unknown;
self.cat_before = self.cat_after.take();
if self.cat_before.is_none() {
Expand Down Expand Up @@ -781,7 +781,9 @@ impl GraphemeCursor {
if self.offset == chunk_start {
return Err(GraphemeIncomplete::PrevChunk);
}
let mut iter = chunk[..self.offset - chunk_start].chars().rev();
let mut iter = chunk[..self.offset.saturating_sub(chunk_start)]
.chars()
.rev();
let mut ch = iter.next().unwrap();
loop {
if self.offset == chunk_start {
Expand Down
Loading