{"record":{"id":"e93557f1d31fb53a","repo":"SixLabors/ImageSharp","slug":"restart-interval-must-be-in-0-65535-range","errorCode":null,"errorMessage":"Restart interval must be in [0..65535] range.","messagePattern":"Restart interval must be in \\[0\\.\\.65535\\] range\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/ImageSharp/Formats/Jpeg/JpegEncoder.cs","lineNumber":89,"sourceCode":"        }\n    }\n\n    /// <summary>\n    /// Gets numbers of MCUs between restart markers.\n    /// Defaults to <value>0</value>.\n    /// </summary>\n    /// <remarks>\n    /// Currently supported in progressive encoding only.\n    /// </remarks>\n    /// <exception cref=\"ArgumentException\">Restart interval must be in [0..65535] range.</exception>\n    public int RestartInterval\n    {\n        get => this.restartInterval;\n        init\n        {\n            if (value is < 0 or > 65535)\n            {\n                throw new ArgumentException(\"Restart interval must be in [0..65535] range.\");\n            }\n\n            this.restartInterval = value;\n        }\n    }\n\n    /// <summary>\n    /// Gets the component encoding mode.\n    /// </summary>\n    /// <remarks>\n    /// Interleaved encoding mode encodes all color components in a single scan.\n    /// Non-interleaved encoding mode encodes each color component in a separate scan.\n    /// </remarks>\n    public bool? Interleaved { get; init; }\n\n    /// <summary>\n    /// Gets the jpeg color for encoding.\n    /// </summary>","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/SixLabors/ImageSharp/blob/59ce6af6fc29027cda277ef62d4d1694a8acce91/src/ImageSharp/Formats/Jpeg/JpegEncoder.cs#L71-L107","documentation":"The JpegEncoder.RestartInterval property validates its value in its init accessor: it must be between 0 and 65535 inclusive because restart interval is encoded as a 16-bit field in the JPEG DRI marker. Passing a negative value or anything above 65535 throws ArgumentException immediately when the encoder options object is constructed.","triggerScenarios":"Setting JpegEncoder.RestartInterval (directly or via JpegEncoder.RestartInterval property initializer, Image load/save options, or configuration binding) to a value < 0 or > 65535, e.g. restartInterval: 100000 or a negative interval read from user configuration.","commonSituations":"Computing the interval from image width/height without clamping; binding an unvalidated app setting or CLI flag into encoder options; confusing restart interval (in MCUs) with restart marker spacing in bytes.","solutions":["Clamp the value before assigning: Math.Clamp(interval, 0, 65535).","Validate user/config input at the boundary and reject or clamp out-of-range intervals.","If you don't need restart markers, leave RestartInterval at its default (0)."],"exampleFix":"// before\nvar encoder = new JpegEncoder { RestartInterval = userInterval };\n// after\nvar encoder = new JpegEncoder { RestartInterval = Math.Clamp(userInterval, 0, 65535) };","handlingStrategy":"validation","validationCode":"if (restartInterval is < 0 or > 65535)\n    throw new ArgumentOutOfRangeException(nameof(restartInterval), restartInterval, \"Must be in [0..65535].\");\nvar encoder = new JpegEncoder { RestartInterval = restartInterval };","typeGuard":"static bool IsValidRestartInterval(int value) => value is >= 0 and <= 65535;","tryCatchPattern":null,"preventionTips":["Clamp any user/config-derived interval with Math.Clamp(value, 0, 65535) before assigning.","Remember the interval is in MCUs and is a 16-bit field; don't compute byte-based values into it.","Default to 0 (no restart markers) unless you specifically need them."],"tags":["jpeg","encoder","argument-out-of-range","configuration"],"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"}