{"record":{"id":"32235dab29dab8c0","repo":"JamesNK/Newtonsoft.Json","slug":"value-must-be-positive","errorCode":null,"errorMessage":"Value must be positive.","messagePattern":"Value must be positive\\.","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/JsonReader.cs","lineNumber":240,"sourceCode":"        public string? DateFormatString\n        {\n            get => _dateFormatString;\n            set => _dateFormatString = value;\n        }\n\n        /// <summary>\n        /// Gets or sets the maximum depth allowed when reading JSON. Reading past this depth will throw a <see cref=\"JsonReaderException\"/>.\n        /// A null value means there is no maximum. \n        /// The default value is <c>64</c>.\n        /// </summary>\n        public int? MaxDepth\n        {\n            get => _maxDepth;\n            set\n            {\n                if (value <= 0)\n                {\n                    throw new ArgumentException(\"Value must be positive.\", nameof(value));\n                }\n\n                _maxDepth = value;\n            }\n        }\n\n        /// <summary>\n        /// Gets the type of the current JSON token. \n        /// </summary>\n        public virtual JsonToken TokenType => _tokenType;\n\n        /// <summary>\n        /// Gets the text value of the current JSON token.\n        /// </summary>\n        public virtual object? Value => _value;\n\n        /// <summary>\n        /// Gets the .NET type for the current JSON token.","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/JsonReader.cs#L222-L258","documentation":"Thrown by the JsonReader.MaxDepth setter when assigning a value less than or equal to zero. MaxDepth caps nesting depth to defend against deeply nested or malicious JSON (stack overflow / denial of service); a non-positive cap is meaningless, so the setter rejects it with ArgumentException. Null is permitted and means no limit.","triggerScenarios":"Setting reader.MaxDepth = 0, reader.MaxDepth = -1, or computing MaxDepth from user/config input that can be zero or negative.","commonSituations":"Loading MaxDepth from configuration/environment without validating; passing 0 intending 'use default'; arithmetic that subtracts and yields a negative.","solutions":["Use a positive integer (e.g. 64, the library default) for MaxDepth.","Pass null to explicitly disable the depth cap rather than 0.","Validate config-driven values before assignment: if (depth is > 0 int d) reader.MaxDepth = d;."],"exampleFix":"// before\nreader.MaxDepth = 0;\n\n// after\nreader.MaxDepth = 64; // or leave null for no limit","handlingStrategy":"validation","validationCode":"if (configuredDepth is int d && d > 0)\n{\n    reader.MaxDepth = d;\n}","typeGuard":"static bool IsValidMaxDepth(int? v) => v is null or > 0;","tryCatchPattern":null,"preventionTips":["Treat null as 'no limit', not 0.","Bound config values: clamp to a sane positive default (e.g. 64).","Validate at config load and fail loudly rather than at serialization time."],"tags":["configuration","validation","max-depth"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}