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

Argument must be positive or zero (non-negative).

Error message

Argument must be positive or zero (non-negative).

What it means

This ArgumentOutOfRangeException is thrown by EnsureArgument.PositiveOrZero when an int argument is negative. The parameter is expected to be non-negative (>= 0), e.g., a count, index, timeout, or capacity. The exception carries the parameter name for diagnosis.

Solutions

  1. Ensure the value is >= 0 before the call: clamp with Math.Max(0, value) if negatives are meaningless
  2. Find why the value is negative (sentinel usage, subtraction, config) and fix the producer
  3. If -1 is a legitimate "unset" sentinel, use a nullable int (int?) or a separate flag instead
  4. Validate config/env values at startup with a clear error message

Example fix

// before
EnsureArgument.PositiveOrZero(retries, nameof(retries)); // retries == -1
// after
var safeRetries = retries == -1 ? 0 : Math.Max(0, retries);
EnsureArgument.PositiveOrZero(safeRetries, nameof(safeRetries));
Defensive patterns

Strategy: validation

Validate before calling

if (value < 0) throw new InvalidOperationException($"{name} must be >= 0, was {value}");
// or clamp: value = Math.Max(0, value);

Type guard

static bool IsNonNegative(int v) => v >= 0;

Try / catch

try { EnsureArgument.PositiveOrZero(retries, nameof(retries)); }
catch (ArgumentOutOfRangeException ex)
{
    logger.LogError("{Param} was {Value}; must be non-negative", ex.ParamName, retries);
    throw new ConfigurationErrorsException("Retries must be >= 0", ex);
}

Prevention

When it happens

Trigger: Calling EnsureArgument.PositiveOrZero(arg, name) with arg < 0, e.g., a computed value that underflowed, a config value like "-1", or a subtracted count that went below zero.

Common situations: Passing a -1 sentinel value where only >= 0 is allowed; computing a remaining-count that went negative; env var or config parsed to a negative number; integer subtraction overflow to negative.

Related errors


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

Appendix: source

Thrown at src/Core/EnsureArgument.cs:49

                throw new ArgumentException("Argument cannot be empty or white space.", name);
            }
        }

        public static void AbsoluteUri(Uri arg, string name)
        {
            NotNull(arg, name);

            if (!arg.IsAbsoluteUri)
            {
                throw new ArgumentException("Argument must be an absolute URI.", name);
            }
        }

        public static void PositiveOrZero(int arg, string name)
        {
            if (arg < 0)
            {
                throw new ArgumentOutOfRangeException(name, "Argument must be positive or zero (non-negative).");
            }
        }

        public static void Positive(int arg, string name)
        {
            if (arg <= 0)
            {
                throw new ArgumentOutOfRangeException(name, "Argument must be positive.");
            }
        }

        public static void NegativeOrZero(int arg, string name)
        {
            if (arg > 0)
            {
                throw new ArgumentOutOfRangeException(name, "Argument must be negative or zero (non-positive).");
            }
        }

View on GitHub (pinned to e8ce762cd0)