{"record":{"id":"552c160f8cdabbb5","repo":"JamesNK/Newtonsoft.Json","slug":"length","errorCode":null,"errorMessage":"length","messagePattern":"length","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Src/Newtonsoft.Json/Utilities/StringUtils.cs","lineNumber":348,"sourceCode":"        {\n            return (source.Length > 0 && source[source.Length - 1] == value);\n        }\n\n        public static string Trim(this string s, int start, int length)\n        {\n            // References: https://referencesource.microsoft.com/#mscorlib/system/string.cs,2691\n            // https://referencesource.microsoft.com/#mscorlib/system/string.cs,1226\n            if (s == null)\n            {\n                throw new ArgumentNullException();\n            }\n            if (start < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(start));\n            }\n            if (length < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(length));\n            }\n            int end = start + length - 1;\n            if (end >= s.Length)\n            {\n                throw new ArgumentOutOfRangeException(nameof(length));\n            }\n            for (; start < end; start++)\n            {\n                if (!char.IsWhiteSpace(s[start]))\n                {\n                    break;\n                }\n            }\n            for (; end >= start; end--)\n            {\n                if (!char.IsWhiteSpace(s[end]))\n                {\n                    break;","sourceCodeStart":330,"sourceCodeEnd":366,"githubUrl":"https://github.com/JamesNK/Newtonsoft.Json/blob/4f73e74372445108d2c1bda37b36e6f5e43402e0/Src/Newtonsoft.Json/Utilities/StringUtils.cs#L330-L366","documentation":"Defensive guard inside the internal helper StringUtils.Trim(string s, int start, int length) (Src/Newtonsoft.Json/Utilities/StringUtils.cs:334). It throws ArgumentOutOfRangeException with parameter name \"length\" whenever length is negative (StringUtils.cs:346-348), because a negative window size for trimming the substring [start, start+length) is meaningless. The only in-library caller, ReflectionUtils.SplitFullyQualifiedTypeName (ReflectionUtils.cs:867-868), derives length from a validated comma-delimiter index and can never produce a negative value, so this guard is effectively unreachable from any public Newtonsoft.Json deserialization/serialization path.","triggerScenarios":"Directly invoking the StringUtils.Trim(string, int, int) extension overload with a negative length argument (e.g. s.Trim(0, -1)). It is NOT reachable through the public API: SplitFullyQualifiedTypeName computes length as either assemblyDelimiterIndex (>=0) or fullyQualifiedTypeName.Length - assemblyDelimiterIndex - 1 (>=0 since the delimiter index is always <= Length-1 from GetAssemblyDelimiterIndex), so the guard only fires for code that calls this utility directly or in a fork.","commonSituations":"Contributors/fork maintainers calling the windowed Trim overload with hand-computed indices; unit tests exercising StringUtils directly; custom type-name splitters that reuse this helper with off-by-one arithmetic that underflows below zero.","solutions":["Guard or clamp length before calling: use int safeLength = Math.Max(0, length); so a negative computed value collapses to an empty window.","Recompute length defensively from boundaries (end - start) instead of passing a derived value that can underflow.","Prefer System.String.Trim() / Substring(start, len) for application code and reserve this internal overload for library internals."],"exampleFix":"// before\nstring t = s.Trim(start, end - start); // underflows when end < start\n\n// after\nstring t = (end >= start) ? s.Trim(start, end - start) : s;","handlingStrategy":"validation","validationCode":"int safeLength = Math.Max(0, length);\nif (start < 0) start = 0;\nstring result = s.Trim(start, safeLength);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Validate the window bounds (start>=0, length>=0, start+length<=s.Length) before invoking this internal Trim overload.","Derive length from a checked delimiter index the way ReflectionUtils.SplitFullyQualifiedTypeName does, rather than computing it ad hoc.","Treat StringUtils.Trim(int,int) as an internal utility; use System.String.Trim()/Substring for application code."],"tags":["argument-validation","strings","argumentoutofrange"],"analyzedSha":"4f73e74372445108d2c1bda37b36e6f5e43402e0","analyzedAt":"2026-08-07T06:10:08.596Z","schemaVersion":2},"datasetVersion":"2026-08-07T07:17:06.508Z"}