{"record":{"id":"5863ed8dc01da22c","repo":"dotnet/wpf","slug":"sr-format-sr-collection-copyto-5863ed","errorCode":null,"errorMessage":"SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\")","messagePattern":"SR\\.Format\\(SR\\.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs","lineNumber":80,"sourceCode":"        /// Determines whether the FontFamily contains the specified FontFamilyMap.\n        /// </summary>\n        public bool Contains(FontFamilyMap item)\n        {\n            return FindItem(item) >= 0;\n        }\n\n        /// <summary>\n        /// Copies the contents of the list to the specified array.\n        /// </summary>\n        public void CopyTo(FontFamilyMap[] array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (index >= array.Length)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"));\n\n            if (_count > array.Length - index)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_NumberOfElementsExceedsArrayLength, index, \"array\"));\n\n            if (_count != 0)\n                Array.Copy(_items, 0, array, index, _count);\n        }\n\n        void SC.ICollection.CopyTo(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n\n            if (array.Rank != 1)\n                throw new ArgumentException(SR.Collection_CopyTo_ArrayCannotBeMultidimensional);\n\n            Type elementType = array.GetType().GetElementType();\n            if (!elementType.IsAssignableFrom(typeof(FamilyTypeface)))\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(FamilyTypeface), elementType));\n\n            if (index >= array.Length)\n                throw new ArgumentException(SR.Format(SR.Collection_CopyTo_IndexGreaterThanOrEqualToArrayLength, \"index\", \"array\"));","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FamilyMapCollection.cs#L62-L98","documentation":"FamilyMapCollection.CopyTo throws ArgumentException when the collection has more elements than the destination array can hold from the given index onward (_count > array.Length - index). This prevents partial/overflow copies per the ICollection.CopyTo contract.","triggerScenarios":"Calling CopyTo with an array shorter than the collection (offset by index), e.g. an array allocated for an older, smaller collection or a hardcoded size.","commonSituations":"Caching a FontFamilyMap[] sized once and reusing it after the composite font gains more family maps, or allocating exactly Count but passing a nonzero index.","solutions":["Allocate the destination array with length >= index + collection.Count (or exactly collection.Count with index 0).","Re-query collection.Count immediately before CopyTo instead of caching sizes.","Use ToArray() when a correctly sized buffer is all that is needed."],"exampleFix":"// before\nvar buffer = new FontFamilyMap[4];\nfamilyMaps.CopyTo(buffer, 0);\n// after\nvar buffer = new FontFamilyMap[familyMaps.Count];\nfamilyMaps.CopyTo(buffer, 0);","handlingStrategy":"validation","validationCode":"if (array.Length - index < col.Count) array = new FontFamilyMap[col.Count];","typeGuard":null,"tryCatchPattern":"try { col.CopyTo(array, index); }\ncatch (ArgumentException) { array = new FontFamilyMap[col.Count]; col.CopyTo(array, 0); }","preventionTips":["Re-query Count right before copying","Prefer ToArray() over manual buffers"],"tags":["wpf","fonts","collections","buffer-size"],"backgroundTag":"index-out-of-range","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}