Repository navigation
fix(search): decode recipe files lossily instead of failing on invalid UTF-8 - #513
Conversation
…d UTF-8
`cook search tuna` died with
Error: Failed to read '/data/recipes'
Caused by:
stream did not contain valid UTF-8
over a single recipe that was not valid UTF-8 — a file saved from a
Latin-1 editor, a stray byte from a bad paste — and took every other
recipe's results down with it. Only some queries failed, which is what
made it puzzling: the file had to score before anything read it.
`matches_every_term` read candidates with `read_to_string`, so one bad
byte anywhere in a matched recipe's body failed the whole search. Read
the bytes and decode them with `String::from_utf8_lossy` instead: a
mis-encoded accent now costs one wrong character in the haystack rather
than the recipe, and only the bad bytes themselves stop matching.
A file that cannot be read at all is still an error, and pinned by a
test. Lossy decoding is for bytes that are there and malformed, not for
bytes that never arrived; treating an unreadable recipe as an empty one
would turn a fixable problem into results that are quietly wrong.
Refs #498. The other half of that report — a bad byte in the *front
matter* — fails inside `cooklang-find`, which propagates it out of
`search` instead of skipping the file the way `build_tree` does. Fixed
in cooklang/cooklang-find#13 and picked up by a 0.7.1 bump.
Review: fix(search): decode recipe files lossily instead of failing on invalid UTF-8Nicely scoped fix — one function changed, the change is exactly as wide as the bug, and the accompanying doc comments/tests make the "why" traceable back to #498. A few notes: Correctness
Performance
Test coverage
Security
Minor/style
Overall: solid, well-tested, appropriately narrow fix. No blocking issues from me. |
…atter Completes the fix for #498. The previous commit handled a bad byte in a recipe's body, which this crate read; a bad byte in the *front matter* failed inside `cooklang-find`, which propagated it out of its own walk instead of skipping the file the way `build_tree` does. That is the arm that named the search root rather than the file, and it is why the report read "Failed to read '/data/recipes'". 0.7.1 (cooklang/cooklang-find#13) decodes those files lossily too, and scores their contents rather than silently giving them zero — so such a recipe is findable by an ingredient in its body, not only by its file name. Lockfile only: the `0.7.0` requirement is a caret range that already admitted it. The regression test now covers both halves, and asserts a content-only term matches, so it cannot pass on a file-name match alone. `cooklang-reports` still pulls its own `cooklang-find` 0.6.1, which is untouched by this. Fixes #498
|
Review Nicely scoped fix, I read through crates/core/src/search.rs in full (not just the diff) plus the Cargo.lock/Cargo.toml change and CoreError::Io. Summary: no blocking issues found; this is ready to merge as far as the code itself goes. Code quality and correctness:
Test coverage: One small gap: there's no test for a file that's entirely invalid bytes (e.g. a stray binary file with a .cook extension) rather than mostly-valid-with-one-bad-byte, though given the implementation is a straight from_utf8_lossy, I'd expect it to behave the same -- a minor completeness nit, not a real risk. Minor nitpick (non-blocking): Security: Scope: |
…d file Review notes on #513. `search` is public and linked `[matches_every_term]`, which is not, so rustdoc warned and rendered it as plain text anyway — plain backticks say the same thing without the broken link. The three sibling warnings in this crate are left alone; they are not this PR's. And a test for the degenerate case: a file with no valid text in it at all, rather than one bad byte among good ones.
ReviewSolid, well-scoped fix. The distinction it draws — malformed-but-present bytes get lossy-decoded, genuinely unreadable files still error — is exactly the right one, and the PR description explains the two-crate root cause clearly. Code quality
Bugs / correctnessNone found. Test coverageThorough — the four new tests cover: a bad byte in the body, a bad byte in the front matter (exercising the SecurityNone. This only changes how bytes already read from local recipe files are decoded; no new I/O surface or injection vector. Scope (not a blocker)The PR description already flags Nice work overall — the fix, docs, and regression tests all match the standard this file already sets. |
Fixes #498.
What was happening
One recipe in the collection was not valid UTF-8 — a file saved from a Latin-1 editor, a stray byte from a bad paste — and it failed the whole search, taking every other recipe's results with it.
Only some queries failed, which is what made it puzzling: content scoring of the bad file silently fails, so it only entered the result set when the filename matched —
cook search tunaagainstTuna Mornay.cook. Searching for an ingredient in no recipe returned nothing, as reported.Two independent readers were rejecting the file, in two crates.
This repo: a bad byte in the body
matches_every_termread every candidate withread_to_stringfor the AND filter. Now it reads the bytes and decodes them withString::from_utf8_lossy: a mis-encoded accent costs one wrong character in the haystack rather than the recipe, and only the bad bytes themselves stop matching — a term either side of one still does.A file that cannot be read at all is still an error, pinned by
an_unreadable_file_is_still_an_io_error. Lossy decoding is for bytes that are there and malformed, not for bytes that never arrived; treating an unreadable recipe as an empty one would turn a fixable problem into results that are quietly wrong.cooklang-find 0.7.1: a bad byte in the front matter
RecipeEntry::from_pathreads front matter throughBufRead::lines, andsearchpropagated theInvalidDatarather than skipping the file the waybuild_treein the same crate already does. That is the arm that reports the search root instead of the file, which is why the error named/data/recipes.Fixed in cooklang/cooklang-find#13, released as 0.7.1. It also makes the content scorer lossy, so such a recipe becomes findable by an ingredient in its body rather than only by its file name.
Lockfile-only change here — the
"0.7.0"requirement is a caret range that already admitted 0.7.1.Verification
Against a real ISO-8859 recipe:
cook doctor validatestill reports the file as an error, which is the right division of labour: search finds the recipe, doctor tells you the encoding is wrong.Both regression tests fail without their fix and pass with it. The search test asserts a content-only term matches, so it cannot pass on a file-name match alone.
cargo fmt,cargo clippy --all-targets -- -D warningsandcargo testare clean (22 test binaries, 0 failures).Not covered
cooklang-reportspulls its owncooklang-find0.6.1, which still has the upstream bug, socook reportcould hit it on the same file. Out of scope here — it needs acooklang-reportsrelease.