You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DivideByZeroException in BmpDecoderCore.ReadRgbPalette with bitsPerPixel=0 #3067
I have verified that I am running the latest version of ImageSharp
I have verified if the problem exist in both DEBUG and RELEASE mode
I have searched open and closed issues to ensure it has not already been reported
ImageSharp version
3.1.12
Other ImageSharp packages and versions
N/A — only SixLabors.ImageSharp
Environment (Operating system, version and so on)
Linux (WSL2, Ubuntu 22.04) and Windows 11
.NET Framework version
.NET 10.0
Description
A crafted BMP file with bitsPerPixel = 0 in the DIB header causes an unhandled DivideByZeroException in BmpDecoderCore.ReadRgbPalette. This exception is not part of the ImageSharp exception hierarchy (ImageFormatException / InvalidImageContentException / UnknownImageFormatException), so applications following the documented error handling pattern cannot catch it.
For comparison, other malformed inputs (truncated PNG, random garbage, etc.) correctly throw InvalidImageContentException or UnknownImageFormatException.
The root cause is in BmpDecoderCore.ReadRgbPalette:
intppb=8/bitsPerPixel;// DivideByZeroException when bitsPerPixel == 0
The Decode() method routes to ReadRgbPalette when BmpCompression.RGB and BitsPerPixel <= 8 — a value of 0 matches this condition.
A suggested fix would be to validate bitsPerPixel before the switch:
if(bitsPerPixelis not (1 or 2 or 4 or 8 or 16 or 24 or 32)){BmpThrowHelper.ThrowInvalidImageContentException($"Invalid bits per pixel: {bitsPerPixel}");}
Found by coverage-guided fuzzing with SharpFuzz + AFL++.
Activity
Fix #3067
Fix #3067