Fixed an out-of-bounds access.

This commit is contained in:
Maxim Pimenov 2016-04-19 15:58:23 +03:00
parent 9367412623
commit 578fc4d320
2 changed files with 8 additions and 1 deletions

View file

@ -582,6 +582,11 @@ UNIT_TEST(AlmostEqual)
TEST(!AlmostEqual("MKAD, 600 km", "MKAD, 599 km", 2), ());
TEST(!AlmostEqual("MKAD, 45-y kilometre", "MKAD, 46", 2), ());
TEST(!AlmostEqual("ул. Героев Панфиловцев", "ул. Планерная", 2), ());
string small(10, '\0');
string large(1000, '\0');
TEST(AlmostEqual(small, large, large.length()), ());
TEST(AlmostEqual(large, small, large.length()), ());
}
UNIT_TEST(EditDistance)

View file

@ -1,6 +1,7 @@
#include "base/assert.hpp"
#include "base/string_utils.hpp"
#include "std/algorithm.hpp"
#include "std/cmath.hpp"
#include "std/iomanip.hpp"
#include "std/iterator.hpp"
@ -310,7 +311,8 @@ bool AlmostEqual(string const & str1, string const & str2, size_t mismatchedCoun
for (size_t i = 0; i <= mismatchedCount; ++i)
{
mis = mismatch(mis.first, str1End, mis.second);
auto const end = mis.first + min(distance(mis.first, str1End), distance(mis.second, str2End));
mis = mismatch(mis.first, end, mis.second);
if (mis.first == str1End && mis.second == str2End)
return true;
if (mis.first != str1End)