UBSan flags two spots that shift a value into the top bits of a word via a
signed operand:
* lib/mdfour.c copy64(): `in[i] << 24` promotes the uchar to int, so a
byte >= 128 overflows int (UB). Cast each byte to uint32.
* hashtable.c NON_ZERO_64(): `(int64)(x) << 32` overflows int64 whenever
x's high bit is set. Shift as uint64_t (covers all four call sites).
Behavior-preserving -- only the intermediate type changes; the resulting
bit pattern is identical.
*/
#define NON_ZERO_32(x) ((x) ? (x) : (uint32_t)1)
-#define NON_ZERO_64(x, y) ((x) || (y) ? (y) | (int64)(x) << 32 | (y) : (int64)1)
+#define NON_ZERO_64(x, y) ((x) || (y) ? (y) | (uint64_t)(x) << 32 | (y) : (int64)1)
uint32_t hashlittle(const void *key, size_t length)
{
int i;
for (i = 0; i < MD4_DIGEST_LEN; i++) {
- M[i] = (in[i*4+3] << 24) | (in[i*4+2] << 16)
- | (in[i*4+1] << 8) | (in[i*4+0] << 0);
+ M[i] = ((uint32)in[i*4+3] << 24) | ((uint32)in[i*4+2] << 16)
+ | ((uint32)in[i*4+1] << 8) | ((uint32)in[i*4+0] << 0);
}
}