Skip to content

Conversation

@HendrikHuebner
Copy link
Contributor

llvm::TypeSize uses 64bit integers, so we should cast the recordSize before multiplying by 8 to prevent an overflow.

@llvmbot llvmbot added clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project labels Nov 11, 2025
@llvmbot
Copy link
Member

llvmbot commented Nov 11, 2025

@llvm/pr-subscribers-clangir

@llvm/pr-subscribers-clang

Author: Hendrik Hübner (HendrikHuebner)

Changes

llvm::TypeSize uses 64bit integers, so we should cast the recordSize before multiplying by 8 to prevent an overflow.


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

1 Files Affected:

  • (modified) clang/lib/CIR/Dialect/IR/CIRTypes.cpp (+1-1)
diff --git a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
index 5897352829891..f7907c76c8ccb 100644
--- a/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
+++ b/clang/lib/CIR/Dialect/IR/CIRTypes.cpp
@@ -341,7 +341,7 @@ RecordType::getTypeSizeInBits(const mlir::DataLayout &dataLayout,
   if (isUnion())
     return dataLayout.getTypeSize(getLargestMember(dataLayout));
 
-  unsigned recordSize = computeStructSize(dataLayout);
+  auto recordSize = static_cast<uint64_t>(computeStructSize(dataLayout));
   return llvm::TypeSize::getFixed(recordSize * 8);
 }
 

Copy link
Contributor

@andykaylor andykaylor left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you seen a case where this overflow happens? Your change seems good. Normally we require tests for every change, but in this case I don't think that's necessary.

@HendrikHuebner
Copy link
Contributor Author

I have not tried reproducing it, I just noticed this while reading the code. Itt seems safer to add the cast.

@andykaylor andykaylor merged commit a799a8e into llvm:main Nov 12, 2025
13 checks passed
git-crd pushed a commit to git-crd/crd-llvm-project that referenced this pull request Nov 13, 2025
`llvm::TypeSize` uses 64bit integers, so we should cast the `recordSize`
before multiplying by 8 to prevent an overflow.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang Clang issues not falling into any other category ClangIR Anything related to the ClangIR project

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants