Canop/broot · error · io::Error

too short

Error message

too short

What it means

Sentinel io::Error (kind UnexpectedEof) thrown by line_count_at_pos when the file at `path` ends before byte offset `pos` is reached, so no line can contain that position. It is a guard against positions past end-of-file, not an actual I/O failure.

Solutions

  1. Check that pos is within the file size before calling line_count_at_pos
  2. Treat the returned UnexpectedEof error as 'position past EOF' and surface a user-facing message about the search position being invalid
  3. If pos can legitimately exceed the file, fall back to the last line count instead of propagating the error
Defensive patterns

Strategy: try-catch

When it happens

Trigger: Thrown at src/content_search/mod.rs:91 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of Canop/broot@17204794d7 (2026-09-08). Data as JSON: /api/errors/4ac598feb6b74396. Report an issue: GitHub.

Appendix: source

Thrown at src/content_search/mod.rs:91

/// Return the 1-indexed line number for the byte at position pos
pub fn line_count_at_pos<P: AsRef<Path>>(
    path: P,
    pos: usize,
) -> io::Result<usize> {
    let mut reader = BufReader::new(File::open(path)?);
    let mut line = String::new();
    let mut line_count = 1;
    let mut bytes_count = 0;
    while reader.read_line(&mut line)? > 0 {
        bytes_count += line.len();
        if bytes_count > pos {
            return Ok(line_count);
        }
        line_count += 1;
        line.clear();
    }
    Err(io::Error::new(
        io::ErrorKind::UnexpectedEof,
        "too short".to_string(),
    ))
}

#[cfg(test)]
mod line_count_at_pos_tests {
    use {
        super::line_count_at_pos,
        std::io::Write,
        tempfile::NamedTempFile,
    };

    /// Regression: a match whose first byte is exactly the first byte of a
    /// line (here `T` of `TARGET`, the first byte of line 2) must report the
    /// line it is actually on, not the line above. Before the fix,
    /// `bytes_count >= pos` returned line 1 because `bytes_count` after
    /// reading line 1 equals the byte index where line 2 begins.

View on GitHub (pinned to 17204794d7)