{"record":{"id":"6ae439d08896baa9","repo":"dotnet/wpf","slug":"sr-format-sr-codepointoutofrange-key","errorCode":null,"errorMessage":"SR.Format(SR.CodePointOutOfRange, key)","messagePattern":"SR\\.Format\\(SR\\.CodePointOutOfRange, key\\)","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":355,"sourceCode":"                page = _pageTable[i];\n                if (page == null)\n                {\n                    _pageTable[i] = page = new CharacterMetrics[PageSize];\n                }\n            }\n            else\n            {\n                _pageTable = new CharacterMetrics[PageCount][];\n                _pageTable[i] = page = new CharacterMetrics[PageSize];\n            }\n\n            return page;\n        }\n\n        private void SetValue(int key, CharacterMetrics value, bool failIfExists)\n        {\n            if (key < 0 || key > LastDeviceFontCharacterCode)\n                throw new ArgumentOutOfRangeException(SR.Format(SR.CodePointOutOfRange, key));\n\n            ArgumentNullException.ThrowIfNull(value);\n\n            CharacterMetrics[] page = GetPageFromUnicodeScalar(key);\n            int i = key & PageMask;\n\n            if (failIfExists && page[i] != null)\n                throw new ArgumentException(SR.Format(SR.CollectionDuplicateKey, key));\n\n            page[i] = value;\n            _count = 0;\n        }\n\n        internal CharacterMetrics GetValue(int key)\n        {\n            CharacterMetrics metrics = null;\n\n            if (key >= 0 && key <= FontFamilyMap.LastUnicodeScalar && _pageTable != null)","sourceCodeStart":337,"sourceCodeEnd":373,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L337-L373","documentation":"CharacterMetricsDictionary keys are Unicode scalar code points limited to 0..LastDeviceFontCharacterCode (0xFFFF range used by the device-font glyph map). SetValue throws ArgumentOutOfRangeException when a key is negative or above this maximum.","triggerScenarios":"Calling Add(key, value) or the indexer this[int key] = value with key < 0 or key > LastDeviceFontCharacterCode (0x10FFFF-ish device-font bound; practically any negative or surrogate-plane value).","commonSituations":"Feeding full UTF-32 code points from supplementary planes (key > 0xFFFF); sign errors turning a char arithmetic result negative; using character counts or glyph IDs as keys instead of code points.","solutions":["Clamp or validate the key to the valid code-point range before calling Add/indexer.","Use (int)someChar or codePoint values in 0..0xFFFF for BMP characters.","For supplementary-plane characters, operate on their UTF-16 code units (surrogate pairs) instead."],"exampleFix":"// before\ndict[0x1F600] = metrics; // astral-plane code point\n// after\nif (codePoint >= 0 && codePoint <= char.MaxValue)\n    dict[codePoint] = metrics;","handlingStrategy":"validation","validationCode":"if (key < 0 || key > char.MaxValue)\n    throw new ArgumentOutOfRangeException(nameof(key), key, \"Key must be a valid BMP code point.\");\ndict[key] = value;","typeGuard":null,"tryCatchPattern":"try { dict.Add(key, value); }\ncatch (ArgumentOutOfRangeException ex) { /* log invalid code point and skip */ }","preventionTips":["Validate keys are non-negative and within the device-font code-point range.","Don't feed astral-plane (surrogate) code points as single keys.","Distinguish glyph IDs from Unicode code points."],"tags":["wpf","unicode","argumentoutofrange","dictionary"],"backgroundTag":"value-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"}