git-ecosystem/git-credential-manager · error · ArgumentOutOfRangeException

Argument must be less than or equal to

Error message

Argument must be less than or equal to {upper}.

What it means

This ArgumentOutOfRangeException is thrown by EnsureArgument.InRange when the argument exceeds the upper bound while upperInclusive is true. The value must satisfy arg <= upper. The interpolated message includes the actual upper bound and the parameter name is attached.

Solutions

  1. Verify the upper bound is correct: for zero-based inclusive indexes use length - 1, not length
  2. Clamp the value with Math.Min(value, upper) when exceeding the max is harmless
  3. Fix the computation (pagination, percentage) that produces an out-of-range value
  4. Validate user/config input at the boundary with a friendly message

Example fix

// before
EnsureArgument.InRange(index, nameof(index), 0, list.Count); // index == list.Count
// after
EnsureArgument.InRange(index, nameof(index), 0, list.Count - 1);
Defensive patterns

Strategy: validation

Validate before calling

if (value > upper) value = upper; // clamp
EnsureArgument.InRange(value, nameof(value), lower, upper);

Type guard

static bool IsWithin(int v, int lower, int upper) => v >= lower && v <= upper;

Try / catch

try { EnsureArgument.InRange(index, nameof(index), 0, list.Count - 1); }
catch (ArgumentOutOfRangeException ex)
{
    logger.LogWarning("{Param}={Value} exceeds max {Max}; clamping", ex.ParamName, index, list.Count - 1);
    index = list.Count - 1;
}

Prevention

When it happens

Trigger: Calling EnsureArgument.InRange(arg, name, lower, upper) (default upperInclusive: true) with arg > upper, e.g., an index equal to a collection's Count (one past the last element), or a percentage above 100.

Common situations: Classic off-by-one: using count instead of count - 1 as the inclusive max index; computing a page number exceeding the page count; user input above the allowed maximum.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of git-ecosystem/git-credential-manager@e8ce762cd0 (2026-09-11). Data as JSON: /api/errors/e89b05983755a9e6. Report an issue: GitHub.

Appendix: source

Thrown at src/Core/EnsureArgument.cs:91

                throw new ArgumentOutOfRangeException(name, "Argument must be negative.");
            }
        }

        public static void InRange(int arg, string name, int lower, int upper, bool lowerInclusive = true, bool upperInclusive = true)
        {
            if (lowerInclusive && arg < lower)
            {
                throw new ArgumentOutOfRangeException(name, $"Argument must be greater than or equal to {lower}.");
            }

            if (!lowerInclusive && arg <= lower)
            {
                throw new ArgumentOutOfRangeException(name, $"Argument must be strictly greater than {lower}.");
            }

            if (upperInclusive && arg > upper)
            {
                throw new ArgumentOutOfRangeException(name, $"Argument must be less than or equal to {upper}.");
            }

            if (!upperInclusive && arg >= upper)
            {
                throw new ArgumentOutOfRangeException(name, $"Argument must be strictly less than {upper}.");
            }
        }
    }
}

View on GitHub (pinned to e8ce762cd0)