proofs.rs

Clarify retry behavior after a buffer fill error

Clarify retry behavior after a buffer fill error

Found in Report #40 v1 · Updated 2026-11-27 11:58 UTC

Description

BufReadDecoder::next_strict can skip an unread byte on retry when the underlying BufRead returns a transient error on its second fill_buf call. Is retry after an I/O error intended to preserve unread input?

Steps to Reproduce

With utf-8 = "=0.7.6", run the following. Successful execution reproduces the discrepancy:

extern crate utf8;
use std::io::{self, BufRead, ErrorKind, Read};

/// A position-preserving read of a single-byte source, interrupted on its second read.
struct Source {
    reads: usize,
}
impl Read for Source {
    fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
        self.reads += 1;
        if self.reads == 2 { return Err(ErrorKind::Interrupted.into()); }
        if out.is_empty() { return Ok(0); }
        out[0] = b'x';
        Ok(1)
    }
}
struct Window {
    source: Source,
    preview: [u8; 1],
    consumed: bool,
}
impl BufRead for Window {
    fn fill_buf(&mut self) -> io::Result<&[u8]> {
        if self.consumed { return Ok(&[]); }
        // Refresh a view of the same byte without advancing the logical cursor.
        let count = self.source.read(&mut self.preview)?;
        Ok(&self.preview[..count])
    }
    fn consume(&mut self, count: usize) {
        assert!(count <= usize::from(!self.consumed));
        if count != 0 { self.consumed = true; }
    }
}
impl Read for Window {
    fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
        if out.is_empty() { return Ok(0); }
        let count = self.fill_buf()?.read(out)?;
        self.consume(count);
        Ok(count)
    }
}
fn window() -> Window {
    Window { source: Source { reads: 0 }, preview: [0], consumed: false }
}
fn main() {
    let mut direct = window();
    assert_eq!(direct.fill_buf().unwrap(), b"x");
    assert_eq!(direct.fill_buf().unwrap_err().kind(), ErrorKind::Interrupted);
    assert_eq!(direct.fill_buf().unwrap(), b"x");
    let mut decoder = utf8::BufReadDecoder::new(window());
    match decoder.next_strict() {
        Some(Err(utf8::BufReadDecoderError::Io(e))) => assert_eq!(e.kind(), ErrorKind::Interrupted),
        other => panic!("unexpected {:?}", other)
    }
    // Success reproduces the missing byte on retry.
    assert!(decoder.next_strict().is_none());
}
Expected Behavior

Please clarify whether retrying should return the byte that the reader has not consumed.

Actual Behavior

The first decoder call returns Interrupted; retry returns None. Direct calls to the same reader return x, Interrupted, then x before a consume.

Environment

utf-8 0.7.6, rustc 1.91.0, default unoptimized build.

Screenshots/Logs

The assertions in the example pass.

Additional Context

This uses a custom reader that preserves its logical position across the error. The example does not establish the behavior for std::io::BufReader.

Change history
  1. v1 · created · 2026-11-27 11:58 UTC
    {
      "title": "Clarify retry behavior after a buffer fill error",
      "body": "## Description\n\n`BufReadDecoder::next_strict` can skip an unread byte on retry when the underlying `BufRead` returns a transient error on its second `fill_buf` call. Is retry after an I/O error intended to preserve unread input?\n\n## Steps to Reproduce\n\nWith `utf-8 = \"=0.7.6\"`, run the following. Successful execution reproduces the discrepancy:\n\n```rust\nextern crate utf8;\nuse std::io::{self, BufRead, ErrorKind, Read};\n\n/// A position-preserving read of a single-byte source, interrupted on its second read.\nstruct Source {\n    reads: usize,\n}\nimpl Read for Source {\n    fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {\n        self.reads += 1;\n        if self.reads == 2 { return Err(ErrorKind::Interrupted.into()); }\n        if out.is_empty() { return Ok(0); }\n        out[0] = b'x';\n        Ok(1)\n    }\n}\nstruct Window {\n    source: Source,\n    preview: [u8; 1],\n    consumed: bool,\n}\nimpl BufRead for Window {\n    fn fill_buf(&mut self) -> io::Result<&[u8]> {\n        if self.consumed { return Ok(&[]); }\n        // Refresh a view of the same byte without advancing the logical cursor.\n        let count = self.source.read(&mut self.preview)?;\n        Ok(&self.preview[..count])\n    }\n    fn consume(&mut self, count: usize) {\n        assert!(count <= usize::from(!self.consumed));\n        if count != 0 { self.consumed = true; }\n    }\n}\nimpl Read for Window {\n    fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {\n        if out.is_empty() { return Ok(0); }\n        let count = self.fill_buf()?.read(out)?;\n        self.consume(count);\n        Ok(count)\n    }\n}\nfn window() -> Window {\n    Window { source: Source { reads: 0 }, preview: [0], consumed: false }\n}\nfn main() {\n    let mut direct = window();\n    assert_eq!(direct.fill_buf().unwrap(), b\"x\");\n    assert_eq!(direct.fill_buf().unwrap_err().kind(), ErrorKind::Interrupted);\n    assert_eq!(direct.fill_buf().unwrap(), b\"x\");\n    let mut decoder = utf8::BufReadDecoder::new(window());\n    match decoder.next_strict() {\n        Some(Err(utf8::BufReadDecoderError::Io(e))) => assert_eq!(e.kind(), ErrorKind::Interrupted),\n        other => panic!(\"unexpected {:?}\", other)\n    }\n    // Success reproduces the missing byte on retry.\n    assert!(decoder.next_strict().is_none());\n}\n```\n\n## Expected Behavior\n\nPlease clarify whether retrying should return the byte that the reader has not consumed.\n\n## Actual Behavior\n\nThe first decoder call returns `Interrupted`; retry returns `None`. Direct calls to the same reader return `x`, `Interrupted`, then `x` before a consume.\n\n## Environment\n\n`utf-8` 0.7.6, rustc 1.91.0, default unoptimized build.\n\n## Screenshots/Logs\n\nThe assertions in the example pass.\n\n## Additional Context\n\nThis uses a custom reader that preserves its logical position across the error. The example does not establish the behavior for `std::io::BufReader`.",
      "validity": "reported",
      "upstream_status": "not_reported"
    }

Read and join the discussion on the report →