Add more robust error handling to mkv parsing#393
Merged
Conversation
added 4 commits
November 26, 2025 12:33
VINTs that were between 5 and 8 bytes were not being parsed correctly,
causing the parser to get out of sync with the stream.
To reproduce the original bug:
- Take any Matroska element whose size field is encoded with a 7‑byte VINT (legal per EBML). Example: a Cluster element (ID =
0x1F43B675) with a size of 0x20 bytes encoded as:
- ID bytes: 1F 43 B6 75
- Size bytes (7‑byte VINT): 02 00 00 00 00 00 20
- The leading byte 0x02 is 00000010b, which in EBML means “length = 7 bytes, value bits = 0x000000000020”.
- What the code does:
- readVINT reads the first size byte fb = 0x02.
- It sets len = fb >> 4, which is 0x0.
- Because len is 0, it falls into the final else branch (“8‑byte VINT”), sets mask = MASK_BYTE_8, allocates an 8‑byte array,
and reads 7 more bytes for this size.
- The seven bytes it reads are the remaining 6 bytes of the size (00 00 00 00 00 20) plus the first byte of the element
payload (because there were only 6 size bytes left). So the parser consumes one payload byte as if it were part of the
size field.
- Consequences:
- The computed size is wrong (it’s based on 8 bytes instead of 7, and includes one payload byte).
- The stream position is now off by one byte. The next call to parseTag starts in the middle of the element payload, so
subsequent IDs/sizes are garbage. This typically leads to decoding failures, incorrect sizes, and eventually EOF/NPE/assert
failures.
skip() calls InputStream.skip(), which is allowed to return 0, for example if reading from a named pipe or fifo file that's blocked or trying to read past the end of the file. If skip isn't working, try reading one byte to either unstick it or at least advance the file pointer so it doesn't end up with an infinite loop. An alternative would be to just give up if input.skip() returns 0 because I'm not sure recovery is possible.
If inputStream.read has an error, it returns -1. Treat that like an error instead of rewinding the read pointer by 1.
If parseTag fails it can return null. Intentionally throw a meaningful exception instead of an NPE.
mondain
approved these changes
Nov 26, 2025
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.
Commit from kdkd fork
2c921c3matroska/ParserUtils.java:
VINTs that were between 5 and 8 bytes were not being parsed correctly,
causing the parser to get out of sync with the stream.
To reproduce the original bug:
0x1F43B675) with a size of 0x20 bytes encoded as:
and reads 7 more bytes for this size.
payload (because there were only 6 size bytes left). So the parser consumes one payload byte as if it were part of the
size field.
subsequent IDs/sizes are garbage. This typically leads to decoding failures, incorrect sizes, and eventually EOF/NPE/assert
failures.
Commit from kdkd fork
0621be8matroska/ParserUtils.java:
skip() calls InputStream.skip(), which is allowed to return 0, for example
if reading from a named pipe or fifo file that's blocked or trying to
read past the end of the file. If skip isn't working, try reading one
byte to either unstick it or at least advance the file pointer so it
doesn't end up with an infinite loop.
An alternative would be to just give up if input.skip() returns 0 because
I'm not sure recovery is possible.
Commit from kdkd fork
1883e31matroska/ParserUtils.java:
If inputStream.read has an error, it returns -1. Treat that like an error
instead of rewinding the read pointer by 1.
Commit from kdkd fork
ced1acamatroska/ParserUtils.java:
If parseTag fails it can return null. Intentionally throw a meaningful
exception instead of an NPE.