louthy/language-ext · error · ArgumentException

The minimum value for

Error message

The minimum value for {nameof(bytesCount)} is 1

What it means

randomBase64 validates that bytesCount >= 1 and throws ArgumentException with 'The minimum value for bytesCount is 1' when a smaller value is passed. The method rents bytes from the shared ArrayPool and Base64-encodes the random bytes, so at least one byte is required.

Solutions

  1. Ensure bytesCount >= 1 before calling (clamp or validate)
  2. Fix the upstream computation producing 0 or negative byte counts
  3. Catch ArgumentException if the caller can legitimately pass dynamic values

Example fix

// before
var token = randomBase64(userConfig.KeyBytes); // KeyBytes = 0
// after
var token = randomBase64(Math.Max(1, userConfig.KeyBytes));
Defensive patterns

Strategy: validation

Validate before calling

if (bytesCount < 1) throw new ArgumentOutOfRangeException(nameof(bytesCount)); // or clamp: var n = Math.Max(1, bytesCount);

Type guard

static bool ValidByteCount(int n) => n >= 1;

Try / catch

try { var s = randomBase64(n); }
catch (ArgumentException ex) { /* n was 0/negative — fix upstream value */ }

Prevention

When it happens

Trigger: Calling Prelude.randomBase64(0) or randomBase64 with a negative int.

Common situations: Computing byte counts from parsed config/CLI values or division results that end up zero, e.g. randomBase64(length / 2) with length < 2.

Related errors


AI-assisted analysis of louthy/language-ext@2f0e362824 (2026-09-15). Data as JSON: /api/errors/c527b51bb3a42b46. Report an issue: GitHub.

Appendix: source

Thrown at LanguageExt.Core/Prelude/Random/Prelude.Random.cs:49

    public static int random(int max)
    {
        var bytes = ArrayPool<byte>.Shared.Rent(4);
        rnd.GetBytes(bytes);
        bytes[wordTop] &= 0x7f;
        var value = BitConverter.ToInt32(bytes, 0) % max;
        ArrayPool<byte>.Shared.Return(bytes);
        return value;
    }

    /// <summary>
    /// Thread-safe cryptographically strong random base-64 string generator
    /// </summary>
    /// <param name="bytesCount">number of bytes generated that are then 
    /// returned Base64 encoded</param>
    /// <returns>Base64 encoded random string</returns>
    public static string randomBase64(int bytesCount)
    {
        if (bytesCount < 1) throw new ArgumentException($"The minimum value for {nameof(bytesCount)} is 1");
        var bytes = ArrayPool<byte>.Shared.Rent(bytesCount);
        rnd.GetBytes(bytes);
        var r = Convert.ToBase64String(bytes);
        ArrayPool<byte>.Shared.Return(bytes);
        return r;
    }
}

View on GitHub (pinned to 2f0e362824)