diff --git a/src/locate/mod.rs b/src/locate/mod.rs index 3eb399c..9041027 100644 --- a/src/locate/mod.rs +++ b/src/locate/mod.rs @@ -490,8 +490,10 @@ impl DbReader { return None; }; - // drop nul byte when matching - match String::from_utf8_lossy(&buf[..buf.len() - 1]).as_ref() { + // drop the trailing nul byte when matching. `read_until` may stop at EOF + // without one (e.g. a too-short file), so strip it only if present rather + // than slicing `buf.len() - 1`, which underflows when `buf` is empty. + match String::from_utf8_lossy(buf.strip_suffix(b"\0").unwrap_or(&buf)).as_ref() { "LOCATE02" => Some(DbFormat::Locate02), _ => None, } diff --git a/tests/db_tests.rs b/tests/db_tests.rs index 89e949d..609071e 100644 --- a/tests/db_tests.rs +++ b/tests/db_tests.rs @@ -253,3 +253,16 @@ fn test_updatedb_locate_roundtrip() { "locate output did not contain the indexed file: {stdout:?}" ); } + +#[test] +fn test_locate_one_byte_db() { + let dir = tempfile::tempdir().unwrap(); + let db = dir.path().join("db_1byte"); + std::fs::write(&db, b"X").unwrap(); + Command::cargo_bin("locate") + .expect("couldn't find locate binary") + .arg(format!("--database={}", db.display())) + .arg("foo") + .assert() + .code(1); +}