[clang] [BoundsSafety][Sema] Allow counted_by and counted_by_or_null on pointers where the pointee type is incomplete but potentially completable (PR #106321)

Dan Liew via cfe-commits cfe-commits at lists.llvm.org
Tue Jan 7 10:33:42 PST 2025


================
@@ -102,7 +105,36 @@ bool Sema::CheckCountedByAttrOnField(FieldDecl *FD, Expr *E, bool CountInBytes,
   // only `PointeeTy->isStructureTypeWithFlexibleArrayMember()` is reachable
   // when `FieldTy->isArrayType()`.
   bool ShouldWarn = false;
-  if (PointeeTy->isIncompleteType() && !CountInBytes) {
+  if (PointeeTy->isAlwaysIncompleteType() && !CountInBytes) {
----------------
delcypher wrote:

It's not a silly question at all. This is not a model I explored in much depth.

One reason to not do it that way is that our current implementation requires that "uses" of pointers annotated with `__counted_by` have a complete pointee type at the "use" site because in some cases we represent bounds checks at the AST level and IIRC the creation of the AST node that represents the bounds check needs to know the pointee size.

This is a design decision that could (and probably should be) revisited at some point but I don't think this is the right time to do it.

As noted in the PR description there is a case with tentative definitions that isn't handled correctly right now and would require waiting until the end of the translation unit to handle correctly. It's been omitted because it's not something we've needed so we've simply not implemented it yet.

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


More information about the cfe-commits mailing list