[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:12:16 PST 2026


================
@@ -222,3 +222,39 @@ struct on_void_ty {
   // expected-error at +1{{field has incomplete type 'void'}}
   void wrong_ty __counted_by_or_null(count);
 };
+
+//==============================================================================
+// __counted_by_or_null on pointer members in unions
+//==============================================================================
+
+// Pointer in anonymous union with count in parent struct - OK
+struct ptr_in_anon_union_count_in_parent {
+  int count;
+  union {
+    int a;
+    struct size_known *buf __counted_by_or_null(count);
+  };
+};
+
+// Pointer in named union - ERROR
+union ptr_in_named_union {
+  int count;
+  struct size_known *buf __counted_by_or_null(count); // expected-error {{'counted_by_or_null' cannot be applied to a union member}}
----------------
kees wrote:

Is this distinction really needed? The distinction is currently whether the counted or counter member is in a union. Is it useful to call out that both are in unions?

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


More information about the cfe-commits mailing list