From: Jonathan Wakely Date: Mon, 28 Nov 2022 10:52:23 +0000 (+0000) Subject: libstdc++: Fix _Hash_bytes for I16LP32 targets [PR107885] X-Git-Tag: basepoints/gcc-14~2838 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=7b79fa930917da735f02b4f6911dfbb0a91f9714;p=thirdparty%2Fgcc.git libstdc++: Fix _Hash_bytes for I16LP32 targets [PR107885] For H8/300 size_t is 32 bits wide, but (unsigned char)buf[2] << 16 promotes to int which is only 16 bits wide. The shift is then undefined. This fixes it by converting to size_t before shifting. libstdc++-v3/ChangeLog: PR libstdc++/107885 * libsupc++/hash_bytes.cc (_Hash_bytes): Convert to size_t instead of implicit integer promotion to 16 bits. --- diff --git a/libstdc++-v3/libsupc++/hash_bytes.cc b/libstdc++-v3/libsupc++/hash_bytes.cc index ffdd04f7602d..67e2dbb1a0fe 100644 --- a/libstdc++-v3/libsupc++/hash_bytes.cc +++ b/libstdc++-v3/libsupc++/hash_bytes.cc @@ -90,17 +90,21 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION len -= 4; } + size_t k; // Handle the last few bytes of the input array. switch(len) { case 3: - hash ^= static_cast(buf[2]) << 16; + k = static_cast(buf[2]); + hash ^= k << 16; [[gnu::fallthrough]]; case 2: - hash ^= static_cast(buf[1]) << 8; + k = static_cast(buf[1]); + hash ^= k << 8; [[gnu::fallthrough]]; case 1: - hash ^= static_cast(buf[0]); + k = static_cast(buf[0]); + hash ^= k; hash *= m; };