From 5b632083ae3e4169545f80dfdec540277724d784 Mon Sep 17 00:00:00 2001 From: Conor Patrick Date: Thu, 4 Mar 2021 13:38:31 -0800 Subject: [PATCH] prevent `self.used` being greater than the buffer which can cause panics --- src/render.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/render.rs b/src/render.rs index 9b1a29e..66568aa 100644 --- a/src/render.rs +++ b/src/render.rs @@ -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; + } } } @@ -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 {