Copilot commented on code in PR #3658:
URL: https://github.com/apache/brpc/pull/3658#discussion_r4378945147


##########
src/brpc/policy/crc32c_checksum.cpp:
##########
@@ -61,12 +63,15 @@ void Crc32cCompute(const ChecksumIn& in) {
 }
 
 bool Crc32cVerify(const ChecksumIn& in) {
-    uint32_t crc = ComputeCrc32c(in);
     auto& val = ControllerPrivateAccessor(const_cast<Controller*>(in.cntl))
                     .checksum_value();
-    CHECK_EQ(val.size(), sizeof(crc));
-    auto expected = *reinterpret_cast<const uint32_t*>(val.data());
+    uint32_t expected;
+    if (val.size() != sizeof(expected)) {
+        return false;
+    }
+    memcpy(&expected, val.data(), sizeof(expected));

Review Comment:
   `<cstring>` does not guarantee `memcpy` is available in the global namespace 
on all standard library implementations. Prefer `std::memcpy` (and keep 
`<cstring>`), to avoid portability issues.



##########
test/brpc_checksum_unittest.cpp:
##########
@@ -50,6 +50,18 @@ TEST_F(ChecksumAttachmentTest, 
verify_succeeds_when_body_only) {
     EXPECT_TRUE(brpc::policy::Crc32cVerify(verify_in));
 }
 
+TEST_F(ChecksumAttachmentTest, verify_rejects_invalid_checksum_lengths) {
+    brpc::Controller cntl;
+    butil::IOBuf body;
+    body.append("request body");
+    brpc::ChecksumIn in{&body, &cntl, nullptr};
+    brpc::ControllerPrivateAccessor accessor(&cntl);
+    for (size_t size : {size_t(0), size_t(3), size_t(5)}) {
+        accessor.set_checksum_value(std::string(size, '\0'));
+        EXPECT_FALSE(brpc::policy::Crc32cVerify(in)) << "size=" << size;
+    }
+}

Review Comment:
   Indentation in this newly added test uses 4 spaces; the surrounding tests in 
this file appear to use 2-space indentation. Aligning with the existing style 
will keep diffs consistent and improve readability.



-- 
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