{"record":{"id":"6dceb35a931e5b78","repo":"SixLabors/ImageSharp","slug":"must-be-sizev5-bytes-was-data-length-bytes","errorCode":null,"errorMessage":"Must be {SizeV5} bytes. Was {data.Length} bytes.","messagePattern":"Must be (.+?) bytes\\. Was (.+?) bytes\\.","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs","lineNumber":470,"sourceCode":"        blueX: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(84, 4)),\n        blueY: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(88, 4)),\n        blueZ: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(92, 4)),\n        gammeRed: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(96, 4)),\n        gammeGreen: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(100, 4)),\n        gammeBlue: BinaryPrimitives.ReadInt32LittleEndian(data.Slice(104, 4)));\n\n    /// <summary>\n    /// Parses the full BMP Version 5 BITMAPINFOHEADER header (124 bytes).\n    /// </summary>\n    /// <param name=\"data\">The data to parse.</param>\n    /// <returns>The parsed header.</returns>\n    /// <seealso href=\"https://docs.microsoft.com/de-de/windows/win32/api/wingdi/ns-wingdi-bitmapv5header?redirectedfrom=MSDN\"/>\n    /// <exception cref=\"ArgumentException\">Invalid size.</exception>\n    public static BmpInfoHeader ParseV5(ReadOnlySpan<byte> data)\n    {\n        if (data.Length < SizeV5)\n        {\n            throw new ArgumentException($\"Must be {SizeV5} bytes. Was {data.Length} bytes.\", nameof(data));\n        }\n\n        return MemoryMarshal.Cast<byte, BmpInfoHeader>(data)[0];\n    }\n\n    /// <summary>\n    /// Writes a bitmap version 3 (Microsoft Windows NT) header to a buffer (40 bytes).\n    /// </summary>\n    /// <param name=\"buffer\">The buffer to write to.</param>\n    public void WriteV3Header(Span<byte> buffer)\n    {\n        buffer.Clear();\n        BinaryPrimitives.WriteInt32LittleEndian(buffer[..4], SizeV3);\n        BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(4, 4), this.Width);\n        BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(8, 4), this.Height);\n        BinaryPrimitives.WriteInt16LittleEndian(buffer.Slice(12, 2), this.Planes);\n        BinaryPrimitives.WriteUInt16LittleEndian(buffer.Slice(14, 2), this.BitsPerPixel);\n        BinaryPrimitives.WriteInt32LittleEndian(buffer.Slice(16, 4), (int)this.Compression);","sourceCodeStart":452,"sourceCodeEnd":488,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Bmp/BmpInfoHeader.cs#L452-L488","documentation":"BmpInfoHeader.ParseV5 reinterprets the input byte span as a BmpInfoHeader structure, which requires at least SizeV5 bytes. If the provided span is shorter, it throws ArgumentException naming the 'data' parameter with the required and actual lengths — the caller passed a buffer smaller than a BITMAPV5HEADER.","triggerScenarios":"Calling BmpInfoHeader.ParseV5 directly with a span sliced from a shorter header (e.g. a 40-byte BITMAPINFOHEADER region) or from a truncated file whose V5 header size field doesn't match available bytes.","commonSituations":"Custom metadata parsers reading V5 headers from truncated BMPs; code assuming SizeV5 bytes are always present after the file header.","solutions":["Read the header's declared size first and only call ParseV5 when the available span length >= BmpInfoHeader.SizeV5.","Pad/extend the buffer from the stream to at least SizeV5 bytes before parsing.","Use the decoder's public API instead of parsing the header span manually."],"exampleFix":"// before\nvar header = BmpInfoHeader.ParseV5(data[14..]); // may be < SizeV5\n// after\nif (data.Length - 14 >= BmpInfoHeader.SizeV5)\n    var header = BmpInfoHeader.ParseV5(data[14..]);","handlingStrategy":"type-guard","validationCode":"if (span.Length < BmpInfoHeader.SizeV5) throw new ArgumentException(\"Header buffer too short for V5.\");","typeGuard":"static bool IsV5HeaderPresent(ReadOnlySpan<byte> data) => data.Length >= 14 + BmpInfoHeader.SizeV5;","tryCatchPattern":"try { var h = BmpInfoHeader.ParseV5(data); }\ncatch (ArgumentException ex) { /* read declared header size and re-slice buffer */ }","preventionTips":["Read the header size field before slicing the parse buffer.","Ensure the buffer covers file header (14 bytes) + full info header.","Prefer the decoder's public API over manual header parsing."],"tags":["bmp","header-parsing","argument-validation"],"backgroundTag":"argument-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"}