[clang] [clang][bytecode][NFC] Code size is always aligned (PR #151824)

Shafik Yaghmour via cfe-commits cfe-commits at lists.llvm.org
Sat Aug 2 12:17:21 PDT 2025


================
@@ -137,21 +137,21 @@ int32_t ByteCodeEmitter::getOffset(LabelTy Label) {
 template <typename T>
 static void emit(Program &P, std::vector<std::byte> &Code, const T &Val,
                  bool &Success) {
+  size_t ValPos = Code.size();
   size_t Size;
 
   if constexpr (std::is_pointer_v<T>)
-    Size = sizeof(uint32_t);
+    Size = align(sizeof(uint32_t));
   else
-    Size = sizeof(T);
+    Size = align(sizeof(T));
 
-  if (Code.size() + Size > std::numeric_limits<unsigned>::max()) {
+  if (ValPos + Size > std::numeric_limits<unsigned>::max()) {
----------------
shafik wrote:

This does not look like a valid overflow check. Unless we assert both `ValPos` and `Size` are less than or equal to max unsigned. Then the check should really be `ValPos > std::numeric_limits<unsigned>::max() - Size`

https://blog.regehr.org/archives/1139

Maybe I am misunderstanding the check.

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


More information about the cfe-commits mailing list