From 93b3b73b49aa2d1e03df49114b6a628f00c808d5 Mon Sep 17 00:00:00 2001 From: Dmitry Ilvokhin Date: Tue, 14 Jan 2025 10:46:39 -0800 Subject: [PATCH] Fix integer overflow in test/unit/hash.c `final[3]` is `uint8_t`. Integer conversion rank of `uint8_t` is lower than integer conversion rank of `int`, so `uint8_t` got promoted to `int`, which is signed integer type. Shift `final[3]` value left on 24, when leftmost bit is set overflows `int` and it is undefined behaviour. Before this change Undefined Behaviour Sanitizer was unhappy about it with the following message. ``` ../test/unit/hash.c:119:25: runtime error: left shift of 176 by 24 places cannot be represented in type 'int' ``` After this commit problem is gone. --- test/unit/hash.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/test/unit/hash.c b/test/unit/hash.c index 17c66ec6..7276333d 100644 --- a/test/unit/hash.c +++ b/test/unit/hash.c @@ -115,8 +115,11 @@ hash_variant_verify_key(hash_variant_t variant, uint8_t *key) { } default: not_reached(); } - computed = (final[0] << 0) | (final[1] << 8) | (final[2] << 16) | - (final[3] << 24); + computed = + ((uint32_t)final[0] << 0) | + ((uint32_t)final[1] << 8) | + ((uint32_t)final[2] << 16) | + ((uint32_t)final[3] << 24); switch (variant) { #ifdef JEMALLOC_BIG_ENDIAN