drivenum.cpp: fix undefined behavior in find_approximate_matches (#12441)

* If `it` points to the last element of `penalty`, the `resize` call invalidates it, and the subsequent call to `emplace` is undefined. This causes a crash in MSVC debug mode.
* Fix it by resizing _after_ emplacing.
This commit is contained in:
Roman Donchenko 2024-06-06 04:27:58 +03:00 committed by GitHub
parent ccad8c4c2f
commit af79954d15
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -260,7 +260,7 @@ void driver_enumerator::find_approximate_matches(std::string const &string, std:
{
// allocate memory to track the penalty value
std::vector<std::pair<double, int> > penalty;
penalty.reserve(count);
penalty.reserve(count + 1);
std::u32string const search(ustr_from_utf8(normalize_unicode(string, unicode_normalization_form::D, true)));
std::string composed;
std::u32string candidate;
@ -303,9 +303,9 @@ void driver_enumerator::find_approximate_matches(std::string const &string, std:
auto const it(std::upper_bound(penalty.begin(), penalty.end(), std::make_pair(curpenalty, index)));
if (penalty.end() != it)
{
if (penalty.size() >= count)
penalty.resize(count - 1);
penalty.emplace(it, curpenalty, index);
if (penalty.size() > count)
penalty.pop_back();
}
else if (penalty.size() < count)
{