<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/58620>58620</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
unsigned integer overflow in llvm::djbHash
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
ibmibmibm
</td>
</tr>
</table>
<pre>
in `llvm/include/llvm/Support/DJB.h`:
```c++
inline uint32_t djbHash(StringRef Buffer, uint32_t H = 5381) {
for (unsigned char C : Buffer.bytes())
H = (H << 5) + H + C;
return H;
}
```
`(H << 5) + H` may overflow, should cast to uint64_t before calculation:
`H = static_cast<uint32_t>((static_cast<uint64_t>(H) << 5) + H + C);`
Likewise, in `llvm/lib/Support/DJB.cpp`:
```c++
static Optional<uint32_t> fastCaseFoldingDjbHash(StringRef Buffer, uint32_t H) {
bool AllASCII = true;
for (unsigned char C : Buffer) {
H = H * 33 + ('A' <= C && C <= 'Z' ? C - 'A' + 'a' : C);
AllASCII &= C <= 0x7f;
}
if (AllASCII)
return H;
return None;
}
```
`H = static_cast<uint32_t>(static_cast<uint64_t>(H) * 33 + ('A' <= C && C <= 'Z' ? C - 'A' + 'a' : C));`
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy1VMlu2zAQ_RrqQsSQqf2gg2zVcIqiBZpbLwEpURZTWhRIKmn-vkPKlrMVzqXAWKJneXwzeiRT7XMpBozSUMrHIyI7MTRyajmsTo67aRyVtrCqv25WPWSiqEJhjcLKrb01iGycea8YpBg4nsRgI3JvcfvA9tT0iOR3Vovh8JN3eDN1HdeIbC9pe4yiGidRvkakwCg7oWHcKY2heBqMOAy8xU1PNd5CdnWCWbFnyw2kQKGzUx0-QULALbZgOPHYZONC8ATfso3mdtID3i8ulNVv2rz8_RASAvhIn7F65LqT6sm1Z3o1SeBMjcVW-W7TGLplHNri4JfNJKkVang51Zm4sRBo7l0tbHQeFIq--Fbz92GHPIf3ntQ_eoYRQY9LO_75TfzmT8Jwx_mVHqRg70TQjON1Gcz08I_RNUfl6w5wB6y31PCdki1oov6kRt5IgyklcSVldbe9vfUzs3riLz7qVe28ATxrxo2qwlHkJ-bHnVXwm0daOwSSgnmo7Syy7NecsAPnDV4KfH1G51i1TH_Z78IeEGfoGTH8k3UvMhc1Yiw6R-lc-Erx70S8uL6rgX9G2teld113_3F0Z-0GvFynaZplYZLkQVtGbREVNLDCSl4unxu48QPXy4l00va6BulG1eliCiYty97a0Tg32YEdhO0ntmrU8XIPutfNqNUDb9w5EMZM7tLZJXlKwqAvi4bxgrVdmyZpw-I0paQp8iiKadat8zAPJGVcmhIlcEbIwIGMg4A1SupAlCQkZB2SNMziYp2v1lHcshR6SrM2SeIIxSE_UiFXjsdK6UOgS0-JTQcDQSmMNZcgNX4E3G8H-HSyvdKlYMfZAr956cn_BamUpdk">