dotnet/machinelearning · error · ArgumentOutOfRangeException
min must not exceed max (Parameter 'max')
Error message
min must not exceed max (Parameter 'max')
What it means
ArrayUtils.EnsureSize<T> grows (or optionally shrinks) a backing array so its length is at least 'min' and at most 'max'. The API contract requires min <= max; when min exceeds max the method throws ArgumentOutOfRangeException on parameter 'max' before doing any resizing. This invariant keeps callers from requesting contradictory bounds that the resize logic cannot satisfy.
Source
Thrown at src/Microsoft.ML.Core/Utilities/ArrayUtils.cs:79
limCur = mid;
else
minCur = mid + 1;
Debug.Assert(min <= minCur && minCur <= limCur && limCur <= lim);
Debug.Assert(minCur == min || input[minCur - 1] < value);
Debug.Assert(limCur == lim || input[limCur] >= value);
}
Debug.Assert(min <= minCur && minCur == limCur && limCur <= lim);
Debug.Assert(minCur == min || input[minCur - 1] < value);
Debug.Assert(limCur == lim || input[limCur] >= value);
return minCur;
}
public static int EnsureSize<T>(ref T[] array, int min, int max, bool keepOld, out bool resized)
{
if (min > max)
throw new ArgumentOutOfRangeException(nameof(max), "min must not exceed max");
// This code adapted from the private method EnsureCapacity code of List<T>.
int size = ArrayUtils.Size(array);
if (size >= min)
{
resized = false;
return size;
}
int newSize = size == 0 ? 4 : size * 2;
// This constant taken from the internal code of system\array.cs of mscorlib.
if ((uint)newSize > max)
newSize = max;
if (newSize < min)
newSize = min;
if (keepOld && size > 0)
Array.Resize(ref array, newSize);
elseView on GitHub (pinned to 7b76e69cf9)
Solutions
- Verify the min/max computation: ensure max is always >= min before the call, e.g. use Math.Max(min, currentSize) for the max parameter.
- If you intended simple growth, pass the same value or a larger value for max (or the maximum allowed size) rather than a stale capacity value.
- Guard the call: if (min > max) swap or clamp the values before invoking EnsureSize.
Example fix
// before ArrayUtils.EnsureSize(ref buffer, requiredSize, currentCapacity, true, out resized); // after int max = Math.Max(requiredSize, currentCapacity); ArrayUtils.EnsureSize(ref buffer, requiredSize, max, true, out resized);
Defensive patterns
Strategy: validation
Validate before calling
if (min > max)
max = Math.Max(min, max); // or throw your own descriptive error
ArrayUtils.EnsureSize(ref array, min, max, keepOld, out resized); Try / catch
try
{
ArrayUtils.EnsureSize(ref array, min, max, keepOld, out resized);
}
catch (ArgumentOutOfRangeException)
{
// min > max: recompute bounds from a single source of truth
} Prevention
- Derive min and max from one shared computation to keep them consistent.
- Add a debug assert (Debug.Assert(min <= max)) at call sites.
- Watch for integer overflow when computing large sizes.
When it happens
Trigger: Calling ArrayUtils.EnsureSize(ref arr, min, max, keepOld, out resized) with min > max, e.g. computing min and max from two different length calculations that disagree, or passing a fixed max (like a current capacity) with a min computed from a larger required size.
Common situations: Custom data loaders or transforms in ML.NET pipelines that pre-compute required buffer sizes; off-by-one or integer-overflow bugs when computing new sizes; passing ArrayUtils.Size(oldArray) as max while min comes from a growing dataset.
Related errors
- Specified argument was out of the range of valid values. (Pa
- index
- Strings.IndexIsGreaterThanColumnLength
- nameof(startIndex)
- PositiveNumberOfCharacters
AI-assisted analysis of dotnet/machinelearning@7b76e69cf9 (2026-09-11).
Data as JSON: /api/errors/52e2be46452dd559.
Report an issue: GitHub.