{"record":{"id":"fec6e3555b4c6136","repo":"TeamNewPipe/NewPipe","slug":"invalid-encoded-length","errorCode":null,"errorMessage":"Invalid encoded length","messagePattern":"Invalid encoded length","errorType":"exception","errorClass":"IOException","httpStatus":null,"severity":"error","filePath":"app/src/main/java/org/schabi/newpipe/streams/WebMReader.java","lineNumber":167,"sourceCode":"                    mask >>= size;\n\n                    long number = value & mask;\n\n                    for (int i = 1; i < size; i++) {\n                        value = stream.read();\n                        number <<= 8;\n                        number |= value;\n                    }\n\n                    return number;\n                }\n\n                mask >>= 1;\n                size++;\n            }\n        }\n\n        throw new IOException(\"Invalid encoded length\");\n    }\n\n    private Element readElement() throws IOException {\n        final Element elem = new Element();\n        elem.offset = stream.position();\n        elem.type = (int) readEncodedNumber();\n        elem.contentSize = readEncodedNumber();\n        elem.size = elem.contentSize + stream.position() - elem.offset;\n\n        return elem;\n    }\n\n    private Element readElement(final int expected) throws IOException {\n        final Element elem = readElement();\n        if (expected != 0 && elem.type != expected) {\n            throw new NoSuchElementException(\"expected \" + elementID(expected)\n                    + \" found \" + elementID(elem.type));\n        }","sourceCodeStart":149,"sourceCodeEnd":185,"githubUrl":"https://github.com/TeamNewPipe/NewPipe/blob/9e8be091560a69f35d44bd252eb00bc7911c977b/app/src/main/java/org/schabi/newpipe/streams/WebMReader.java#L149-L185","documentation":"WebMReader.readEncodedNumber reads the EBML variable-length integer by scanning the first byte for the leading 1-bit length marker (the position of the first set bit indicates how many bytes the number spans). If after 8 bytes no marker bit is found (all-zero bytes), the encoding is invalid and IOException 'Invalid encoded length' is thrown. Every EBML element ID and size uses this VINT encoding.","triggerScenarios":"Reading an EBML element ID or size where the leading byte(s) are all zero (no length marker). Happens when the stream is misaligned, contains zero-padding, or the data is not WebM/EBML at the current position.","commonSituations":"The parser is desynchronized after a prior corrupt element and now reads padding/garbage as a VINT; a file with unknown-size elements not handled; a stream that is not actually WebM but was passed as one; a truncated header full of zero bytes.","solutions":["Re-download or re-verify the file; the EBML structure is corrupt at the current offset.","Confirm the input is genuinely a WebM/Matroska file before parsing (check EBML magic 0x1A45DFA3).","If you control muxing, ensure no all-zero VINT bytes are emitted and element sizes are correctly encoded.","Catch IOException and report a corruption offset (stream.position()) for diagnosis."],"exampleFix":"// before — no validation, parser desyncs into zero bytes\nnew WebMReader(mysteryStream).parse(); // IOException: Invalid encoded length\n\n// after — verify EBML magic before parsing\nbyte[] magic = new byte[4];\nmysteryStream.read(magic);\nif ((magic[0]&0xFF)!=0x1A||(magic[1]&0xFF)!=0x45||(magic[2]&0xFF)!=0xDF||(magic[3]&0xFF)!=0xA3) {\n    throw new IOException(\"not a WebM/Matroska file\");\n}","handlingStrategy":"validation","validationCode":"// Verify EBML magic before parsing to avoid desyncing into zero bytes.\nbyte[] magic = new byte[4];\nsource.read(magic);\nif (!hasEbmlMagic(magic)) throw new IOException(\"not a WebM/Matroska file\");\n// Also ensure the file is not just zero-padding.","typeGuard":"public static boolean isValidVintLeadingByte(byte b) {\n    return (b & 0xFF) != 0; // a valid VINT lead byte has at least one set bit\n}","tryCatchPattern":"try {\n    reader.parse();\n} catch (IOException e) {\n    if (e.getMessage().contains(\"Invalid encoded length\")) {\n        // parser desynced into garbage/zeroes — re-download or re-mux\n        Log.w(TAG, \"EBML VINT corrupt at \" + e.getMessage());\n        redownloadOrRemux();\n    } else throw e;\n}","preventionTips":["Validate the EBML magic before parsing.","Re-download files where the parser desyncs.","Do not feed non-WebM data to WebMReader.","Confirm muxers emit valid VINT encodings (no all-zero lead bytes)."],"tags":["webm","ebml","corrupt-data","format-validation","java"],"backgroundTag":null,"analyzedSha":"9e8be091560a69f35d44bd252eb00bc7911c977b","analyzedAt":"2026-08-14T00:11:33.519Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}