dotnet/wpf · error · ArgumentOutOfRangeException

SR.Format(SR.CodePointOutOfRange, key)

Error message

SR.Format(SR.CodePointOutOfRange, key)

What it means

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.

Solutions

  1. Clamp or validate the key to the valid code-point range before calling Add/indexer.
  2. Use (int)someChar or codePoint values in 0..0xFFFF for BMP characters.
  3. For supplementary-plane characters, operate on their UTF-16 code units (surrogate pairs) instead.

Example fix

// before
dict[0x1F600] = metrics; // astral-plane code point
// after
if (codePoint >= 0 && codePoint <= char.MaxValue)
    dict[codePoint] = metrics;
Defensive patterns

Strategy: validation

Validate before calling

if (key < 0 || key > char.MaxValue)
    throw new ArgumentOutOfRangeException(nameof(key), key, "Key must be a valid BMP code point.");
dict[key] = value;

Try / catch

try { dict.Add(key, value); }
catch (ArgumentOutOfRangeException ex) { /* log invalid code point and skip */ }

Prevention

When it happens

Trigger: 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).

Common situations: 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.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/6ae439d08896baa9. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/CharacterMetricsDictionary.cs:355

                page = _pageTable[i];
                if (page == null)
                {
                    _pageTable[i] = page = new CharacterMetrics[PageSize];
                }
            }
            else
            {
                _pageTable = new CharacterMetrics[PageCount][];
                _pageTable[i] = page = new CharacterMetrics[PageSize];
            }

            return page;
        }

        private void SetValue(int key, CharacterMetrics value, bool failIfExists)
        {
            if (key < 0 || key > LastDeviceFontCharacterCode)
                throw new ArgumentOutOfRangeException(SR.Format(SR.CodePointOutOfRange, key));

            ArgumentNullException.ThrowIfNull(value);

            CharacterMetrics[] page = GetPageFromUnicodeScalar(key);
            int i = key & PageMask;

            if (failIfExists && page[i] != null)
                throw new ArgumentException(SR.Format(SR.CollectionDuplicateKey, key));

            page[i] = value;
            _count = 0;
        }

        internal CharacterMetrics GetValue(int key)
        {
            CharacterMetrics metrics = null;

            if (key >= 0 && key <= FontFamilyMap.LastUnicodeScalar && _pageTable != null)

View on GitHub (pinned to 81131a70a4)