Skip to content

Add more robust error handling to mkv parsing#393

Merged
mondain merged 4 commits intoRed5:mainfrom
kdkd:cherry-pick-2c921c3-20251126-123323
Nov 26, 2025
Merged

Add more robust error handling to mkv parsing#393
mondain merged 4 commits intoRed5:mainfrom
kdkd:cherry-pick-2c921c3-20251126-123323

Conversation

@kdkd
Copy link
Copy Markdown

@kdkd kdkd commented Nov 26, 2025

Commit from kdkd fork 2c921c3

matroska/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:

  • 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.

Commit from kdkd fork 0621be8

matroska/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 1883e31

matroska/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 ced1aca

matroska/ParserUtils.java:

If parseTag fails it can return null. Intentionally throw a meaningful
exception instead of an NPE.


Kevin Day 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 mondain merged commit bf713ae into Red5:main Nov 26, 2025
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.

2 participants