Repository: microsoft/terminal
File: src/cascadia/TerminalSettingsEditor/Appearances.cpp (C++)

Code before:
                    uint32_t localeIndex;
                    BOOL localeExists;
                    wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
                    const auto localeToTry = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) ? localeName : L"en-US";

                    axesVector.resize(axesCount);
                    fontFace5->GetFontAxisValues(axesVector.data(), axesCount);
                    for (uint32_t i = 0; i < axesCount; ++i)
                    {
                        const auto tagString = _axisTagToString(axesVector[i].axisTag);

                        wil::com_ptr<IDWriteLocalizedStrings> names;
                        THROW_IF_FAILED(fontResource->GetAxisNames(i, names.put()));

                        if (!SUCCEEDED(names->FindLocaleName(localeToTry, &localeIndex, &localeExists)) || !localeExists)
                        {
                            // default to the first locale in the list
                            localeIndex = 0;
                        }

                        uint32_t length;
                        if (SUCCEEDED(names->GetStringLength(localeIndex, &length)))
                        {
                            // it is reasonable to assume that the name length is not going to exceed 512 chars
                            wchar_t name[512];
                            if (SUCCEEDED(names->GetString(localeIndex, name, length + 1)))
                            {
                                _fontAxesTagsAndNames.Insert(tagString, winrt::hstring{ name });
                                continue;
                            }
                        }
                        // if there was no name found, it means the font does not actually support this axis
                        // don't insert anything into the vector in this case
                    }
                }
            }
        }
        return _fontAxesTagsAndNames;
    }

    winrt::hstring Font::_axisTagToString(DWRITE_FONT_AXIS_TAG tag)
    {
        std::wstring result;
        for (int i = 0; i < 4; ++i)
        {
            result.push_back((tag >> (i * 8)) & 0xFF);
        }
        return winrt::hstring{ result };
    }

    AxisKeyValuePair::AxisKeyValuePair(winrt::hstring axisKey, float axisValue, const Windows::Foundation::Collections::IMap<winrt::hstring, float>& baseMap, const Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring>& tagToNameMap) :

Review comment (bug):
This is not safe: You don't know whether `length` is actually <512. Just drop the `GetStringLength` call and pass `ARRAYSIZE(name)` as the length parameter. Alternatively, use this:

```cpp
static winrt::hstring getLocalizedStringByIndex(IDWriteLocalizedStrings* strings, UINT32 index)
{
    UINT32 length = 0;
    THROW_IF_FAILED(strings->GetStringLength(index, &length));

    winrt::impl::hstring_builder builder{ length };
    THROW_IF_FAILED(strings->GetString(index, builder.data(), length + 1));

    return builder.to_hstring();
}
```

Code after:
                    uint32_t localeIndex;
                    BOOL localeExists;
                    wchar_t localeName[LOCALE_NAME_MAX_LENGTH];
                    const auto localeToTry = GetUserDefaultLocaleName(localeName, LOCALE_NAME_MAX_LENGTH) ? localeName : L"en-US";

                    std::unordered_map<winrt::hstring, winrt::hstring> fontAxesTagsAndNames;
                    for (uint32_t i = 0; i < axesCount; ++i)
                    {
                        wil::com_ptr<IDWriteLocalizedStrings> names;
                        THROW_IF_FAILED(fontResource->GetAxisNames(i, names.put()));

                        if (!SUCCEEDED(names->FindLocaleName(localeToTry, &localeIndex, &localeExists)) || !localeExists)
                        {
                            // default to the first locale in the list
                            localeIndex = 0;
                        }

                        UINT32 length = 0;
                        if (SUCCEEDED(names->GetStringLength(localeIndex, &length)))
                        {
                            winrt::impl::hstring_builder builder{ length };
                            if (SUCCEEDED(names->GetString(localeIndex, builder.data(), length + 1)))
                            {
                                fontAxesTagsAndNames.insert(std::pair<winrt::hstring, winrt::hstring>(_axisTagToString(axesVector[i].axisTag), builder.to_hstring()));
                                continue;
                            }
                        }
                        // if there was no name found, it means the font does not actually support this axis
                        // don't insert anything into the vector in this case
                    }
                    _fontAxesTagsAndNames = winrt::single_threaded_map(std::move(fontAxesTagsAndNames));
                }
            }
        }
        return _fontAxesTagsAndNames;
    }

    winrt::hstring Font::_axisTagToString(DWRITE_FONT_AXIS_TAG tag)
    {
        std::wstring result;
        for (int i = 0; i < 4; ++i)
        {
            result.push_back((tag >> (i * 8)) & 0xFF);
        }
        return winrt::hstring{ result };
    }

    AxisKeyValuePair::AxisKeyValuePair(winrt::hstring axisKey, float axisValue, const Windows::Foundation::Collections::IMap<winrt::hstring, float>& baseMap, const Windows::Foundation::Collections::IMap<winrt::hstring, winrt::hstring>& tagToNameMap) :
        _AxisKey{ axisKey },
        _AxisValue{ axisValue },
        _baseMap{ baseMap },