[clang] [Clang][Sema] Support 'counted_by' attribute on FAM/pointers in anonymous unions (PR #171996)

Kees Cook via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 9 11:07:35 PST 2026


================
@@ -207,10 +217,17 @@ bool Sema::CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes,
     return true;
   }
 
+  // Reject if both FD and CountFD are in the same union - they share storage.
+  if (FD->getParent() == CountFD->getParent() && FD->getParent()->isUnion()) {
+    Diag(FD->getBeginLoc(), diag::err_count_attr_in_union)
+        << Kind << FD->getSourceRange();
+    return true;
+  }
+
   if (FD->getParent() != CountFD->getParent()) {
     if (CountFD->getParent()->isUnion()) {
----------------
kees wrote:

Yes! Thank you, good catch. I've added this and fixed the tests to notice it.

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


More information about the cfe-commits mailing list