{"record":{"id":"52e2be46452dd559","repo":"dotnet/machinelearning","slug":"min-must-not-exceed-max-parameter-max","errorCode":null,"errorMessage":"min must not exceed max (Parameter 'max')","messagePattern":"min must not exceed max \\(Parameter 'max'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Core/Utilities/ArrayUtils.cs","lineNumber":79,"sourceCode":"                    limCur = mid;\n                else\n                    minCur = mid + 1;\n\n                Debug.Assert(min <= minCur && minCur <= limCur && limCur <= lim);\n                Debug.Assert(minCur == min || input[minCur - 1] < value);\n                Debug.Assert(limCur == lim || input[limCur] >= value);\n            }\n            Debug.Assert(min <= minCur && minCur == limCur && limCur <= lim);\n            Debug.Assert(minCur == min || input[minCur - 1] < value);\n            Debug.Assert(limCur == lim || input[limCur] >= value);\n\n            return minCur;\n        }\n\n        public static int EnsureSize<T>(ref T[] array, int min, int max, bool keepOld, out bool resized)\n        {\n            if (min > max)\n                throw new ArgumentOutOfRangeException(nameof(max), \"min must not exceed max\");\n\n            // This code adapted from the private method EnsureCapacity code of List<T>.\n            int size = ArrayUtils.Size(array);\n            if (size >= min)\n            {\n                resized = false;\n                return size;\n            }\n\n            int newSize = size == 0 ? 4 : size * 2;\n            // This constant taken from the internal code of system\\array.cs of mscorlib.\n            if ((uint)newSize > max)\n                newSize = max;\n            if (newSize < min)\n                newSize = min;\n            if (keepOld && size > 0)\n                Array.Resize(ref array, newSize);\n            else","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Core/Utilities/ArrayUtils.cs#L61-L97","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before\nArrayUtils.EnsureSize(ref buffer, requiredSize, currentCapacity, true, out resized);\n// after\nint max = Math.Max(requiredSize, currentCapacity);\nArrayUtils.EnsureSize(ref buffer, requiredSize, max, true, out resized);","handlingStrategy":"validation","validationCode":"if (min > max)\n    max = Math.Max(min, max); // or throw your own descriptive error\nArrayUtils.EnsureSize(ref array, min, max, keepOld, out resized);","typeGuard":null,"tryCatchPattern":"try\n{\n    ArrayUtils.EnsureSize(ref array, min, max, keepOld, out resized);\n}\ncatch (ArgumentOutOfRangeException)\n{\n    // min > max: recompute bounds from a single source of truth\n}","preventionTips":["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."],"tags":["argument-out-of-range","array-utils","ml-net"],"backgroundTag":"argument-out-of-range","analyzedSha":"7b76e69cf964daeca3f1377af6bc5543284d56c6","analyzedAt":"2026-09-11T12:35:38.930Z","contentChangedAt":"2026-09-11T12:35:38.930Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}