mirror of
https://github.com/yhirose/cpp-httplib
synced 2024-11-21 14:29:10 -07:00
Prevent overflow in hash function str2tag_core() (#1529)
* str2tag_core(): prevent overflow * Update httplib.h works for all sizes of unsigned int and if there exists a #define for max
This commit is contained in:
parent
5745eabe69
commit
d262033ded
1 changed files with 7 additions and 3 deletions
10
httplib.h
10
httplib.h
|
@ -2978,9 +2978,13 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) {
|
||||||
|
|
||||||
inline constexpr unsigned int str2tag_core(const char *s, size_t l,
|
inline constexpr unsigned int str2tag_core(const char *s, size_t l,
|
||||||
unsigned int h) {
|
unsigned int h) {
|
||||||
return (l == 0) ? h
|
return (l == 0)
|
||||||
: str2tag_core(s + 1, l - 1,
|
? h
|
||||||
(h * 33) ^ static_cast<unsigned char>(*s));
|
: str2tag_core(
|
||||||
|
s + 1, l - 1,
|
||||||
|
//unsets the 6 high bits of h, therefore no overflow happens
|
||||||
|
(((std::numeric_limits<unsigned int>::max)() >> 6) & h * 33) ^
|
||||||
|
static_cast<unsigned char>(*s));
|
||||||
}
|
}
|
||||||
|
|
||||||
inline unsigned int str2tag(const std::string &s) {
|
inline unsigned int str2tag(const std::string &s) {
|
||||||
|
|
Loading…
Reference in a new issue