Skip to content

Commit

Permalink
Add second check if read fails unexpectedly
Browse files Browse the repository at this point in the history
  • Loading branch information
sharifhsn authored and sharkdp committed Nov 13, 2022
1 parent f6a1c4e commit 5f82942
Showing 1 changed file with 14 additions and 11 deletions.
25 changes: 14 additions & 11 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,9 +489,12 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
if let Ok(n) = buf.read(&mut self.line_buf) {
if n > 0 && n < 8 * self.panels as usize {
is_empty = false;
self.line_buf.resize(n, 0);
leftover = Some(n);
break;
// perform second check on read
if let Ok(0) = buf.read(&mut self.line_buf[n..]) {
self.line_buf.resize(n, 0);
leftover = Some(n);
break;
};
} else if n == 0 {
if self.squeezer == Squeezer::Delete {
self.line_buf.clear();
Expand All @@ -503,16 +506,16 @@ impl<'a, Writer: Write> Printer<'a, Writer> {
is_empty = false;

if self.squeezer == Squeezer::Delete && self.squeeze_byte == 0 {
for w in self.line_buf.chunks_exact(std::mem::size_of::<usize>()) {
if usize::from_ne_bytes(w.try_into().unwrap()) != 0 {
self.squeezer = Squeezer::Ignore;
all_zero = false;
break;
}
}
if all_zero {
if self
.line_buf
.chunks_exact(std::mem::size_of::<usize>())
.all(|w| usize::from_ne_bytes(w.try_into().unwrap()) == 0)
{
self.idx += 8 * self.panels;
continue;
} else {
self.squeezer = Squeezer::Ignore;
all_zero = false;
}
}

Expand Down

0 comments on commit 5f82942

Please sign in to comment.