[all-commits] [llvm/llvm-project] 4b455a: [BoundsSafety][Sema] Allow `sized_by`/`sized_by_or...

Dan Liew via All-commits all-commits at lists.llvm.org
Wed Jul 15 02:41:07 PDT 2026


  Branch: refs/heads/main
  Home:   https://github.com/llvm/llvm-project
  Commit: 4b455a91141d59ace3a29870c885f5f5e4ff0506
      https://github.com/llvm/llvm-project/commit/4b455a91141d59ace3a29870c885f5f5e4ff0506
  Author: Dan Liew <dan at su-root.co.uk>
  Date:   2026-07-15 (Wed, 15 Jul 2026)

  Changed paths:
    M clang/docs/ReleaseNotes.md
    M clang/lib/Sema/SemaBoundsSafety.cpp
    M clang/test/Sema/attr-sized-by-late-parsed-struct-ptrs.c
    M clang/test/Sema/attr-sized-by-or-null-late-parsed-struct-ptrs.c
    M clang/test/Sema/attr-sized-by-or-null-struct-ptrs.c
    M clang/test/Sema/attr-sized-by-struct-ptrs.c

  Log Message:
  -----------
  [BoundsSafety][Sema] Allow `sized_by`/`sized_by_or_null` on pointers to structs with a flexible array member (#209603)

`Sema::CheckCountedByAttrOnField()` in `SemaBoundsSafety.cpp` rejects
the `counted_by` family of attributes when the pointee is a struct that
contains a flexible array member (FAM). For example:

```
struct has_unannotated_fam {
  int count;
  int buffer[];
};

struct on_member_pointer_struct_with_fam {
  int size;
  struct has_unannotated_fam *objects __counted_by(size);
};
```

This restriction makes sense for `counted_by`/`counted_by_or_null`
because the size of a struct with a FAM is not statically known, so the
count of elements cannot be used to compute the size of the buffer.

However, the same check was incorrectly applied to `sized_by` and
`sized_by_or_null`. Unlike `counted_by`, these attributes describe the
size of the buffer in *bytes* rather than in *elements*, so the pointee
size is irrelevant and there is no ambiguity. Previously the following
would be rejected:

```
struct on_member_pointer_struct_with_fam {
  int size;
  struct has_unannotated_fam *objects __sized_by(size);
};
```

with the diagnostic:

```
error: 'sized_by' cannot be applied to a pointer with pointee of unknown
size because 'struct has_unannotated_fam' is a struct type with a
flexible array member
```

This patch guards the FAM check with `!CountInBytes` so that it only
applies to `counted_by`/`counted_by_or_null`. `sized_by` and
`sized_by_or_null` on such pointers are now correctly accepted.

The obsolete `expected-error` directives in the affected `sized_by` and
`sized_by_or_null` Sema tests are removed, turning those cases into
positive tests.

Assisted-by: Claude Code
rdar://182226884



To unsubscribe from these emails, change your notification settings at https://github.com/llvm/llvm-project/settings/notifications


More information about the All-commits mailing list