Skip to content
This repository was archived by the owner on Apr 30, 2025. It is now read-only.

Commit d262033

Browse files
Prevent overflow in hash function str2tag_core() (yhirose#1529)
* str2tag_core(): prevent overflow * Update httplib.h works for all sizes of unsigned int and if there exists a #define for max
1 parent 5745eab commit d262033

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

httplib.h

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2978,9 +2978,13 @@ inline void get_remote_ip_and_port(socket_t sock, std::string &ip, int &port) {
29782978

29792979
inline constexpr unsigned int str2tag_core(const char *s, size_t l,
29802980
unsigned int h) {
2981-
return (l == 0) ? h
2982-
: str2tag_core(s + 1, l - 1,
2983-
(h * 33) ^ static_cast<unsigned char>(*s));
2981+
return (l == 0)
2982+
? h
2983+
: str2tag_core(
2984+
s + 1, l - 1,
2985+
//unsets the 6 high bits of h, therefore no overflow happens
2986+
(((std::numeric_limits<unsigned int>::max)() >> 6) & h * 33) ^
2987+
static_cast<unsigned char>(*s));
29842988
}
29852989

29862990
inline unsigned int str2tag(const std::string &s) {

0 commit comments

Comments
 (0)