{"record":{"id":"40d8e71215a35116","repo":"dotnet/wpf","slug":"sr-tablecollectionoutofrangeneednonnegnum","errorCode":null,"errorMessage":"SR.TableCollectionOutOfRangeNeedNonNegNum","messagePattern":"SR\\.TableCollectionOutOfRangeNeedNonNegNum","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs","lineNumber":73,"sourceCode":"        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        /// <see cref=\"ICollection.CopyTo\"/>\n        /// </exception>\n        /// <exception cref=\"ArgumentOutOfRangeException\">\n        /// <see cref=\"ICollection.CopyTo\"/>\n        /// </exception>\n        /// <param name=\"array\"><see cref=\"ICollection.CopyTo\"/></param>\n        /// <param name=\"index\"><see cref=\"ICollection.CopyTo\"/></param>\n        public void CopyTo(Array array, int index)\n        {\n            ArgumentNullException.ThrowIfNull(array);\n            if (array.Rank != 1)\n            {\n                throw new ArgumentException(SR.TableCollectionRankMultiDimNotSupported);\n            }\n            if (index < 0)\n            {\n                throw new ArgumentOutOfRangeException(nameof(index), SR.TableCollectionOutOfRangeNeedNonNegNum);\n            }\n            if (array.Length - index < Size)\n            {\n                throw new ArgumentException(SR.TableCollectionInvalidOffLen);\n            }\n\n            Array.Copy(Items, 0, array, index, Size);\n        }\n\n        /// <summary>\n        /// Strongly typed version of ICollection.CopyTo.\n        /// </summary>\n        /// <exception cref=\"ArgumentNullException\">\n        /// <see cref=\"ICollection.CopyTo\"/>\n        /// </exception>\n        /// <exception cref=\"ArgumentException\">\n        /// <see cref=\"ICollection.CopyTo\"/>\n        /// </exception>","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/ContentElementCollection.cs#L55-L91","documentation":"ContentElementCollection.CopyTo throws ArgumentOutOfRangeException when the destination index is negative. Non-negative start index is a requirement of the ICollection.CopyTo contract implemented here; the check runs after array null/rank validation and before the capacity check.","triggerScenarios":"Calling CopyTo with index < 0, e.g. collection.CopyTo(array, -1), or an index computed from an expression that evaluated negative (bad offset arithmetic, default-initialized variable, off-by-one decrement).","commonSituations":"Computing the insert position from a found index of -1 (e.g. IndexOf miss used directly as the copy index); copying into a shared buffer with a rolling offset that underflows.","solutions":["Clamp or validate the index with Math.Max(0, index) before calling CopyTo","Ensure the source of the index (e.g. IndexOf result) is checked for -1 before reuse","Use index 0 when copying to the start of a fresh array"],"exampleFix":"// before\nint idx = list.IndexOf(item); // may be -1\ncollection.CopyTo(array, idx);\n// after\nint idx = list.IndexOf(item);\nif (idx >= 0) collection.CopyTo(array, idx);","handlingStrategy":"validation","validationCode":"if (index < 0) throw new ArgumentOutOfRangeException(nameof(index), index, \"Index must be non-negative\");\ncollection.CopyTo(array, index);","typeGuard":null,"tryCatchPattern":"try { collection.CopyTo(array, index); }\ncatch (ArgumentOutOfRangeException ex)\n{\n    logger.LogWarning(\"CopyTo rejected negative index {Index}\", ex.ActualValue);\n    collection.CopyTo(array, Math.Max(0, index));\n}","preventionTips":["Validate computed offsets before use, especially results of IndexOf (which returns -1 on miss)","Clamp indices with Math.Max(0, index)","Prefer index 0 for fresh arrays"],"tags":["wpf","argumentoutofrangeexception","copyto","index"],"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"}