refactor(mp4): let DecodeStyp delegate to DecodeStypSR - #588
Merged
Merged
Conversation
DecodeStyp built the box itself instead of delegating the way DecodeFtyp does, so the short-payload guard added in #581 had to be written twice for styp and once for ftyp. Delegating removes the duplicate guard and brings styp to the same dual-path shape as ftyp. The two had already drifted: DecodeStypSR measured the payload as int(hdr.Size)-hdr.Hdrlen where ftyp used hdr.payloadLen(). They are equal today, but only one of them stays correct if a box ever carries an extended size. Unify on payloadLen. This also removes an observable inconsistency. For a styp that declares a short payload and is also truncated, the reader path reported the short payload while ftyp on the same input reported the truncation. Both now report the truncation, which is what actually went wrong. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
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.
Follow-up to #581, as mentioned in the review there.
#581 correctly fixed the panic. This removes the duplication that made it need
an extra guard.
Why styp needed two guards and ftyp one
DecodeFtypdelegates toDecodeFtypSR, so one guard in the SR decoder coversboth paths.
DecodeStypinstead built the box itself:so the guard had to be repeated. Delegating the way
ftypalready does drops itback to one guard per box type, 3 → 2.
The drift this fixes
The two decoders had already diverged in how they measure the payload:
Equal today, but only
payloadLen()stays correct for a box carrying anextended (large) size, where the header is 16 bytes rather than 8 — the same
reasoning applied to
esdsin #587. Unified onpayloadLen().An observable inconsistency, not just cosmetics
For a
stypthat declares a short payload and is truncated, the two boxtypes disagreed on
masterfor byte-identical input:ftypreads the body first and so reports the truncation;stypguarded beforereading and reported the declared length. After this change both report the
truncation, which is what actually went wrong.
That is the one behaviour change here: a
stypthat is both short-declared andtruncated now reports the truncation rather than the short payload. Both are
errors, neither panics, and the result now matches
ftyp.Tests
TestFtypStypDecodeConsistencyasserts thatftypandstypreport the samething for the same malformed input, across short-payload, truncated and
empty-payload cases. It fails on master:
TestFtypStypShortPayloadfrom #581 passes unchanged — the panic fix is intact.Scope
No API change, so no CHANGELOG entry: the exported types, signatures and the
errors for well-formed input are all untouched.
This is the cheap part of the
FStypBoxidea on the oldftyp-styp-combinedbranch — one decode body, fixes land once — without the breaking type merge.
The accessors (
MajorBrand,CompatibleBrands,Info, …) are still duplicated;they are pure reads over
dataand much less prone to drift than decoders thatcarry guards.
🤖 Generated with Claude Code