{"record":{"id":"ca669cfaf0e0f712","repo":"SixLabors/ImageSharp","slug":"invalid-max-pixel-value","errorCode":null,"errorMessage":"Invalid max pixel value.","messagePattern":"Invalid max pixel value\\.","errorType":"exception","errorClass":"InvalidImageContentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs","lineNumber":158,"sourceCode":"        if (!stream.SkipWhitespaceAndComments() ||\n            !stream.ReadDecimal(out int width) ||\n            !stream.SkipWhitespaceAndComments() ||\n            !stream.ReadDecimal(out int height) ||\n            !stream.SkipWhitespaceAndComments())\n        {\n            ThrowPrematureEof();\n        }\n\n        if (this.colorType != PbmColorType.BlackAndWhite)\n        {\n            if (!stream.ReadDecimal(out this.maxPixelValue))\n            {\n                ThrowPrematureEof();\n            }\n\n            if (this.maxPixelValue <= 0 || this.maxPixelValue >= 65536)\n            {\n                throw new InvalidImageContentException(\"Invalid max pixel value.\");\n            }\n\n            if (this.maxPixelValue > 255)\n            {\n                this.componentType = PbmComponentType.Short;\n            }\n            else\n            {\n                this.componentType = PbmComponentType.Byte;\n            }\n\n            stream.SkipWhitespaceAndComments();\n        }\n        else\n        {\n            this.componentType = PbmComponentType.Bit;\n        }\n","sourceCodeStart":140,"sourceCodeEnd":176,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Pbm/PbmDecoderCore.cs#L140-L176","documentation":"Thrown by PbmDecoderCore.ProcessHeader when a PBM/PGM/PPM header declares a max pixel value (MAXVAL) that is 0 or >= 65536. The PNM specification requires MAXVAL to be between 1 and 65535, so any value outside that range means the file is not a decodable Netpbm image. The library throws InvalidImageContentException during header parsing, before any pixels are read.","triggerScenarios":"Calling Image.Identify or Image.Decode on a PNM stream whose plain or binary header contains 'MAXVAL' <= 0 or >= 65536 (e.g. a corrupt or hand-written header, or a byte stream misidentified as PBM).","commonSituations":"Corrupted downloads, truncated or overwritten PNM files, generating PBM files with a buggy custom writer that emits MAXVAL 0, or feeding a non-PNM file to the decoder because the extension was .pgm/.ppm.","solutions":["Regenerate or re-export the image with a conforming PNM writer so MAXVAL is between 1 and 65535.","Open the file in a text/hex editor and fix the MAXVAL token in the header (e.g. 255 for 8-bit, 65535 for 16-bit).","Verify the file is actually a Netpbm image (magic P1-P7) and not another format mislabeled with a .pbm/.pgm/.ppm extension.","Wrap decode in try-catch for InvalidImageContentException and fall back to another decoder if the file is expected to be a different format."],"exampleFix":"// before: decoding a corrupt file directly\nusing var image = Image.Load(\"corrupt.pgm\");\n// after\ntry\n{\n    using var image = Image.Load(\"corrupt.pgm\");\n}\ncatch (InvalidImageContentException ex)\n{\n    // header MAXVAL out of [1, 65535]; inspect/repair the source file\n    Console.WriteLine($\"Corrupt PNM header: {ex.Message}\");\n}","handlingStrategy":"validation","validationCode":"// PNM MAXVAL must be 1..65535; sniff the plain-text header before decoding\nusing var reader = new StreamReader(stream, leaveOpen: true);\nstring magic = reader.ReadLine();\nif (magic is \"P2\" or \"P3\" or \"P5\" or \"P6\")\n{\n    // skip comments, read width/height/maxval tokens...\n    int maxval = ReadNextInt(reader);\n    if (maxval is < 1 or > 65535)\n        throw new InvalidDataException($\"PNM MAXVAL {maxval} out of range 1..65535\");\n}\nstream.Position = 0;","typeGuard":"static bool IsValidMaxPixelValue(int maxPixelValue) => maxPixelValue > 0 && maxPixelValue < 65536;","tryCatchPattern":"try\n{\n    using var image = Image.Load(pnmStream);\n}\ncatch (InvalidImageContentException ex) when (ex.Message == \"Invalid max pixel value.\")\n{\n    log.LogWarning(\"Rejected PNM with out-of-range MAXVAL: {Msg}\", ex.Message);\n}","preventionTips":["Validate MAXVAL in the 1..65535 range before handing PNM files to the decoder.","Only write PNM headers with conformant writer code paths; never format MAXVAL manually.","Scan user-supplied image batches with Image.Identify first and quarantine InvalidImageContentException files.","Keep the file extension and actual format in sync to avoid feeding foreign formats to the PNM decoder."],"tags":["pnm","image-decoding","invalid-header","corrupt-file"],"backgroundTag":"value-out-of-range","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"}