{"record":{"id":"c52ca00a1cb1e230","repo":"LykosAI/StabilityMatrix","slug":"invalid-riff-header","errorCode":null,"errorMessage":"Invalid RIFF header","messagePattern":"Invalid RIFF header","errorType":"exception","errorClass":"InvalidDataException","httpStatus":null,"severity":"error","filePath":"StabilityMatrix.Core/Helper/Webp/WebpReader.cs","lineNumber":30,"sourceCode":"\n        while (BaseStream.Position < headerFileSize)\n        {\n            if (ReadVoidChunk() is \"ANMF\" or \"ANIM\")\n            {\n                return true;\n            }\n        }\n\n        return false;\n    }\n\n    private void ReadHeader()\n    {\n        // RIFF\n        var riff = ReadBytes(4);\n        if (!riff.SequenceEqual([..\"RIFF\"u8]))\n        {\n            throw new InvalidDataException(\"Invalid RIFF header\");\n        }\n\n        // Size: uint32\n        headerFileSize = ReadUInt32();\n\n        // WEBP\n        var webp = ReadBytes(4);\n        if (!webp.SequenceEqual([..\"WEBP\"u8]))\n        {\n            throw new InvalidDataException(\"Invalid WEBP header\");\n        }\n    }\n\n    // Read a single chunk and discard its contents\n    private string ReadVoidChunk()\n    {\n        // FourCC: 4 bytes in ASCII\n        var result = ReadBytes(4);","sourceCodeStart":12,"sourceCodeEnd":48,"githubUrl":"https://github.com/LykosAI/StabilityMatrix/blob/af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002/StabilityMatrix.Core/Helper/Webp/WebpReader.cs#L12-L48","documentation":"WebpReader.ReadHeader parses the first bytes of a WEBP container. It expects a 'RIFF' magic signature and throws InvalidDataException when the first 4 bytes differ, meaning the stream is not a RIFF container and therefore cannot be a WEBP image.","triggerScenarios":"Calling GetIsAnimatedFlag on a file/stream whose first bytes are not 'RIFF' — a PNG/JPEG/GIF misnamed as .webp, a truncated or zero-byte file, or reading from an offset that skips the header.","commonSituations":"Inspecting user-imported images with wrong extensions; metadata scrapers reading partially downloaded files; passing an already-decompressed bitmap stream instead of the raw file.","solutions":["Validate the file starts with 'RIFF'....'WEBP' before using WebpReader","Ensure you are reading the raw file bytes from offset 0","Treat the file as non-animated or skip it when the header is invalid","Re-obtain the file if it is truncated/corrupt"],"exampleFix":"// before\nusing var reader = new WebpReader(stream);\nvar animated = reader.GetIsAnimatedFlag();\n// after\nSpan<byte> magic = stackalloc byte[4];\nstream.ReadExactly(magic);\nstream.Position = 0;\nif (!magic.SequenceEqual(\"RIFF\"u8)) return false;\nusing var reader = new WebpReader(stream);\nvar animated = reader.GetIsAnimatedFlag();","handlingStrategy":"validation","validationCode":"byte[] head = new byte[4];\nusing (var fs = File.OpenRead(path)) fs.ReadExactly(head);\nbool isRiff = head.AsSpan().SequenceEqual(\"RIFF\"u8);","typeGuard":"static bool LooksLikeRiff(Stream s) { var b = new byte[4]; long pos = s.Position; s.ReadExactly(b); s.Position = pos; return b.AsSpan().SequenceEqual(\"RIFF\"u8); }","tryCatchPattern":"try { animated = new WebpReader(stream).GetIsAnimatedFlag(); }\ncatch (InvalidDataException) { animated = false; /* not a WEBP/RIFF file */ }","preventionTips":["Verify magic bytes before format-specific parsing","Only feed raw file streams (position 0) to WebpReader","Treat wrong-extension files as unsupported rather than retrying","Validate downloads are complete before parsing headers"],"tags":["csharp","parsing","webp","file-format"],"backgroundTag":"invalid-argument-format","analyzedSha":"af93d6ef57c01cd890d7e0ad0a9ea8c9fcda3002","analyzedAt":"2026-09-12T19:02:43.389Z","contentChangedAt":"2026-09-12T19:02:43.389Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}