{"record":{"id":"5c6a4cfde4a97df4","repo":"dotnet/wpf","slug":"sr-format-sr-collectionnumberofelementsshouldbeequalto-5c6a4c","errorCode":null,"errorMessage":"SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, CodepointCount + 1)","messagePattern":"SR\\.Format\\(SR\\.CollectionNumberOfElementsShouldBeEqualTo, CodepointCount \\+ 1\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs","lineNumber":411,"sourceCode":"                                throw new ArgumentException(SR.ClusterMapFirstEntryMustBeZero, nameof(clusterMap));\n                            }\n                        }\n                        else\n                        {\n                            throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, characters.Count), nameof(clusterMap));\n                        }\n                    }\n                    else\n                    {\n                        if (GlyphCount != characters.Count)\n                            throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, GlyphCount), nameof(clusterMap));\n                    }\n                }\n\n                if (caretStops != null && caretStops.Count != 0)\n                {\n                    if (caretStops.Count != CodepointCount + 1)\n                        throw new ArgumentException(SR.Format(SR.CollectionNumberOfElementsShouldBeEqualTo, CodepointCount + 1), nameof(caretStops));\n                }\n\n                if (isSideways && (bidiLevel & 1) != 0)\n                    throw new ArgumentException(SR.SidewaysRTLTextIsNotSupported);\n\n                // NOTE:  In previous versions this function would estimate the size\n                // of this glyph run's bitmaps and compare it against the theoretical\n                // maximum size allowed before rendering falls back to using geometry.\n                // This was done in order to produce a managed exception where we might\n                // hit overflow or memory allocation issues in native code.\n                // We no longer own the code the produces these bitmaps so we can't reliably\n                // avoid the issue here any longer.\n            }\n            else\n            {\n                ArgumentOutOfRangeException.ThrowIfEqual(renderingEmSize, double.NaN);\n                ArgumentOutOfRangeException.ThrowIfNegative(renderingEmSize);\n                ArgumentNullException.ThrowIfNull(glyphTypeface);","sourceCodeStart":393,"sourceCodeEnd":429,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/GlyphRun.cs#L393-L429","documentation":"When caretStops is supplied and non-empty, GlyphRun.Initialize requires caretStops.Count == CodepointCount + 1 (one stop per codepoint plus a trailing stop). Any other count is rejected with ArgumentException on the caretStops parameter.","triggerScenarios":"Calling the GlyphRun constructor, TryCreate, or EndInit with a caretStops collection whose length is not (number of codepoints + 1) — e.g. sizing it to characters.Count or glyphIndices.Count.","commonSituations":"Implementing custom caret navigation / hit-testing and miscomputing the caret stop array length, especially with surrogate pairs where codepoints differ from chars.","solutions":["Size caretStops to CodepointCount + 1 (count surrogate pairs as one codepoint)","Pass null (or empty) caretStops if default caret behavior is acceptable","Compute codepoint count with StringInfo.GetTextElementEnumerator / char.IsSurrogatePair logic"],"exampleFix":"// before\ncaretStops = new bool[chars.Length];\n// after\ncaretStops = new bool[CodepointCount + 1]; // +1 for trailing stop","handlingStrategy":"validation","validationCode":"if (caretStops != null && caretStops.Count != 0 && caretStops.Count != CodepointCount(characters) + 1)\n    throw new ArgumentException(\"caretStops must have CodepointCount+1 entries\");","typeGuard":"static bool CaretStopsValid(IList<bool> stops, int codepointCount) => stops == null || stops.Count == 0 || stops.Count == codepointCount + 1;","tryCatchPattern":"try { var run = new GlyphRun(...); }\ncatch (ArgumentException ex) when (ex.ParamName == \"caretStops\") { /* fix size or pass null */ }","preventionTips":["Size caret stops as codepoints+1, counting surrogate pairs once","Pass null when default caret behavior suffices","Unit-test caret stop sizing on astral-plane characters"],"tags":["wpf","glyphrun","caret-stops","argument-validation"],"backgroundTag":"invalid-argument-value","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"}