This is an automated email from the ASF dual-hosted git repository.

wwbmmm pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/brpc.git


The following commit(s) were added to refs/heads/master by this push:
     new 2cc17a54e Validate CRC32C checksum values before decoding (#3658)
2cc17a54e is described below

commit 2cc17a54ed2c118a30478d22ff07ae9cfe737b94
Author: Xiaofeng Wang <[email protected]>
AuthorDate: Sat Oct 31 14:59:25 2026 +0800

    Validate CRC32C checksum values before decoding (#3658)
    
    * Validate CRC32C checksum values before decoding
    
    * Guard checksum log capture when glog is enabled
    
    * Simplify checksum length validation test
    
    * Qualify checksum byte copy with std namespace
---
 src/brpc/policy/crc32c_checksum.cpp | 11 ++++++++---
 test/brpc_checksum_unittest.cpp     | 12 ++++++++++++
 2 files changed, 20 insertions(+), 3 deletions(-)

diff --git a/src/brpc/policy/crc32c_checksum.cpp 
b/src/brpc/policy/crc32c_checksum.cpp
index 8af7fbe2f..953fb2c7a 100644
--- a/src/brpc/policy/crc32c_checksum.cpp
+++ b/src/brpc/policy/crc32c_checksum.cpp
@@ -17,6 +17,8 @@
 
 #include "brpc/policy/crc32c_checksum.h"
 
+#include <cstring>
+
 #include "brpc/details/controller_private_accessor.h"
 #include "brpc/log.h"
 #include "butil/crc32c.h"
@@ -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;
+    }
+    std::memcpy(&expected, val.data(), sizeof(expected));
     expected = butil::crc32c::Unmask(butil::NetToHost32(expected));
+    const uint32_t crc = ComputeCrc32c(in);
     RPC_VLOG << "Crc32cVerify crc=" << crc << " expected=" << expected;
     return crc == expected;
 }
diff --git a/test/brpc_checksum_unittest.cpp b/test/brpc_checksum_unittest.cpp
index 7811cd6ce..57be5e964 100644
--- a/test/brpc_checksum_unittest.cpp
+++ b/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;
+    }
+}
+
 TEST_F(ChecksumAttachmentTest, verify_succeeds_when_attachment_included) {
     brpc::Controller cntl;
     butil::IOBuf body;


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

Reply via email to