{"record":{"id":"b978df7ed2497457","repo":"SixLabors/ImageSharp","slug":"not-enough-bytes-to-read-icon-header","errorCode":null,"errorMessage":"Not enough bytes to read icon header.","messagePattern":"Not enough bytes to read icon header\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Icon/IconDecoderCore.cs","lineNumber":383,"sourceCode":"\n            // Some established single-image ICO files overstate BytesInRes but contain a complete payload.\n            // The containing stream is still a safe hard boundary because no sibling image can follow it.\n            length = remaining;\n        }\n\n        frameStream.Reset(basePosition + entry.ImageOffset, length);\n    }\n\n    /// <summary>\n    /// Ensures that a complete fixed-size directory structure was read.\n    /// </summary>\n    /// <param name=\"bytesRead\">The number of bytes read.</param>\n    /// <param name=\"expectedLength\">The required structure length.</param>\n    private static void CheckEndOfStream(int bytesRead, int expectedLength)\n    {\n        if (bytesRead != expectedLength)\n        {\n            throw new InvalidImageContentException(\"Not enough bytes to read icon header.\");\n        }\n    }\n}\n","sourceCodeStart":365,"sourceCodeEnd":387,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Icon/IconDecoderCore.cs#L365-L387","documentation":"Thrown by CheckEndOfStream when a read of an icon structure (ICONDIR header or ICONDIRENTRY) returned fewer bytes than the required structure length, meaning the stream ended mid-header. SixLabors.ImageSharp throws InvalidImageContentException because the icon file is truncated and its directory cannot be read.","triggerScenarios":"Calling Image.Load or Image.Identify on a file shorter than 6 bytes (no full ICONDIR) or whose stream ends before all 16-byte ICONDIRENTRY structures are read — e.g. a zero/one-byte file or an icon truncated inside the directory.","commonSituations":"Empty placeholder .ico files; interrupted downloads; files cut off at an exact block boundary; test fixtures accidentally committed empty.","solutions":["Check the file size is at least 6 + 16*Count bytes before loading","Re-download or restore the complete icon file","Catch InvalidImageContentException and treat the file as corrupt/empty","If bundling icons, verify build packaging did not truncate them"],"exampleFix":"// before\nvar image = Image.Load(\"icon.ico\");\n// after\nvar fi = new FileInfo(\"icon.ico\");\nif (fi.Length < 6)\n    throw new InvalidOperationException(\"File too small to be an ICO\");\nvar image = Image.Load(\"icon.ico\");","handlingStrategy":"validation","validationCode":"static bool IcoHeaderIsComplete(string path)\n{\n    var fi = new FileInfo(path);\n    if (fi.Length < 6) return false;\n    using var fs = File.OpenRead(path);\n    Span<byte> b = stackalloc byte[6];\n    fs.ReadExactly(b);\n    ushort count = (ushort)(b[4] | (b[5] << 8));\n    return fi.Length >= 6 + 16L * count;\n}","typeGuard":null,"tryCatchPattern":"try\n{\n    var image = Image.Load(path);\n}\ncatch (InvalidImageContentException)\n{\n    // stream ended inside the icon header: file truncated\n    image = null;\n}","preventionTips":["Check file size >= 6 + 16*Count before loading","Validate download completeness via checksum/size","Guard against empty placeholder files in packaging"],"tags":["icon","ico","truncated-file","end-of-stream"],"backgroundTag":"invalid-image-content","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"}