Fix failing testStartsWith: startsWith/endsWith will dereference invalid string iterator when the prefix/suffix is longer than the actual length of the string

This commit is contained in:
CREMARENCO Cosmin 2016-09-06 17:18:25 +02:00
parent bb456d342a
commit c0906ab5aa

View File

@ -627,7 +627,7 @@ template <class S>
bool startsWith(const S& str, const S& prefix)
/// Tests whether the string starts with the given prefix.
{
return equal(prefix.begin(), prefix.end(), str.begin());
return str.size() >= prefix.size() && equal(prefix.begin(), prefix.end(), str.begin());
}
@ -635,7 +635,7 @@ template <class S>
bool endsWith(const S& str, const S& suffix)
/// Tests whether the string ends with the given suffix.
{
return equal(suffix.rbegin(), suffix.rend(), str.rbegin());
return str.size() >= suffix.size() && equal(suffix.rbegin(), suffix.rend(), str.rbegin());
}