JamesNK/Newtonsoft.Json · error · ArgumentOutOfRangeException

start

Error message

start

What it means

Thrown by StringUtils.Trim(string s, int start, int length) when start is negative — an ArgumentOutOfRangeException with paramName "start". This internal helper trims whitespace from a substring range of a string and is used during type-name parsing and other internal trimming operations. Negative start is invalid for any substring operation.

Source

Thrown at Src/Newtonsoft.Json/Utilities/StringUtils.cs:344

            return (source.Length > 0 && source[0] == value);
        }

        public static bool EndsWith(this string source, char value)
        {
            return (source.Length > 0 && source[source.Length - 1] == value);
        }

        public static string Trim(this string s, int start, int length)
        {
            // References: https://referencesource.microsoft.com/#mscorlib/system/string.cs,2691
            // https://referencesource.microsoft.com/#mscorlib/system/string.cs,1226
            if (s == null)
            {
                throw new ArgumentNullException();
            }
            if (start < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(start));
            }
            if (length < 0)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }
            int end = start + length - 1;
            if (end >= s.Length)
            {
                throw new ArgumentOutOfRangeException(nameof(length));
            }
            for (; start < end; start++)
            {
                if (!char.IsWhiteSpace(s[start]))
                {
                    break;
                }
            }
            for (; end >= start; end--)

View on GitHub (pinned to 4f73e74372)

Solutions

  1. Ensure any computed start index passed to Trim is non-negative.
  2. Sanitize/validate input strings (especially type names) before Json.NET processes them.
  3. Avoid calling this internal helper directly; prefer public string.Trim with valid ranges.

Example fix

// before
string t = StringUtils.Trim(name, computedStart, length); // computedStart < 0

// after
int computedStart = Math.Max(0, computedStartRaw);
string t = StringUtils.Trim(name, computedStart, length);
Defensive patterns

Strategy: validation

Validate before calling

if (start < 0) throw new ArgumentOutOfRangeException(nameof(start));
string t = StringUtils.Trim(s, start, length);

Prevention

When it happens

Trigger: Calling StringUtils.Trim with a negative start argument. Typically this surfaces indirectly when internal Json.NET helpers (e.g. SplitFullyQualifiedTypeName / type-name trimming) receive malformed input that produces a negative computed start.

Common situations: Corrupt or malformed fully-qualified type names processed under TypeNameHandling, custom code calling this internal API with bad offsets, or crafted JSON that triggers internal trimming with invalid bounds.

Related errors


AI-assisted analysis of JamesNK/Newtonsoft.Json@4f73e74372 (2026-08-07). Data as JSON: /api/errors/c15b559e119d742e. Report an issue: GitHub.