{"record":{"id":"4ac598feb6b74396","repo":"Canop/broot","slug":"too-short","errorCode":null,"errorMessage":"too short","messagePattern":"too short","errorType":"error_code","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"src/content_search/mod.rs","lineNumber":91,"sourceCode":"\n/// Return the 1-indexed line number for the byte at position pos\npub fn line_count_at_pos<P: AsRef<Path>>(\n    path: P,\n    pos: usize,\n) -> io::Result<usize> {\n    let mut reader = BufReader::new(File::open(path)?);\n    let mut line = String::new();\n    let mut line_count = 1;\n    let mut bytes_count = 0;\n    while reader.read_line(&mut line)? > 0 {\n        bytes_count += line.len();\n        if bytes_count > pos {\n            return Ok(line_count);\n        }\n        line_count += 1;\n        line.clear();\n    }\n    Err(io::Error::new(\n        io::ErrorKind::UnexpectedEof,\n        \"too short\".to_string(),\n    ))\n}\n\n#[cfg(test)]\nmod line_count_at_pos_tests {\n    use {\n        super::line_count_at_pos,\n        std::io::Write,\n        tempfile::NamedTempFile,\n    };\n\n    /// Regression: a match whose first byte is exactly the first byte of a\n    /// line (here `T` of `TARGET`, the first byte of line 2) must report the\n    /// line it is actually on, not the line above. Before the fix,\n    /// `bytes_count >= pos` returned line 1 because `bytes_count` after\n    /// reading line 1 equals the byte index where line 2 begins.","sourceCodeStart":73,"sourceCodeEnd":109,"githubUrl":"https://github.com/Canop/broot/blob/17204794d7c564f21d023c31f105c09ac169dd2d/src/content_search/mod.rs#L73-L109","documentation":"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.","triggerScenarios":"Thrown at src/content_search/mod.rs:91 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Check that pos is within the file size before calling line_count_at_pos","Treat the returned UnexpectedEof error as 'position past EOF' and surface a user-facing message about the search position being invalid","If pos can legitimately exceed the file, fall back to the last line count instead of propagating the error"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"17204794d7c564f21d023c31f105c09ac169dd2d","analyzedAt":"2026-09-08T00:58:21.814Z","contentChangedAt":"2026-09-08T00:58:21.814Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}