Skip to content

fix(router): stop trusting two bytes, and a cut that lands mid-character - #1

Merged
drsamonline merged 1 commit into
mainfrom
fix/router-signatures
Nov 10, 2026
Merged

drsamonline merged 1 commit into
mainfrom
fix/router-signatures

Conversation

@drsamonline

Copy link
Copy Markdown
Owner

What I read

crates/feather-core/src/format.rs (the router), natural.rs, the CLI's info/render, the workspace Cargo.toml, ROADMAP.md, docs/decisions.md, and .github/workflows/ci.yml. The router decides once, from bytes, with the extension as a tie-breaker for zip containers.

What was wrong

Two routes that send a file to a pipeline that cannot open it.

  1. BM is two bytes. Any file beginning with those two bytes went to the image pipeline. BMW service history.txt is a text file; the viewer would fail to open it.
  2. The text sniff cut at 512 bytes and required that slice to be valid UTF-8. When byte 512 landed inside a multi-byte character — routine in a document with any non-Latin text, and this viewer is aimed at Indian documents — valid text was judged binary. With no extension it fell through to Unknown and would not open at all.

What I changed

is_bmp() corroborates the BMP signature with the rest of the header: both reserved words zero, and a DIB header size from the set every BMP writer emits (12, 40, 52, 56, 64, 108, 124). A real BMP still routes to the image pipeline, unchanged.

The 512-byte cut now steps back to the last valid character boundary before asking whether the bytes are text. The classification logic after it is untouched.

Evidence

Three tests, added to the existing format.rs module — a_text_file_beginning_with_bm_is_not_a_bitmap, a_real_bmp_header_is_still_an_image, a_character_split_at_the_cut_does_not_make_text_binary. The first and third fail on main; the second is the guard that the fix did not break real bitmaps.

CI on this PR runs cargo test --workspace (71 tests before, 74 after) plus the release build and the M0 render smoke test on three platforms — the run is the evidence, not this text.

What I deliberately did not do

  • No shaping work. ROADMAP.md puts v0.2 (skrifa/harfrust/unicode-bidi) and v0.3 (PDF text extraction) behind "a compiler in the loop", and it is right to: that is rasterisation, fallback and glyph caching, not a slice to write blind. I have no Rust toolchain here — static.rust-lang.org is unreachable — so CI is my only compiler, and I will not spend your CI cycles guessing at a half-migrated text stack.
  • No dependency. clap was rejected for feather-cli (two subcommands, hand-rolled, and this repo gates its dependency budget); pdf.js, pdfjs-dist and PyMuPDF are other languages against hayro; ripgrep-all shells out to external binaries, which a single-binary viewer cannot do; mcat, crossterm and ratatui are terminal rendering, and this window is winit + softbuffer; pdfcpu and sumatrapdf are a Go tool and a C++ Windows app — the ideas worth having from them (structural validation, startup ordering) are features, not slices, and they are in the roadmap's own queue.
  • No version bump.

Two ways the router sent a file to the wrong pipeline.

`BM` is a two-byte signature, so `BMW service history.txt` was routed to the
image pipeline and failed to open. The BMP check now corroborates the header:
both reserved words zero and a DIB header size from the set every writer uses.
A real BMP is unaffected.

The text sniff cut at 512 bytes and required that slice to be valid UTF-8. When
byte 512 landed inside a multi-byte character, good text looked binary — with no
extension it fell through to `Unknown`, and the file would not open at all. The
cut now steps back to the last character boundary.

Three tests: the two false routes (both fail on the old code) and a real BMP
header that must keep routing to the image pipeline.
@drsamonline
drsamonline merged commit 009ab37 into main Nov 10, 2026
4 of 5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant