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

Jared Wyles via llvm-commits llvm-commits at lists.llvm.org
Sat Oct 3 04:22:33 PDT 2026


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

>From fa2268be60e5acfc268e893d3020673135defb57 Mon Sep 17 00:00:00 2001
From: Shreesh Adiga <16567adigashreesh at gmail.com>
Date: Sat, 3 Oct 2026 15:56:55 +0530
Subject: [PATCH] [Support] Add assert for DataBytes in calculateReflectedCRC32

Add an assertion to calculateReflectedCRC32 to check DataBytes is one
of the expected value (1, 2, 4, or 8 bytes).
---
 llvm/include/llvm/Support/CRC.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/llvm/include/llvm/Support/CRC.h b/llvm/include/llvm/Support/CRC.h
index 39c03e2e69c0369..d511115b216f581 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) {



More information about the llvm-commits mailing list