{"record":{"id":"a82e64c01bf38643","repo":"SixLabors/ImageSharp","slug":"the-bigtiff-directory-entry-count-exceeds-the-available-data","errorCode":null,"errorMessage":"The BigTIFF directory entry count exceeds the available data.","messagePattern":"The BigTIFF directory entry count exceeds the available data\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs","lineNumber":231,"sourceCode":"                {\n                    this.ReadValues(values, (uint)subIfdOffset);\n                }\n            }\n        }\n    }\n\n    protected void ReadValues64(List<IExifValue> values, ulong offset)\n    {\n        DebugGuard.MustBeLessThanOrEqualTo(offset, (ulong)this.data.Length, \"By spec UInt64.MaxValue is supported, but .NET Stream.Length can Int64.MaxValue.\");\n\n        this.Seek(offset);\n        ulong count = this.ReadUInt64();\n\n        // Each entry occupies 20 bytes and the directory ends with an 8-byte next-IFD offset.\n        long remainingDirectoryBytes = this.data.Length - this.data.Position;\n        if (remainingDirectoryBytes < 8 || count > (ulong)((remainingDirectoryBytes - 8) / 20))\n        {\n            throw new InvalidImageContentException(\"The BigTIFF directory entry count exceeds the available data.\");\n        }\n\n        Span<byte> offsetBuffer = stackalloc byte[8];\n        for (ulong i = 0; i < count; i++)\n        {\n            this.ReadValue64(values, offsetBuffer);\n        }\n    }\n\n    protected void ReadBigValue(IList<IExifValue> values, (ulong Offset, ExifDataType DataType, ulong NumberOfComponents, ExifValue Exif) tag, Span<byte> buffer)\n    {\n        this.Seek(tag.Offset);\n        if (this.TryReadSpan(buffer))\n        {\n            object? value = this.ConvertValue(tag.DataType, buffer, tag.NumberOfComponents > 1 || tag.Exif.IsArray);\n            this.Add(values, tag.Exif, value);\n        }\n    }","sourceCodeStart":213,"sourceCodeEnd":249,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Metadata/Profiles/Exif/ExifReader.cs#L213-L249","documentation":"During BigTIFF EXIF parsing, ReadValues64 validates that the declared directory entry count can physically fit in the remaining data (20 bytes per entry plus an 8-byte next-IFD offset). If the count is larger than the available bytes can hold, the file is malformed and an InvalidImageContentException is thrown to prevent reading garbage or looping excessively.","triggerScenarios":"Loading a BigTIFF image whose EXIF/TIFF IFD declares an entry count exceeding (remainingDirectoryBytes - 8) / 20 — i.e. corrupt or truncated files, wrong offsets, or count fields pointing beyond the buffer.","commonSituations":"Decoding damaged/truncated BigTIFF files; files edited by tools that wrote inconsistent IFD counts; fuzzed or maliciously crafted images (this check is an anti-DoS guard).","solutions":["Treat it as corrupt input: catch InvalidImageContentException around Image.Load and reject or quarantine the file.","Re-export/repair the file with a trusted tool (e.g. libtiff's tools, ImageMagick) before processing.","If it happens on files your pipeline produces, audit the BigTIFF writer to ensure entry counts and IFD offsets match the actual data written."],"exampleFix":"// before\nusing var image = Image.Load(path); // throws on malformed BigTIFF\n// after\ntry\n{\n    using var image = Image.Load(path);\n}\ncatch (InvalidImageContentException ex)\n{\n    logger.LogWarning(ex, \"Malformed BigTIFF rejected: {Path}\", path);\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { using var image = Image.Load(path); }\ncatch (InvalidImageContentException ex) when (ex.Message.Contains(\"BigTIFF\"))\n{ /* mark file corrupt / attempt repair with an external tool */ }","preventionTips":["Validate BigTIFF inputs at ingestion (identify + size checks) before processing.","Quarantine files that fail metadata parsing instead of retrying them.","If you write BigTIFF, assert that IFD entry counts match bytes actually written."],"tags":["bigtiff","exif","corrupt-input","parsing"],"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"}