{"record":{"id":"c522fb7a2f5b7a07","repo":"dotnet/wpf","slug":"sr-arg-arrayplusofftoosmall","errorCode":null,"errorMessage":"SR.Arg_ArrayPlusOffTooSmall","messagePattern":"SR\\.Arg_ArrayPlusOffTooSmall","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs","lineNumber":156,"sourceCode":"\n            if (array.Rank != 1) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_RankMultiDimNotSupported);\n                throw new ArgumentException(SR.Arg_RankMultiDimNotSupported);\n            }\n\n            if( array.GetLowerBound(0) != 0 ) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_NonZeroLowerBound);\n                throw new ArgumentException(SR.Arg_NonZeroLowerBound);\n            }\n\n            if (index < 0) {\n                //ThrowHelper.ThrowArgumentOutOfRangeException(ExceptionArgument.arrayIndex, ExceptionResource.ArgumentOutOfRange_NeedNonNegNum);\n                throw new ArgumentOutOfRangeException(nameof(index), SR.ArgumentOutOfRange_NeedNonNegNum);\n            }\n\n            if (array.Length - index < Count) {\n                //ThrowHelper.ThrowArgumentException(ExceptionResource.Arg_ArrayPlusOffTooSmall);\n                throw new ArgumentException(SR.Arg_ArrayPlusOffTooSmall);\n            }\n\n            IList<T> dlist = CreateDereferencedList();\n            if (array is T[] items)\n            {\n                dlist.CopyTo(items, index);\n            }\n            else\n            {\n                //\n                // Catch the obvious case assignment will fail.\n                // We can found all possible problems by doing the check though.\n                // For example, if the element type of the Array is derived from T,\n                // we can't figure out if we can successfully copy the element beforehand.\n                //\n                Type targetType = array.GetType().GetElementType();\n                Type sourceType = typeof(T);\n                if (!(targetType.IsAssignableFrom(sourceType) || sourceType.IsAssignableFrom(targetType)))","sourceCodeStart":138,"sourceCodeEnd":174,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Collections/ObjectModel/WeakReadOnlyCollection.cs#L138-L174","documentation":"CopyTo validates that the destination has room: it throws ArgumentException with SR.Arg_ArrayPlusOffTooSmall when array.Length - index < Count. The array is big enough in total perhaps, but not enough space remains from the given index to hold every element.","triggerScenarios":"Calling CopyTo(array, index) where count of elements exceeds array.Length - index, e.g., copying a 5-element collection into an array of length 4, or at index 3 of a length-6 array.","commonSituations":"Reusing a shared buffer sized for earlier smaller data; off-by-one when reserving space for a terminator element.","solutions":["Size the array to at least index + collection.Count before copying","Copy into a newly allocated array of exact Count size","Check array.Length - index >= collection.Count before calling"],"exampleFix":"// before\nvar target = new WeakReference[collection.Count - 1];\ncollection.CopyTo(target, 0);\n// after\nvar target = new WeakReference[collection.Count];\ncollection.CopyTo(target, 0);","handlingStrategy":"validation","validationCode":"if (array.Length - index < collection.Count) throw new ArgumentException(\"Destination array too small from the given index.\", nameof(array));","typeGuard":"bool HasRoom(Array a, int index, int count) => a.Length - index >= count;","tryCatchPattern":"try { ((ICollection)collection).CopyTo(array, index); } catch (ArgumentException) { array = new T[collection.Count]; ((ICollection)collection).CopyTo(array, 0); }","preventionTips":["Size buffers as index + Count","Re-allocate buffers when the collection grows","Call CopyTo(T[], 0) into fresh arrays to sidestep sizing math"],"tags":["wpf","copyto","buffer-size"],"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"}