{"record":{"id":"1336be4fd7b0e86a","repo":"dotnet/wpf","slug":"sr-format-sr-codepointoutofrange-value","errorCode":null,"errorMessage":"SR.Format(SR.CodePointOutOfRange, value)","messagePattern":"SR\\.Format\\(SR\\.CodePointOutOfRange, value\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs","lineNumber":504,"sourceCode":"\n            string s = key as string;\n            if (s != null)\n            {\n                int i = 0;\n                if (!FontFamilyMap.ParseHexNumber(s, ref i, out value) || i < s.Length)\n                    throw new ArgumentException(SR.Format(SR.CannotConvertStringToType, s, \"int\"), nameof(key));\n            }\n            else if (key is int)\n            {\n                value = (int)key;\n            }\n            else\n            {\n                throw new ArgumentException(SR.Format(SR.CannotConvertType, key.GetType(), \"int\"), nameof(key));\n            }\n\n            if (value < 0 || value > FontFamilyMap.LastUnicodeScalar)\n                throw new ArgumentException(SR.Format(SR.CodePointOutOfRange, value), nameof(key));\n\n            return value;\n        }\n\n        private CharacterMetrics ConvertValue(object value)\n        {\n            CharacterMetrics metrics = value as CharacterMetrics;\n            if (metrics != null)\n                return metrics;\n\n            ArgumentNullException.ThrowIfNull(value);\n\n            throw new ArgumentException(SR.Format(SR.CannotConvertType, typeof(CharacterMetrics), value.GetType()));\n        }\n\n        private struct Enumerator : SC.IDictionaryEnumerator, IEnumerator<KeyValuePair<int, CharacterMetrics>>\n        {\n            private CharacterMetricsDictionary _dictionary;","sourceCodeStart":486,"sourceCodeEnd":522,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs#L486-L522","documentation":"CharacterMetricsDictionary keys are Unicode code points and must lie in the range 0..FontFamilyMap.LastUnicodeScalar (the maximum valid UTF-16/Unicode scalar value, 0x10FFFF). ConvertKey throws this ArgumentOutOfRangeException-style ArgumentException after a key has been successfully parsed to an int but is negative or exceeds that maximum.","triggerScenarios":"Calling Add or the indexer with an int key < 0 or > 0x10FFFF (e.g. a surrogate-pair packed value, a negative sentinel, or a hash-like number), including string keys whose hex value exceeds the maximum.","commonSituations":"Encoding surrogate pairs as a single 32-bit number and using it as a key; using negative values as 'not set' sentinels; keys taken from full character codes including non-character planes above the scalar limit.","solutions":["Ensure the key is a valid Unicode scalar: 0 <= key <= 0x10FFFF, excluding surrogates if you need a real character","Split surrogate pairs into individual scalar values and add one entry per scalar","Validate the key with char.ConvertToUtf32 before inserting"],"exampleFix":"// before\n dictionary.Add(0x1F600 + 0x100000000, metrics); // out of range\n// after\n dictionary.Add(char.ConvertToUtf32(\"\\uD83D\\uDE00\", 0), metrics); // 0x1F600","handlingStrategy":"validation","validationCode":"bool IsValidCodePoint(int v) => v >= 0 && v <= 0x10FFFF && !char.IsSurrogate((char)v);","typeGuard":null,"tryCatchPattern":"try { dict.Add(codePoint, metrics); } catch (ArgumentException ex) when (ex.Message.Contains(\"code point\")) { /* clamp or split value */ }","preventionTips":["Clamp/validate code points to [0, 0x10FFFF] before adding","Split surrogate pairs into individual scalars with char.ConvertToUtf32","Never use negative sentinels as dictionary keys"],"tags":["wpf","argument","range","unicode"],"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"}