Skip to content

DivideByZeroException in PbmDecoderCore.ProcessUpscaling #3130

@pawlos

Description

@pawlos

Prerequisites

  • I have written a descriptive issue title
  • 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

4.0.0 (commit: ff36e83) (not a regression, also present on 3.1.12)

Other ImageSharp packages and versions

N/A

Environment (Operating system, version and so on)

Ubuntu/Windows

.NET Framework version

.NET 10

Description

Loading a Netpbm image (PGM/PPM, ASCII or binary) with a maxval header field of 0 throws System.DivideByZeroException from PbmDecoderCore.ProcessUpscaling.

The standard puts this value to be less than 65536, and more than zero, so the input is malformed - and the decoder should surface it as InvalidImageContentException.

Any invalid value would trigger the same effect. In the current code maxPixelValue isn't validated after parsing, so 0 (either literally, or the parser's default when the field starts with a non-digit) ends up as the divisor in ProcessUpscaling.

Steps to Reproduce

var bytes = "P2 1 1 0\n0"u8.ToArray(); 
using var ms = new MemoryStream(bytes);
using var image = Image.Load<Rgba32>(ms); // throws DivideByZeroException

Reproduces with all four Netpbm magics (P2, P3, P5, P6).

Images

N/A. Example is self contained.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions