{"record":{"id":"afaff05c7b6201fc","repo":"SixLabors/ImageSharp","slug":"invalid-gif-colormap-size-result-globalcolortablesize","errorCode":null,"errorMessage":"Invalid gif colormap size '{result.GlobalColorTableSize}'","messagePattern":"Invalid gif colormap size '(.+?)'","errorType":"exception","errorClass":"ImageFormatException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs","lineNumber":98,"sourceCode":"    /// Gets the color depth, in number of bits per pixel.\n    /// The lowest 3 packed bits represent the bit depth minus 1.\n    /// </summary>\n    public int BitsPerPixel => (this.Packed & 0x07) + 1;\n\n    public void WriteTo(Span<byte> buffer)\n    {\n        ref GifLogicalScreenDescriptor dest = ref Unsafe.As<byte, GifLogicalScreenDescriptor>(ref MemoryMarshal.GetReference(buffer));\n\n        dest = this;\n    }\n\n    public static GifLogicalScreenDescriptor Parse(ReadOnlySpan<byte> buffer)\n    {\n        GifLogicalScreenDescriptor result = MemoryMarshal.Cast<byte, GifLogicalScreenDescriptor>(buffer)[0];\n\n        if (result.GlobalColorTableSize > 255 * 4)\n        {\n            throw new ImageFormatException($\"Invalid gif colormap size '{result.GlobalColorTableSize}'\");\n        }\n\n        return result;\n    }\n\n    public static byte GetPackedValue(bool globalColorTableFlag, int colorResolution, bool sortFlag, int globalColorTableSize)\n    {\n        /*\n        Global Color Table Flag    | 1 Bit\n        Color Resolution           | 3 Bits\n        Sort Flag                  | 1 Bit\n        Size of Global Color Table | 3 Bits\n        */\n\n        byte value = 0;\n\n        if (globalColorTableFlag)\n        {","sourceCodeStart":80,"sourceCodeEnd":116,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Gif/Sections/GifLogicalScreenDescriptor.cs#L80-L116","documentation":"Thrown when parsing the GIF Logical Screen Descriptor if the computed global color table size exceeds the maximum the format and this decoder allow (255 * 4 entries). A larger value indicates corrupt packed-field bits or a non-conformant file. Raised as ImageFormatException during header parsing.","triggerScenarios":"Image.Load/Decode on a GIF whose packed field's global-color-table-size bits decode to a value greater than 255*4 — a corrupt or maliciously malformed header.","commonSituations":"Bit-flipped/corrupt headers from damaged storage, hand-crafted fuzzed GIFs, or files passed through transformers that mangle the packed byte.","solutions":["Re-obtain the GIF from a known-good source; the header packed field is corrupt","Validate the file (e.g. magick identify) before processing to reject malformed inputs early","Catch ImageFormatException at your ingest boundary and quarantine the file","If files come from an untrusted pipeline, sanitize/re-encode them before ImageSharp decode"],"exampleFix":"// before\nvar image = Image.Load(stream);\n// after\ntry { var image = Image.Load(stream); }\ncatch (ImageFormatException ex) { quarantine(path, ex); }","handlingStrategy":"validation","validationCode":"// Check the global color table size bits in the packed field before decode\nif (stream.CanSeek)\n{\n    var head = new byte[13]; long pos = stream.Position; stream.Read(head, 0, 13); stream.Position = pos;\n    if ((head[10] & 0x80) != 0 && ((head[10] & 0x07) > 7)) throw new InvalidDataException(\"Malformed GIF color table size\");\n}\n","typeGuard":null,"tryCatchPattern":"try { return Image.Load(stream); }\ncatch (ImageFormatException ex) when (ex.Message.StartsWith(\"Invalid gif colormap size\")) { Quarantine(path, ex); return null; }","preventionTips":["Quarantine files failing header validation instead of crashing batch jobs","Re-encode untrusted GIFs before decoding","Validate uploads with magick identify or Image.Identify first","Treat this as corruption — always re-fetch from source"],"tags":["gif","decoder","header","validation"],"backgroundTag":"value-out-of-range","analyzedSha":"59ce6af6fc29027cda277ef62d4d1694a8acce91","analyzedAt":"2026-09-13T18:34:59.331Z","contentChangedAt":"2026-09-13T18:34:59.331Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}