mirror of
https://github.com/yhirose/cpp-httplib.git
synced 2026-10-19 11:19:36 +00:00
Hash case-insensitive keys in a loop
case_ignore::hash recursed once per character of the key, and trailer field names from the wire reach it, so a long one could overflow the stack of a build that keeps the recursion (-O0). Hash in a loop instead, which gives the same values.
This commit is contained in:
1 parent
6acdc6255d
commit
82e28aa4f3
2 files changed
+16
-11
No files matched your search
@@ -676,17 +676,13 @@ struct equal_to {
|
||||
|
||||
struct hash {
|
||||
size_t operator()(const std::string &key) const {
|
||||
return hash_core(key.data(), key.size(), 0);
|
||||
}
|
||||
|
||||
size_t hash_core(const char *s, size_t l, size_t h) const {
|
||||
return (l == 0) ? h
|
||||
: hash_core(s + 1, l - 1,
|
||||
// Unsets the 6 high bits of h, therefore no
|
||||
// overflow happens
|
||||
(((std::numeric_limits<size_t>::max)() >> 6) &
|
||||
h * 33) ^
|
||||
static_cast<unsigned char>(to_lower(*s)));
|
||||
size_t h = 0;
|
||||
for (auto c : key) {
|
||||
// Unsets the 6 high bits of h, therefore no overflow happens
|
||||
h = (((std::numeric_limits<size_t>::max)() >> 6) & h * 33) ^
|
||||
static_cast<unsigned char>(to_lower(c));
|
||||
}
|
||||
return h;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in new issue
Block a user