Copilot commented on code in PR #1019:
URL: https://github.com/apache/iceberg-cpp/pull/1019#discussion_r4273201220


##########
src/iceberg/util/murmurhash3_internal.cc:
##########
@@ -48,12 +50,16 @@ namespace iceberg {
 
 namespace {
 //-----------------------------------------------------------------------------
-// Block read - if your platform needs to do endian-swapping or can only
-// handle aligned reads, do the conversion here
+// Read potentially unaligned blocks in the little-endian order required by
+// MurmurHash3, regardless of the host byte order.
 
-FORCE_INLINE uint32_t getblock32(const uint32_t* p, int i) { return p[i]; }
+FORCE_INLINE uint32_t getblock32(const uint8_t* p, int i) {
+  return ReadLittleEndian<uint32_t>(p + i * 4);
+}
 
-FORCE_INLINE uint64_t getblock64(const uint64_t* p, int i) { return p[i]; }
+FORCE_INLINE uint64_t getblock64(const uint8_t* p, int i) {
+  return ReadLittleEndian<uint64_t>(p + i * 8);
+}

Review Comment:
   The expression `i * 4` / `i * 8` is evaluated in `int` and can trigger 
signed-overflow UB if the index range ever grows (even though current caller 
bounds may keep it safe). Prefer performing the byte-offset computation in 
`ptrdiff_t` (or `size_t` where appropriate) and then applying it to `p` to 
avoid UB and keep sanitizers quiet.



##########
src/iceberg/test/murmurhash3_internal_test.cc:
##########
@@ -0,0 +1,120 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+#include "iceberg/util/murmurhash3_internal.h"
+
+#include <array>
+#include <cstddef>
+#include <cstdint>
+#include <numeric>
+
+#include <gtest/gtest.h>
+
+namespace iceberg {
+
+TEST(MurmurHash3Test, CanonicalLittleEndianBlocks) {
+  struct HashCase {
+    int length;
+    uint32_t seed;
+    uint32_t hash_x86_32;
+    std::array<uint32_t, 4> hash_x86_128;
+    std::array<uint64_t, 2> hash_x64_128;
+  };
+  // Input bytes are 0, 1, ..., length - 1. Expected numeric hash words are 
from
+  // the original SMHasher implementation on a little-endian host:
+  // 
https://github.com/aappleby/smhasher/blob/07bb4de10a63e8cc2e1724865454eba635742383/src/MurmurHash3.cpp
+  const std::array<HashCase, 11> cases = {{
+      {.length = 0,
+       .seed = 0,
+       .hash_x86_32 = 0x00000000u,
+       .hash_x86_128 = {0x00000000u, 0x00000000u, 0x00000000u, 0x00000000u},
+       .hash_x64_128 = {0x0000000000000000ULL, 0x0000000000000000ULL}},
+      {.length = 3,
+       .seed = 0,
+       .hash_x86_32 = 0x51d4d0d7u,
+       .hash_x86_128 = {0x37095e0du, 0x33c18cefu, 0x33c18cefu, 0x33c18cefu},
+       .hash_x64_128 = {0xb872a12fef53e6beULL, 0xfb6255c252b396b6ULL}},
+      {.length = 4,
+       .seed = 0,
+       .hash_x86_32 = 0xf4c0ec39u,
+       .hash_x86_128 = {0xc72c99e7u, 0x4e343ce2u, 0x4e343ce2u, 0x4e343ce2u},
+       .hash_x64_128 = {0xe1c594ae0ddfaf10ULL, 0xd3d605bd13c2fde2ULL}},
+      {.length = 7,
+       .seed = 0,
+       .hash_x86_32 = 0x8d7e4914u,
+       .hash_x86_128 = {0xccfa23d7u, 0xbea74c33u, 0xc75d01c8u, 0xc75d01c8u},
+       .hash_x64_128 = {0xbd4c6987ca4b0d68ULL, 0x613addd4bd25c787ULL}},
+      {.length = 8,
+       .seed = 0,
+       .hash_x86_32 = 0xd161d673u,
+       .hash_x86_128 = {0x91f4928au, 0xb18cd5bcu, 0x867244acu, 0x867244acu},
+       .hash_x64_128 = {0x47a7e1bdd68e2fc8ULL, 0x60e6ee02ec31dcc7ULL}},
+      {.length = 15,
+       .seed = 0,
+       .hash_x86_32 = 0x5bd6952du,
+       .hash_x86_128 = {0x757b9c59u, 0x62a6e4cau, 0x61e2db4eu, 0xca775566u},
+       .hash_x64_128 = {0x47231598fd4925e9ULL, 0xcd846dee88c67de9ULL}},
+      {.length = 16,
+       .seed = 0,
+       .hash_x86_32 = 0x191573ddu,
+       .hash_x86_128 = {0x6c25c825u, 0x559180eeu, 0xea538074u, 0x89a55befu},
+       .hash_x64_128 = {0x444924b591903f30ULL, 0xab906456762fe845ULL}},
+      {.length = 17,
+       .seed = 0,
+       .hash_x86_32 = 0xcbe58dc6u,
+       .hash_x86_128 = {0x6ac99cdbu, 0x359c940bu, 0xae1673adu, 0xa275ab51u},
+       .hash_x64_128 = {0x5c76f40f9fe7c20eULL, 0xc15f026b9edaa824ULL}},
+      {.length = 32,
+       .seed = 0,
+       .hash_x86_32 = 0xcac37638u,
+       .hash_x86_128 = {0xd6b16afdu, 0xa4795780u, 0xb7bd7575u, 0x97df89ceu},
+       .hash_x64_128 = {0xc66d9022b62f500fULL, 0x1c050a6e34c31151ULL}},
+      {.length = 33,
+       .seed = 0,
+       .hash_x86_32 = 0x5460867au,
+       .hash_x86_128 = {0x50de15bau, 0x76f99136u, 0x20e7b670u, 0x220dc781u},
+       .hash_x64_128 = {0x7d41281bfaba4612ULL, 0x55ac8073a7d6a30bULL}},
+      {.length = 33,
+       .seed = 42,
+       .hash_x86_32 = 0xceba6008u,
+       .hash_x86_128 = {0x504909ffu, 0xa6b421aau, 0x53337749u, 0x6d63b0deu},
+       .hash_x64_128 = {0xd693141f9e1df25eULL, 0xaf456193ea9735c8ULL}},
+  }};
+  for (const auto& test : cases) {
+    SCOPED_TRACE(test.length);
+    SCOPED_TRACE(test.seed);
+    for (size_t offset = 0; offset < 8; ++offset) {
+      SCOPED_TRACE(offset);
+      alignas(uint64_t) std::array<uint8_t, 40> input{};
+      std::iota(input.begin() + offset, input.begin() + offset + 33, 
uint8_t{0});
+      const auto* data = input.data() + offset;
+      uint32_t hash_x86_32 = 0;
+      std::array<uint32_t, 4> hash_x86_128{};
+      std::array<uint64_t, 2> hash_x64_128{};
+      MurmurHash3_x86_32(data, test.length, test.seed, &hash_x86_32);
+      MurmurHash3_x86_128(data, test.length, test.seed, hash_x86_128.data());
+      MurmurHash3_x64_128(data, test.length, test.seed, hash_x64_128.data());
+      EXPECT_EQ(hash_x86_32, test.hash_x86_32);
+      EXPECT_EQ(hash_x86_128, test.hash_x86_128);
+      EXPECT_EQ(hash_x64_128, test.hash_x64_128);

Review Comment:
   GTest convention is `EXPECT_EQ(expected, actual)`; swapping the argument 
order improves failure diagnostics (the diff output will label expected vs. 
actual correctly). Consider changing to `EXPECT_EQ(test.hash_x86_32, 
hash_x86_32)` (and similarly for the arrays).



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to