{"record":{"id":"d0ed3baafad48b4b","repo":"SubtitleEdit/subtitleedit","slug":"element-size-mismatch-for-element-fromfourcc-four","errorCode":null,"errorMessage":"Element size mismatch for element {FromFourCc(fourCc)} need {size} but have only {bytesleft}","messagePattern":"Element size mismatch for element (.+?) need (.+?) but have only (.+?)","errorType":"exception","errorClass":"RiffParserException","httpStatus":null,"severity":"error","filePath":"src/libse/ContainerFormats/RiffParser.cs","lineNumber":240,"sourceCode":"                return false;\n            }\n\n            // We have enough bytes, read\n            int fourCc;\n            int size;\n\n            ReadTwoInts(out fourCc, out size);\n\n            // Reduce bytes left\n            bytesleft -= TwoDWordSize;\n\n            // Do we have enough bytes?\n            if (bytesleft < size)\n            {\n                // Skip the bad data and throw an exception\n                SkipData(bytesleft);\n                bytesleft = 0;\n                throw new RiffParserException(\"Element size mismatch for element \" + FromFourCc(fourCc)\n                + \" need \" + size + \" but have only \" + bytesleft);\n            }\n\n            // Examine the element, is it a list or a chunk\n            string type = FromFourCc(fourCc);\n            if (type == List4Cc)\n            {\n                // We have a list\n                ReadOneInt(out fourCc);\n\n                if (null == list)\n                {\n                    SkipData(size - 4);\n                }\n                else\n                {\n                    // Invoke the list method\n                    list(this, fourCc, size - 4);","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/SubtitleEdit/subtitleedit/blob/17a9f0748781032255db3526b7215d2fb891e3af/src/libse/ContainerFormats/RiffParser.cs#L222-L258","documentation":"While descending the RIFF tree, RiffParser computed the remaining bytes in the current scope, then compared them to the chunk/list size just read. If the declared size exceeds the bytes remaining, the data is inconsistent; the parser skips the remaining bytes (to resync) and throws RiffParserException. This is the canonical 'file lies about chunk size' guard.","triggerScenarios":"A RIFF chunk/list declares a size larger than the enclosing scope; nested LIST size inconsistent with parent; truncated file where the final chunk's size header was written but data was cut.","commonSituations":"AVI/WAV produced by a buggy muxer; file partially overwritten; concatenated RIFF fragments without size fixup.","solutions":["Re-acquire the file from source; an inconsistent RIFF tree cannot be safely parsed.","Catch RiffParserException at the format-detection level and try alternate container formats.","Pre-validate by reading the top-level RIFF size and comparing to file length.","Use a tolerant parser that skips the bad chunk if salvage is required."],"exampleFix":"// before\nif (bytesleft < size) { SkipData(bytesleft); bytesleft = 0; throw new RiffParserException(\"Element size mismatch...\"); }\n\n// after\nif (bytesleft < size) { SkipData(bytesleft); bytesleft = 0; throw new RiffParserException($\"Element {FromFourCc(fourCc)} declared {size}B but only {bytesleft}B remain in scope\"); }","handlingStrategy":"try-catch","validationCode":"if (size > bytesleft) throw new InvalidDataException($\"Chunk {FromFourCc(fourCc)} size {size} exceeds remaining {bytesleft}\");","typeGuard":null,"tryCatchPattern":"try { rp.ReadElement(...); }\ncatch (RiffParserException ex) when (ex.Message.Contains(\"size mismatch\")) { /* corrupt RIFF; abort */ }","preventionTips":["Compare top-level RIFF size to file length before parsing.","Treat any size mismatch as unrecoverable corruption."],"tags":["riff","avi","wav","container-format","size-mismatch","corrupt"],"backgroundTag":null,"analyzedSha":"17a9f0748781032255db3526b7215d2fb891e3af","analyzedAt":"2026-08-13T18:11:43.374Z","schemaVersion":2},"datasetVersion":"2026-08-13T19:17:28.613Z"}