Fix out-of-band access issue in iequals function

This commit is contained in:
Mohammad Nejati
2024-12-15 08:29:50 +00:00
committed by Mohammad Nejati
parent cc58816ffc
commit 8b379d4fb1

View File

@ -23,29 +23,26 @@ iequals(
beast::string_view lhs, beast::string_view lhs,
beast::string_view rhs) beast::string_view rhs)
{ {
auto n = lhs.size(); if(lhs.size() != rhs.size())
if(rhs.size() != n)
return false; return false;
auto n = lhs.size();
auto p1 = lhs.data(); auto p1 = lhs.data();
auto p2 = rhs.data(); auto p2 = rhs.data();
char a, b;
// fast loop // fast loop
while(n--) while(n--)
{ {
a = *p1++; if(*p1++ != *p2++)
b = *p2++;
if(a != b)
goto slow; goto slow;
} }
return true; return true;
slow: slow:
--p1;
--p2;
do do
{ {
if( detail::ascii_tolower(a) != if( detail::ascii_tolower(*p1++) !=
detail::ascii_tolower(b)) detail::ascii_tolower(*p2++))
return false; return false;
a = *p1++;
b = *p2++;
} }
while(n--); while(n--);
return true; return true;