{"record":{"id":"9dcd692b5a3d05c3","repo":"SixLabors/ImageSharp","slug":"bad-sampling-factor","errorCode":null,"errorMessage":"Bad sampling factor.","messagePattern":"Bad sampling factor\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs","lineNumber":20,"sourceCode":"// 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.\");\n\n    public static void ThrowNotSupportedColorSpace() => throw new NotSupportedException(\"Image color space could not be deduced.\");\n}\n","sourceCodeStart":2,"sourceCodeEnd":34,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Jpeg/JpegThrowHelper.cs#L2-L34","documentation":"JpegThrowHelper.ThrowBadSampling throws InvalidImageContentException when a frame's component sampling factors are invalid — each component's horizontal/vertical sampling factor must be in 1..4 per the JPEG spec. The parameterless overload reports a generic bad sampling factor.","triggerScenarios":"Decoding a JPEG whose SOF0/SOF2 segment declares a component sampling factor of 0 or greater than 4 (the message shows the offending factor in the int overload) — corrupt headers or non-conformant encoder output.","commonSituations":"Files from broken camera/embedded encoders; bit corruption; fuzzed or malicious uploads; truncated header bytes shifting parsing.","solutions":["Re-obtain or re-export the image; the file header is non-conformant.","Catch InvalidImageContentException and skip/quarantine in batch processing.","If you control the producer, fix the encoder so sampling factors are within 1..4."],"exampleFix":"// before\nusing var image = Image.Load(input);\n// after\ntry { using var image = Image.Load(input); }\ncatch (InvalidImageContentException ex)\n{\n    quarantine(ex, inputPath); // bad sampling factors in SOF\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try\n{\n    using var image = Image.Load(stream);\n}\ncatch (InvalidImageContentException ex) when (ex.Message.Contains(\"sampling\"))\n{\n    quarantine(stream, \"Bad sampling factors in SOF\");\n}","preventionTips":["Check image headers (SOF sampling factors within 1..4) if you control the producing encoder.","Reject or quarantine files from known-broken producers instead of decoding blind.","Validate magic bytes and run a parse check on all untrusted uploads before processing."],"tags":["jpeg","decoder","corrupt-data","sampling"],"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"}