{"record":{"id":"e91b13dd7233624e","repo":"dotnet/wpf","slug":"sr-argument-invalidofflen","errorCode":null,"errorMessage":"SR.Argument_InvalidOffLen","messagePattern":"SR\\.Argument_InvalidOffLen","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/RBTree.cs","lineNumber":570,"sourceCode":"\n        public void Clear()\n        {\n            LeftChild = null;\n            LeftSize = 0;\n        }\n\n        public bool Contains(T item)\n        {\n            RBFinger<T> finger = Find(item, Comparison);\n            return finger.Found;\n        }\n\n        public void CopyTo(T[] array, int arrayIndex)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n            ArgumentOutOfRangeException.ThrowIfNegative(arrayIndex);\n            if (arrayIndex + Count > array.Length)\n                throw new ArgumentException(SR.Argument_InvalidOffLen);\n\n            foreach (T item in this)\n            {\n                array[arrayIndex] = item;\n                ++arrayIndex;\n            }\n        }\n\n        public int Count\n        {\n            get { return LeftSize; }\n        }\n\n        public bool IsReadOnly\n        {\n            get { return false; }\n        }\n","sourceCodeStart":552,"sourceCodeEnd":588,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/Data/RBTree.cs#L552-L588","documentation":"RBTree<T>.CopyTo validates the destination array arguments via ArgumentNullException.ThrowIfNull, ThrowIfNegative(arrayIndex), and throws ArgumentException(SR.Argument_InvalidOffLen) when arrayIndex + Count exceeds array.Length — i.e. the array is too small to hold all tree elements at the given offset.","triggerScenarios":"Calling rbTree.CopyTo(array, index) where array.Length - index < tree.Count (e.g. copying an empty-initialized array, or using index > 0 without sizing the array accordingly).","commonSituations":"Pre-sizing arrays from a stale Count, copying into arrays reused across refreshes, off-by-one when index is non-zero.","solutions":["Allocate the array with size tree.Count + arrayIndex before copying","Pass arrayIndex 0 when using a fresh array of length Count","Copy to a List<T> via constructor if the size is unknown"],"exampleFix":"// before\nvar arr = new string[count]; // count from an earlier snapshot\ntree.CopyTo(arr, 1); // too small\n// after\nvar arr = new string[tree.Count];\ntree.CopyTo(arr, 0);","handlingStrategy":"validation","validationCode":"if (array == null) throw new ArgumentNullException(nameof(array));\nif (arrayIndex < 0 || tree.Count > array.Length - arrayIndex)\n    throw new ArgumentException(\"Destination array too small for given arrayIndex\", nameof(array));","typeGuard":null,"tryCatchPattern":"try { tree.CopyTo(arr, idx); }\ncatch (ArgumentException) { arr = new T[tree.Count]; tree.CopyTo(arr, 0); }","preventionTips":["Size arrays as Count + offset before CopyTo","Copy to List<T> when capacity is unknown","Re-check Count after collection refreshes"],"tags":["wpf","argument","array","copyto"],"backgroundTag":"argument-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}