{"record":{"id":"ecf8ec8b9fa219a2","repo":"SixLabors/ImageSharp","slug":"marker-marker-has-bad-length-length","errorCode":null,"errorMessage":"Marker {marker} has bad length {length}.","messagePattern":"Marker (.+?) has bad length (.+?)\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs","lineNumber":12,"sourceCode":"// Copyright (c) Six Labors.\n// Licensed under the Six Labors Split License.\n\nnamespace SixLabors.ImageSharp.Formats.Jpeg;\n\ninternal static class JpegThrowHelper\n{\n    public static void ThrowNotSupportedException(string errorMessage) => throw new NotSupportedException(errorMessage);\n\n    public static void ThrowInvalidImageContentException(string errorMessage) => throw new InvalidImageContentException(errorMessage);\n\n    public static void ThrowBadMarker(string marker, int length) => throw new InvalidImageContentException($\"Marker {marker} has bad length {length}.\");\n\n    public static void ThrowNotEnoughBytesForMarker(byte marker) => throw new InvalidImageContentException($\"Input stream does not have enough bytes to parse declared contents of the {marker:X2} marker.\");\n\n    public static void ThrowBadQuantizationTableIndex(int index) => throw new InvalidImageContentException($\"Bad Quantization Table index {index}.\");\n\n    public static void ThrowBadQuantizationTablePrecision(int precision) => throw new InvalidImageContentException($\"Unknown Quantization Table precision {precision}.\");\n\n    public static void ThrowBadSampling() => throw new InvalidImageContentException(\"Bad sampling factor.\");\n\n    public static void ThrowBadSampling(int factor) => throw new InvalidImageContentException($\"Bad sampling factor: {factor}\");\n\n    public static void ThrowBadProgressiveScan(int ss, int se, int ah, int al) => throw new InvalidImageContentException($\"Invalid progressive parameters Ss={ss} Se={se} Ah={ah} Al={al}.\");\n\n    public static void ThrowInvalidImageDimensions(int width, int height) => throw new InvalidImageContentException($\"Invalid image dimensions: {width}x{height}.\");\n\n    public static void ThrowDimensionsTooLarge(int width, int height) => throw new ImageFormatException($\"Image is too large to encode at {width}x{height} for JPEG format.\");\n\n    public static void ThrowNotSupportedComponentCount(int componentCount) => throw new NotSupportedException($\"Images with {componentCount} components are not supported.\");","sourceCodeStart":1,"sourceCodeEnd":30,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs#L1-L30","documentation":"JpegThrowHelper.ThrowBadMarker throws InvalidImageContentException when a JPEG marker segment declares a length that does not match what the decoder requires for that marker type (e.g. an SOFn/SOs/DQT segment whose stated length is impossible). It means the stream structure violates the JPEG (ITU T.81) specification.","triggerScenarios":"Decoding a JPEG where a marker segment (identified by the marker parameter, e.g. \"SOF0\", \"DQT\") has a declared segment length inconsistent with the component count or table size the decoder derives — corrupt or hand-crafted streams, truncation mid-segment.","commonSituations":"Corrupted downloads/uploads; files processed by buggy third-party encoders; fuzzed or malicious input in image-processing services; byte-level file corruption on disk.","solutions":["Re-obtain the image from a trusted source and validate checksums if available.","Catch InvalidImageContentException in batch pipelines and skip/quarantine the offending file.","Re-encode the file with a standard encoder if the original producer is at fault.","If decoding untrusted input, add the file to a rejection report instead of letting it crash the worker."],"exampleFix":"// before\nusing var image = Image.LoadAsync(stream);\n// after\ntry { using var image = await Image.LoadAsync(stream); }\ncatch (InvalidImageContentException ex)\n{\n    quarantine(stream, $\"Bad marker: {ex.Message}\");\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    using var image = await Image.LoadAsync(stream);\n}\ncatch (InvalidImageContentException ex)\n{\n    quarantine(stream, ex.Message); // bad marker length\n}","preventionTips":["Validate uploads with a quick parse (SOI marker + non-trivial size) before full decode.","Quarantine files that fail instead of failing whole batch jobs.","Re-encode images produced by non-standard tools before ingesting them."],"tags":["jpeg","decoder","corrupt-data","marker"],"backgroundTag":"corrupt-jpeg-data","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"}