Skip to content

Commit

Permalink
Provide better diagnostics on libpng unexpectedeof
Browse files Browse the repository at this point in the history
  • Loading branch information
lilith committed Sep 5, 2024
1 parent c8ee27d commit 90713c2
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
24 changes: 21 additions & 3 deletions imageflow_core/src/codecs/libpng_decoder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -157,8 +157,26 @@ impl PngDec{
true
},
Err(err) => {
decoder.error = Some(FlowError::from_decoder(err).at(here!()));
false
if err.kind() == ::std::io::ErrorKind::UnexpectedEof {
let len = decoder.io.try_get_length();
let pos = decoder.io.try_get_position();
let remaining = if len.is_some() && pos.is_some() {
Some(len.unwrap() - pos.unwrap())
} else {
None
};
let missing = remaining.map(|r| (bytes_requested as u64) - r);
let err =
FlowError::without_location(ErrorKind::DecodingIoError,
format!("{:?} (failed to read requested {} bytes (only {:?} remain), pos={:?}, len={:?}, missing={:?})", err, bytes_requested, remaining, pos, len, missing)).at(here!());

decoder.error = Some(err);

false
} else {
decoder.error = Some(FlowError::from_decoder(err).at(here!()));
false
}
}
}

Expand Down Expand Up @@ -307,4 +325,4 @@ impl PngDec{
}
}
}
}
}
17 changes: 17 additions & 0 deletions imageflow_core/src/io.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,23 @@ impl IoProxy {
pub fn io_id(&self) -> i32{
self.io_id
}
pub fn try_get_length(&mut self) -> Option<u64>{
match &self.backend{
IoBackend::ReadVec(v) => Some(v.get_ref().len() as u64),
IoBackend::ReadSlice(v) => Some(v.get_ref().len() as u64),
IoBackend::ReadFile(v) => v.get_ref().metadata().map(|m| m.len()).ok(),
_ => None
}
}

pub fn try_get_position(&mut self) -> Option<u64>{
match &self.backend{
IoBackend::ReadVec(v) => Some(v.position()),
IoBackend::ReadSlice(v) => Some(v.position()),
IoBackend::ReadFile(v) => v.get_ref().stream_position().ok(),
_ => None
}
}

pub fn read_exact(&mut self, buf: &mut [u8]) -> std::io::Result<()>{
self.backend.get_read().expect("cannot read from writer").read_exact(buf)
Expand Down

0 comments on commit 90713c2

Please sign in to comment.