[clang] [Clang] Fix offsetof sign-extending unsigned array indices >= 128 (PR #204139)

Timm Baeder via cfe-commits cfe-commits at lists.llvm.org
Tue Jun 23 21:32:24 PDT 2026


================
@@ -3786,10 +3799,23 @@ bool Compiler<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
         continue;
       }
 
+      if (IndexT == PT_IntAP || IndexT == PT_IntAPS) {
+        if (!this->visit(ArrayIndexExpr))
+          return false;
+        if (!this->emitCastNoOverflow(IndexT, E))
+          return false;
+        continue;
+      }
       if (!this->visit(ArrayIndexExpr))
         return false;
-      // Cast to Sint64.
+      // Cast to Sint64. For unsigned types, cast to Uint64 first to avoid
+      // sign-extending values with the high bit set (e.g. uint8_t >= 128).
       if (IndexT != PT_Sint64) {
+        if (!isSignedType(IndexT) && IndexT != PT_Uint64) {
+          if (!this->emitCast(IndexT, PT_Uint64, E))
+            return false;
+          IndexT = PT_Uint64;
+        }
----------------
tbaederr wrote:

This change seem unnecessary.

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


More information about the cfe-commits mailing list