Silence warning in older versions of Valgrind
authorJohn Naylor <[email protected]>
Mon, 24 Feb 2025 11:03:29 +0000 (18:03 +0700)
committerJohn Naylor <[email protected]>
Mon, 24 Feb 2025 11:03:29 +0000 (18:03 +0700)
Due to misunderstanding on my part, commit 235328ee4 did not go far
enough to silence older versions of Valgrind. For those, it was the bit
scan that was problematic, not the subsequent bit-masking operation. To
fix, use the unaligned path for the trailing bytes. Since we don't have
a bit scan here anymore, also remove some comments and endian-specific
coding around that.

Reported-by: Anton A. Melnikov <[email protected]>
Discussion: https://postgr.es/m/f3aa2d45-3b28-41c5-9499-a1bc30e0f8ec@postgrespro.ru
Backpatch-through: 17

src/include/common/hashfn_unstable.h

index e07c0226c1f05da1fdf0980e2c769a9faddf0ed6..8818a0d360b69099d98b502f67f71b65e7b691ff 100644 (file)
@@ -14,8 +14,6 @@
 #ifndef HASHFN_UNSTABLE_H
 #define HASHFN_UNSTABLE_H
 
-#include "port/pg_bitutils.h"
-#include "port/pg_bswap.h"
 
 /*
  * fasthash is a modification of code taken from
@@ -262,26 +260,13 @@ fasthash_accum_cstring_aligned(fasthash_state *hs, const char *str)
 
    /*
     * For every chunk of input, check for zero bytes before mixing into the
-    * hash. The chunk with zeros must contain the NUL terminator. We arrange
-    * so that zero_byte_low tells us not only that a zero exists, but also
-    * where it is, so we can hash the remainder of the string.
-    *
-    * The haszero64 calculation will set bits corresponding to the lowest
-    * byte where a zero exists, so that suffices for little-endian machines.
-    * For big-endian machines, we would need bits set for the highest zero
-    * byte in the chunk, since the trailing junk past the terminator could
-    * contain additional zeros. haszero64 does not give us that, so we
-    * byteswap the chunk first.
+    * hash. The chunk with zeros must contain the NUL terminator.
     */
    for (;;)
    {
        uint64      chunk = *(uint64 *) str;
 
-#ifdef WORDS_BIGENDIAN
-       zero_byte_low = haszero64(pg_bswap64(chunk));
-#else
        zero_byte_low = haszero64(chunk);
-#endif
        if (zero_byte_low)
            break;
 
@@ -290,13 +275,8 @@ fasthash_accum_cstring_aligned(fasthash_state *hs, const char *str)
        str += FH_SIZEOF_ACCUM;
    }
 
-   /*
-    * The byte corresponding to the NUL will be 0x80, so the rightmost bit
-    * position will be in the range 7, 15, ..., 63. Turn this into byte
-    * position by dividing by 8.
-    */
-   remainder = pg_rightmost_one_pos64(zero_byte_low) / BITS_PER_BYTE;
-   fasthash_accum(hs, str, remainder);
+   /* mix in remaining bytes */
+   remainder = fasthash_accum_cstring_unaligned(hs, str);
    str += remainder;
 
    return str - start;