Humanizr/Humanizer · error · ArgumentOutOfRangeException

Argument must be non-negative.

Error message

Argument must be non-negative.

What it means

This is a polyfill implementation of ArgumentOutOfRangeException.ThrowIfNegative(int) for .NET Framework 4.8 and other pre-NET5 targets. It fires whenever an internal Humanizer API validates that a count or index is non-negative and the caller passed a negative value. On NET5+ the runtime's built-in method is used instead.

Source

Thrown at src/Humanizer/PolyfillShims.cs:32

    extension(ArgumentNullException)
    {
        [Intrinsic]
        public static void ThrowIfNull(
            object? argument,
            [CallerArgumentExpression(nameof(argument))] string? paramName = null)
            => _ = argument ?? throw new ArgumentNullException(paramName);
    }

    extension(ArgumentOutOfRangeException)
    {
        /// <summary>Throws an <see cref="ArgumentOutOfRangeException"/> if <paramref name="value"/> is negative.</summary>
        /// <param name="value">The argument to validate as non-negative.</param>
        /// <param name="paramName">The name of the parameter with which <paramref name="value"/> corresponds.</param>
        public static void ThrowIfNegative(int value, [CallerArgumentExpression(nameof(value))] string? paramName = null)
        {
            if (value < 0)
            {
                throw new ArgumentOutOfRangeException(paramName, value, "Argument must be non-negative.");
            }
        }
    }

    // ---- Double ----
    extension(double)
    {
        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsFinite(double x)
        {
            var bits = (ulong)BitConverter.DoubleToInt64Bits(x);
            return (bits & 0x7FF0_0000_0000_0000UL) != 0x7FF0_0000_0000_0000UL;
        }

        [MethodImpl(MethodImplOptions.AggressiveInlining)]
        public static bool IsIntegral(double x)
        {
            if (!IsFinite(x))

View on GitHub (pinned to ffc2b77c0f)

Solutions

  1. Ensure all integer arguments passed to Humanizer extension methods are non-negative.
  2. Add input validation in your calling code before invoking Humanizer APIs.
  3. If on net48 specifically, audit callsites that might pass negative counts.

Example fix

// before
var h = TimeSpan.Humanize(precision: -1);

// after
var h = TimeSpan.Humanize(precision: Math.Max(0, precision));
Defensive patterns

Strategy: validation

Validate before calling

ArgumentOutOfRangeException.ThrowIfNegative(myValue);

Prevention

When it happens

Trigger: Any internal Humanizer API on net48 that delegates to ArgumentOutOfRangeException.ThrowIfNegative with a negative int argument (e.g. negative precision, negative digit count). The polyfill is compiled only when NET5_0_OR_GREATER is not defined.

Common situations: Running on net48 with a caller that passes -1 for a parameter Humanizer validates. Mixing versions where a previously-unvalidated parameter now has a guard. Fuzzing or unit tests with negative edge cases.

Related errors


AI-assisted analysis of Humanizr/Humanizer@ffc2b77c0f (2026-08-13). Data as JSON: /api/errors/9e391155dd408973. Report an issue: GitHub.