Avoid UB in Session::setHash()

Ubsan says that signed integer overflow is happening here:

    #0 0xec344a in Session::setHash(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) common/Session.cpp:267:17
    #1 0x918b54 in DocumentBroker::load(std::shared_ptr<ClientSession> const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&) wsd/DocumentBroker.cpp:682:18
...
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior common/Session.cpp:267:17 in

(While running unit_wopi_watermark.)

Change-Id: Idcb1692f6d5d0df8be35dc557d2d72a345acbcf4
This commit is contained in:
Miklos Vajna 2019-11-22 14:11:00 +01:00
parent c39645bed5
commit 204cff5c19

View file

@ -257,8 +257,8 @@ void Session::getIOStats(uint64_t &sent, uint64_t &recv)
void Session::setHash(const std::string& text)
{
int hash = 0x811C9DC5;
int prime = 0x1000193;
unsigned int hash = 0x811C9DC5;
unsigned int prime = 0x1000193;
if (!text.empty())
{
@ -268,7 +268,7 @@ void Session::setHash(const std::string& text)
hash *= prime;
}
}
_hash = abs(hash);
_hash = abs(static_cast<int>(hash));
}