{"record":{"id":"2f7a694a7e736dd1","repo":"dotnet/machinelearning","slug":"count-must-be-non-negative-and-count-must-not-exce","errorCode":null,"errorMessage":"Count must be non-negative and count must not exceed buffer.Length - offset. (Parameter 'count')","messagePattern":"Count must be non-negative and count must not exceed buffer\\.Length - offset\\. \\(Parameter 'count'\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.ML.Data/Utilities/StreamUtils.cs","lineNumber":206,"sourceCode":"        /// <paramref name=\"count\"/> was negative, or the range specified by the combination of\n        /// <paramref name=\"offset\"/> and <paramref name=\"count\"/> exceed the length of <paramref name=\"buffer\"/>.\n        /// </exception>\n        [MethodImpl(MethodImplOptions.AggressiveInlining)]\n        private static void ValidateBufferArguments(byte[] buffer, int offset, int count)\n        {\n            if (buffer is null)\n            {\n                throw new ArgumentNullException(nameof(buffer));\n            }\n\n            if (offset < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(offset), \"Offset must be non-negative.\");\n            }\n\n            if ((uint)count > buffer.Length - offset)\n            {\n                throw new ArgumentOutOfRangeException(nameof(count), \"Count must be non-negative and count must not exceed buffer.Length - offset.\");\n            }\n        }\n\n        // No argument checking is done here. It is up to the caller.\n        private static int ReadAtLeastCore(Stream stream, byte[] buffer, int offset, int minimumBytes, bool throwOnEndOfStream)\n        {\n            Debug.Assert(minimumBytes <= buffer.Length);\n            int count = minimumBytes;\n            int totalRead = 0;\n            while (totalRead < minimumBytes)\n            {\n                int read = stream.Read(buffer, offset, count);\n                offset += read;\n                count -= read;\n                if (read == 0)\n                {\n                    if (throwOnEndOfStream)\n                    {","sourceCodeStart":188,"sourceCodeEnd":224,"githubUrl":"https://github.com/dotnet/machinelearning/blob/7b76e69cf964daeca3f1377af6bc5543284d56c6/src/Microsoft.ML.Data/Utilities/StreamUtils.cs#L188-L224","documentation":"ValidateBufferArguments requires count to be non-negative and not exceed buffer.Length - offset, i.e. the requested read region must fit within the buffer. Violating either condition throws ArgumentOutOfRangeException for 'count'.","triggerScenarios":"Calling ReadExactly with count < 0, or with offset + count > buffer.Length, so the read would write past the end of the supplied buffer.","commonSituations":"Requesting more bytes than the buffer holds (hardcoded sizes vs actual allocation); forgetting that offset consumes buffer space; off-by-one errors when buffer length was sized for count-1.","solutions":["Ensure buffer.Length >= offset + count before the call, or allocate the buffer with enough room","Validate/normalize count against the remaining buffer space","Increase buffer allocation size to account for the offset"],"exampleFix":"// before\nvar buffer = new byte[100];\nStreamUtils.ReadExactly(stream, buffer, 50, 100); // 50+100 > 100\n// after\nStreamUtils.ReadExactly(stream, buffer, 50, Math.Min(count, buffer.Length - 50));","handlingStrategy":"validation","validationCode":"if (count < 0 || offset + count > buffer.Length)\n    throw new ArgumentException(\"count must satisfy 0 <= count <= buffer.Length - offset\");","typeGuard":null,"tryCatchPattern":"try { StreamUtils.ReadExactly(stream, buffer, offset, count); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"count\")\n{ /* resize buffer or clamp count */ }","preventionTips":["Size buffers to at least offset + requestedCount","Compute count from buffer.Length - offset, not from hardcoded values","Centralize buffer slicing so the invariant is checked in one place"],"tags":["argument-validation","out-of-range","stream"],"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-23T08:17:48.524Z"}