[llvm] [Support] Add assert for DataBytes in calculateReflectedCRC32 (PR #228688)

via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 03:30:42 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-llvm-support

Author: Shreesh Adiga (tantei3)

<details>
<summary>Changes</summary>

Add an assertion to calculateReflectedCRC32 to check DataBytes is one of the expected value (1, 2, 4, or 8 bytes).

---
Full diff: https://github.com/llvm/llvm-project/pull/228688.diff


1 Files Affected:

- (modified) llvm/include/llvm/Support/CRC.h (+4) 


``````````diff
diff --git a/llvm/include/llvm/Support/CRC.h b/llvm/include/llvm/Support/CRC.h
index 39c03e2e69c03..d511115b216f5 100644
--- a/llvm/include/llvm/Support/CRC.h
+++ b/llvm/include/llvm/Support/CRC.h
@@ -15,6 +15,7 @@
 
 #include "llvm/Support/Compiler.h"
 #include "llvm/Support/DataTypes.h"
+#include <cassert>
 
 namespace llvm {
 template <typename T> class ArrayRef;
@@ -33,6 +34,9 @@ LLVM_ABI uint32_t crc32(uint32_t CRC, ArrayRef<uint8_t> Data);
 constexpr inline uint32_t calculateReflectedCRC32(uint32_t Crc, uint64_t Data,
                                                   unsigned DataBytes,
                                                   uint32_t Poly) {
+  assert(
+      (DataBytes == 1 || DataBytes == 2 || DataBytes == 4 || DataBytes == 8) &&
+      "Data Bytes out of range.");
   uint32_t Result = Crc;
   // Process each byte
   for (unsigned I = 0; I != DataBytes; ++I) {

``````````

</details>


https://github.com/llvm/llvm-project/pull/228688


More information about the llvm-commits mailing list