{"record":{"id":"1dec5d69616841c0","repo":"SixLabors/ImageSharp","slug":"this-dimensions-invalid-memory-operation","errorCode":null,"errorMessage":"{this.Dimensions} (invalid memory operation)","messagePattern":"(.+?) \\(invalid memory operation\\)","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/ImageDecoderCore.cs","lineNumber":125,"sourceCode":"    /// <param name=\"configuration\">The shared configuration.</param>\n    /// <param name=\"stream\">The <see cref=\"Stream\" /> containing image data.</param>\n    /// <param name=\"cancellationToken\">The token to monitor for cancellation requests.</param>\n    /// <returns>The <see cref=\"ImageInfo\" />.</returns>\n    /// <exception cref=\"InvalidImageContentException\">Thrown if the encoded image contains errors.</exception>\n    public ImageInfo Identify(\n        Configuration configuration,\n        Stream stream,\n        CancellationToken cancellationToken)\n    {\n        using BufferedReadStream bufferedReadStream = new(configuration, stream, cancellationToken);\n\n        try\n        {\n            return this.Identify(bufferedReadStream, cancellationToken);\n        }\n        catch (InvalidMemoryOperationException ex)\n        {\n            throw new InvalidImageContentException(this.Dimensions, ex);\n        }\n        catch (Exception)\n        {\n            throw;\n        }\n    }\n\n    /// <summary>\n    /// Decodes the image from the specified stream to an <see cref=\"Image{TPixel}\" /> of a specific pixel type.\n    /// </summary>\n    /// <typeparam name=\"TPixel\">The pixel format.</typeparam>\n    /// <param name=\"configuration\">The shared configuration.</param>\n    /// <param name=\"stream\">The <see cref=\"Stream\" /> containing image data.</param>\n    /// <param name=\"cancellationToken\">The token to monitor for cancellation requests.</param>\n    /// <returns>The <see cref=\"Image{TPixel}\" />.</returns>\n    /// <exception cref=\"InvalidImageContentException\">Thrown if the encoded image contains errors.</exception>\n    public Image<TPixel> Decode<TPixel>(\n        Configuration configuration,","sourceCodeStart":107,"sourceCodeEnd":143,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/ImageDecoderCore.cs#L107-L143","documentation":"During Identify, ImageDecoderCore catches InvalidMemoryOperationException thrown by decoders when the image header implies buffers/dimensions that violate memory bounds, and rethrows it as InvalidImageContentException carrying the image dimensions. It signals the file declares sizes or layout that cannot be safely allocated.","triggerScenarios":"Calling Image.Identify/IdentifyAsync on a file whose header claims extreme dimensions, invalid component counts, or out-of-bounds offsets causing InvalidMemoryOperationException inside the format decoder.","commonSituations":"Corrupt or hostile images crafted to declare huge dimensions; truncated files where header fields are garbage; images produced by buggy encoders with inconsistent metadata.","solutions":["Catch InvalidImageContentException around Identify and treat the file as unidentifiable/corrupt","Pre-check file size and magic bytes so obviously truncated files are rejected before decoding","Re-obtain the file from a trusted source and verify its integrity","If dimensions are the concern, validate MaxWidth/MaxHeight options or pre-read header bounds"],"exampleFix":"// before\nvar info = Image.Identify(stream); // InvalidImageContentException\n// after\ntry { var info = Image.Identify(stream); }\ncatch (InvalidImageContentException ex) { logger.LogWarning(ex, \"Corrupt image\"); }","handlingStrategy":"try-catch","validationCode":"if (fileInfo.Length < 64) throw new InvalidOperationException(\"File too small to be a valid image.\");","typeGuard":null,"tryCatchPattern":"try { var info = Image.Identify(stream); }\ncatch (InvalidImageContentException ex)\n{\n    // header implies invalid memory operation — treat as corrupt\n    return Result.Corrupt;\n}","preventionTips":["Reject files from untrusted sources or validate magic bytes first","Catch InvalidImageContentException, not the internal InvalidMemoryOperationException","Check for truncation when network transfers are involved"],"tags":["dotnet","image-decoding","corrupt-data","memory"],"backgroundTag":"schema-validation-failed","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"}