{"record":{"id":"695327355437f542","repo":"dotnet/reactive","slug":"specified-argument-was-out-of-range-of-valid-values-695327","errorCode":null,"errorMessage":"Specified argument was out of range of valid values. (Parameter 'bufferSize')","messagePattern":"Specified argument was out of range of valid values\\. \\(Parameter 'bufferSize'\\)","errorType":"validation","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs","lineNumber":922,"sourceCode":"        /// <exception cref=\"ArgumentNullException\"><paramref name=\"source\"/> or <paramref name=\"selector\"/> or <paramref name=\"scheduler\"/> is null.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"bufferSize\"/> is less than zero.</exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\"><paramref name=\"window\"/> is less than TimeSpan.Zero.</exception>\n        /// <seealso cref=\"ReplaySubject{T}\"/>\n        public static IObservable<TResult> Replay<TSource, TResult>(this IObservable<TSource> source, Func<IObservable<TSource>, IObservable<TResult>> selector, int bufferSize, TimeSpan window, IScheduler scheduler)\n        {\n            if (source == null)\n            {\n                throw new ArgumentNullException(nameof(source));\n            }\n\n            if (selector == null)\n            {\n                throw new ArgumentNullException(nameof(selector));\n            }\n\n            if (bufferSize < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(bufferSize));\n            }\n\n            if (window < TimeSpan.Zero)\n            {\n                throw new ArgumentOutOfRangeException(nameof(window));\n            }\n\n            if (scheduler == null)\n            {\n                throw new ArgumentNullException(nameof(scheduler));\n            }\n\n            return s_impl.Replay(source, selector, bufferSize, window, scheduler);\n        }\n\n        #endregion\n    }\n}","sourceCodeStart":904,"sourceCodeEnd":940,"githubUrl":"https://github.com/dotnet/reactive/blob/94b5d5ab912789f5abe9a72138a25bbd716fe59c/Rx.NET/Source/src/System.Reactive/Linq/Observable.Binding.cs#L904-L940","documentation":"In the selector overload Replay(source, selector, bufferSize, window, scheduler), bufferSize must be >= 0 and is validated eagerly; a negative value throws ArgumentOutOfRangeException (parameter 'bufferSize').","triggerScenarios":"Calling source.Replay(selector, negativeN, window, scheduler) with a negative replay count, e.g. from underflowing arithmetic or an unparsed/negative config value.","commonSituations":"Buffer capacity computed as a difference that went negative; user or config supplied replay count without validation; copying a call site and forgetting to update the size constant.","solutions":["Clamp with Math.Max(0, bufferSize) before the call","Correct the arithmetic/config producing the negative number","Add a startup range validation for configured sizes"],"exampleFix":"// before\nvar replayed = source.Replay(sel, size - taken /* -3 */, window, scheduler);\n// after\nvar n = Math.Max(0, size - taken);\nvar replayed = source.Replay(sel, n, window, scheduler);","handlingStrategy":"validation","validationCode":"if (bufferSize < 0) throw new ArgumentOutOfRangeException(nameof(bufferSize));","typeGuard":"static bool IsValidBufferSize(int bufferSize) => bufferSize >= 0;","tryCatchPattern":"try { var r = src.Replay(sel, bufferSize, window, scheduler); }\ncatch (ArgumentOutOfRangeException ex) when (ex.ParamName == \"bufferSize\") { bufferSize = 0; /* retry */ }","preventionTips":["Clamp arithmetic results with Math.Max(0, n)","Range-check configured replay sizes at startup","Never pass unchecked differences as buffer counts"],"tags":["csharp","rx","argument-range"],"backgroundTag":"argument-out-of-range","analyzedSha":"94b5d5ab912789f5abe9a72138a25bbd716fe59c","analyzedAt":"2026-09-15T02:26:24.759Z","contentChangedAt":"2026-09-15T02:26:24.759Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}