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

Avoid infinite loop when retrying after earlier fatal error. #520

Merged
merged 1 commit into from
Oct 6, 2024

Conversation

anforowicz
Copy link
Contributor

@anforowicz anforowicz commented Oct 4, 2024

When StreamingDecoder reports an error, it leaves state set to None. Before this commit, calling next_frame in this state would have led to an infinite loop:

  • ReadDecoder::decode_next would loop forever (!self.at_eof is true after an error) and would fail to make progress, because
  • When StreamingDecoder::update saw state set to None then before this commit it wouldn't enter the next_state loop and would immediately return no progress (Ok((/* consumed bytes = */ 0, Decoded::Nothing))).

After this commit, StreamingDecoder::update checks if the state is None and treats this as an error.

Code links:

  • ReadDecoder::decode_next:
    fn decode_next(&mut self, image_data: &mut Vec<u8>) -> Result<Option<Decoded>, DecodingError> {
    while !self.at_eof {
    let (consumed, result) = {
    let buf = self.reader.fill_buf()?;
    if buf.is_empty() {
    return Err(DecodingError::IoError(ErrorKind::UnexpectedEof.into()));
    }
    self.decoder.update(buf, image_data)?
    };
    self.reader.consume(consumed);
    match result {
    Decoded::Nothing => (),
    Decoded::ImageEnd => self.at_eof = true,
    result => return Ok(Some(result)),
    }
    }
    Ok(None)
    }
  • StreamingDecoder::update:
    pub fn update(
    &mut self,
    mut buf: &[u8],
    image_data: &mut Vec<u8>,
    ) -> Result<(usize, Decoded), DecodingError> {
    let len = buf.len();
    while !buf.is_empty() && self.state.is_some() {
    match self.next_state(buf, image_data) {
    Ok((bytes, Decoded::Nothing)) => buf = &buf[bytes..],
    Ok((bytes, result)) => {
    buf = &buf[bytes..];
    return Ok((len - buf.len(), result));
    }
    Err(err) => return Err(err),
    }
    }
    Ok((len - buf.len(), Decoded::Nothing))
    }

When `StreamingDecoder` reports an error, it leaves `state` set to
`None`.  Before this commit, calling `next_frame` in this state would
have led to an infinite loop:

* `ReadDecoder::decode_next` would loop forever (`!self.at_eof` is true
  after an error) and would fail to make progress, because
* When `StreamingDecoder::update` sees `state` saw set to `None` then
  before this commit it wouldn't enter the `next_state` loop and would
  immediately return no progress
  (`Ok((/* consumer bytes = */ 0, Decoded::Nothing))`).

After this commit, `StreamingDecoder::update` checks if the `state` is
`None` and treats this as an error.
@kornelski kornelski merged commit 1ec7613 into image-rs:master Oct 6, 2024
19 checks passed
@anforowicz anforowicz deleted the infinite-loop-after-error branch October 6, 2024 22:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants