fix(pure-magic): apply binary/text test gate at rule entry only#45
Draft
tnaroska wants to merge 1 commit into
Draft
fix(pure-magic): apply binary/text test gate at rule entry only#45tnaroska wants to merge 1 commit into
tnaroska wants to merge 1 commit into
Conversation
The skip for is_only_binary/is_only_text in Match::matches ran on every recursion, so child tests of an already-matched parent were dropped on text streams. This silently stripped any rule whose message lived on a scalar/float/pstring/string16/indirect child — most visibly the RTF rule (magic-db/src/magdir/rtf), which carries its message on a level-2 ubyte qualifier and was reported as plain "ASCII text". Hoist the gate into EntryNode::matches and run it only when self.root, mirroring upstream libmagic's softmagic.c::match() semantics: the binary/text decision is taken once at the outer match loop, not re-applied to children. Top-level perf filtering for the >300 rule files is preserved. Add two regression tests covering (a) the RTF-shaped rule emitting its message on a text stream and (b) the upstream DROID-skip qualifier still rejecting fmt-355-signature-id-522.rtf. Co-Authored-By: Claude Opus 4.7 <noreply@anthropic.com>
4 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fix a binary/text gating bug that silently stripped any rule whose message lives on a scalar/float/pstring/string16/indirect child — most visibly
magdir/rtf, which carries its message on a level-2ubytequalifier and was reported as plainASCII textinstead ofRich Text Format data.Problem
Match::matches(pure-magic/src/lib.rs) skipped tests withis_only_binary/is_only_textmismatching the stream kind on every recursion. Once the top-levelstring {\rtfmatched on a text-classified stream, its>5 ubyteand>>5 ubytechildren were dropped becauseubyteis binary-only — so the rule's message (carried on the level-2 child) never fired.Fix
Hoist the gate into
EntryNode::matchesand run it only whenself.root. This mirrors upstream libmagicmatch()insrc/softmagic.c:243-262, where theSTRING_BINTEST/STRING_TEXTTESTcheck fires inside the top-levelfor (magindex = 0; ...)loop andgoto flushskips sub-tests — i.e. the binary/text decision is taken once at the outer match loop, not re-applied to children. Top-level perf filtering across the >300 rule files is preserved.Tests
Two regression tests in
pure-magic/src/lib.rs:test_string_parent_with_scalar_children_on_text_stream— RTF-shaped rule emits its message on a text stream (the bug).test_rtf_droid_skip_still_rejects— the upstream level-1>5 ubyte !0xABqualifier (added specifically to skip DROIDfmt-355-signature-id-522.rtf) still rejects that input now that children run on text streams.Test plan
cargo test -p pure-magic— passes locally (139 tests).🤖 Generated with Claude Code