{"record":{"id":"8b11a8f8a25405ed","repo":"SixLabors/ImageSharp","slug":"the-embedded-icon-resource-contains-an-invalid-seek-offset","errorCode":null,"errorMessage":"The embedded icon resource contains an invalid seek offset.","messagePattern":"The embedded icon resource contains an invalid seek offset\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Icon/IconFrameStream.cs","lineNumber":101,"sourceCode":"\n        return read;\n    }\n\n    /// <inheritdoc/>\n    public override long Seek(long offset, SeekOrigin origin)\n    {\n        long target = origin switch\n        {\n            SeekOrigin.Begin => offset,\n            SeekOrigin.Current => this.position + offset,\n            SeekOrigin.End => this.length + offset,\n            _ => throw new ArgumentOutOfRangeException(nameof(origin))\n        };\n\n        // Casting rejects both negative offsets and offsets beyond Length with one bounds check.\n        if ((ulong)target > (ulong)this.length)\n        {\n            throw new InvalidImageContentException(\"The embedded icon resource contains an invalid seek offset.\");\n        }\n\n        // Delay moving the containing stream until Read; this keeps logical seeks isolated from sibling resources.\n        this.position = target;\n        return target;\n    }\n\n    /// <inheritdoc/>\n    public override void SetLength(long value) => throw new NotSupportedException();\n\n    /// <inheritdoc/>\n    public override void Write(byte[] buffer, int offset, int count) => throw new NotSupportedException();\n}\n","sourceCodeStart":83,"sourceCodeEnd":115,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Icon/IconFrameStream.cs#L83-L115","documentation":"Thrown by IconFrameStream.Seek when the computed seek target is negative or beyond the embedded icon resource's Length; the single unsigned comparison rejects both cases. SixLabors.ImageSharp throws InvalidImageContentException because a seek outside the embedded child resource indicates corrupt directory metadata or a malformed payload stream.","triggerScenarios":"Child decoder seeks (origin Begin/Current/End) computing a target outside [0, Length] of the embedded entry resource — caused by corrupt BytesInRes/ImageOffset metadata or a decoder reading inconsistent chunk sizes.","commonSituations":"Corrupt or fuzzed ICO/CUR files whose declared payload size does not match actual chunk structure; malformed BMP/PNG inner payloads that trigger out-of-range seeks during parsing.","solutions":["Verify ImageOffset and BytesInRes of the entry are consistent with the file size","Re-export the icon from a trusted source","Catch InvalidImageContentException and treat the icon as corrupt","If parsing icons from untrusted input, run validation first or sandbox the operation"],"exampleFix":"// before\nvar image = Image.Load(\"untrusted.ico\");\n// after\ntry\n{\n    var image = Image.Load(\"untrusted.ico\");\n}\ncatch (InvalidImageContentException ex)\n{\n    // invalid seek offset in embedded icon resource\n    logger.LogWarning(ex, \"Corrupt icon rejected\");\n    image = null;\n}","handlingStrategy":"try-catch","validationCode":"// confirm the entry payload exists and is bounded before decoding\nstatic bool EntryPayloadFits(ReadOnlySpan<byte> ico, int entryIndex)\n{\n    int e = 6 + 16 * entryIndex;\n    uint offset = BitConverter.ToUInt32(ico.Slice(e + 12, 4));\n    uint size = BitConverter.ToUInt32(ico.Slice(e + 8, 4));\n    return offset + size <= (uint)ico.Length;\n}","typeGuard":"static bool IsInvalidImageContent(Exception ex) => ex is InvalidImageContentException;","tryCatchPattern":"try\n{\n    var image = Image.Load(icoStream);\n}\ncatch (InvalidImageContentException ex)\n{\n    // child decoder seeked outside the embedded resource: corrupt metadata\n    LogCorruptIcon(icoStream, ex);\n}","preventionTips":["Validate ImageOffset/BytesInRes consistency before decoding untrusted icons","Regenerate icons with inconsistent inner chunk sizes","Treat repeated seek failures as file corruption, not a library bug"],"tags":["icon","ico","stream","invalid-image-content"],"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"}