Skip to content

Commit

Permalink
prevent self.used being greater than the buffer which can cause panics
Browse files Browse the repository at this point in the history
  • Loading branch information
conorpp authored and nickray committed Mar 5, 2021
1 parent bb289e3 commit 5b63208
Showing 1 changed file with 5 additions and 3 deletions.
8 changes: 5 additions & 3 deletions src/render.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ impl<'a> WriteTo<'a> {
}

pub fn endl(&mut self) {
self.buffer[self.used] = b'\n';
self.used += 1;
if self.used < self.buffer.len() {
self.buffer[self.used] = b'\n';
self.used += 1;
}
}
}

Expand All @@ -56,7 +58,7 @@ impl<'a> core::fmt::Write for WriteTo<'a> {
let raw_s = s.as_bytes();
let write_num = cmp::min(raw_s.len(), remaining_buf.len());
remaining_buf[..write_num].copy_from_slice(&raw_s[..write_num]);
self.used += raw_s.len();
self.used += write_num;
if write_num < raw_s.len() {
Err(fmt::Error)
} else {
Expand Down

0 comments on commit 5b63208

Please sign in to comment.