Skip to content

Commit dfd500d

Browse files
GustavoARSilvachucklever
authored andcommitted
fs: nfs: acl: Avoid -Wflex-array-member-not-at-end warning
-Wflex-array-member-not-at-end was introduced in GCC-14, and we are getting ready to enable it, globally. So, in order to avoid ending up with a flexible-array member in the middle of other structs, we use the `struct_group_tagged()` helper to create a new tagged `struct posix_acl_hdr`. This structure groups together all the members of the flexible `struct posix_acl` except the flexible array. As a result, the array is effectively separated from the rest of the members without modifying the memory layout of the flexible structure. We then change the type of the middle struct member currently causing trouble from `struct posix_acl` to `struct posix_acl_hdr`. We also want to ensure that when new members need to be added to the flexible structure, they are always included within the newly created tagged struct. For this, we use `static_assert()`. This ensures that the memory layout for both the flexible structure and the new tagged struct is the same after any changes. This approach avoids having to implement `struct posix_acl_hdr` as a completely separate structure, thus preventing having to maintain two independent but basically identical structures, closing the door to potential bugs in the future. We also use `container_of()` whenever we need to retrieve a pointer to the flexible structure, through which we can access the flexible-array member, if necessary. So, with these changes, fix the following warning: fs/nfs_common/nfsacl.c:45:26: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end] Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org> Acked-by: Anna Schumaker <anna.schumaker@oracle.com> Acked-by: Jeff Layton <jlayton@kernel.org> Signed-off-by: Chuck Lever <chuck.lever@oracle.com>
1 parent 8ce35dc commit dfd500d

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

fs/nfs_common/nfsacl.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ struct nfsacl_encode_desc {
4242
};
4343

4444
struct nfsacl_simple_acl {
45-
struct posix_acl acl;
45+
struct posix_acl_hdr acl;
4646
struct posix_acl_entry ace[4];
4747
};
4848

@@ -112,7 +112,8 @@ int nfsacl_encode(struct xdr_buf *buf, unsigned int base, struct inode *inode,
112112
xdr_encode_word(buf, base, entries))
113113
return -EINVAL;
114114
if (encode_entries && acl && acl->a_count == 3) {
115-
struct posix_acl *acl2 = &aclbuf.acl;
115+
struct posix_acl *acl2 =
116+
container_of(&aclbuf.acl, struct posix_acl, hdr);
116117

117118
/* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
118119
* invoked in contexts where a memory allocation failure is
@@ -177,7 +178,8 @@ bool nfs_stream_encode_acl(struct xdr_stream *xdr, struct inode *inode,
177178
return false;
178179

179180
if (encode_entries && acl && acl->a_count == 3) {
180-
struct posix_acl *acl2 = &aclbuf.acl;
181+
struct posix_acl *acl2 =
182+
container_of(&aclbuf.acl, struct posix_acl, hdr);
181183

182184
/* Avoid the use of posix_acl_alloc(). nfsacl_encode() is
183185
* invoked in contexts where a memory allocation failure is

include/linux/posix_acl.h

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,16 @@ struct posix_acl_entry {
2727
};
2828

2929
struct posix_acl {
30-
refcount_t a_refcount;
31-
unsigned int a_count;
32-
struct rcu_head a_rcu;
30+
/* New members MUST be added within the struct_group() macro below. */
31+
struct_group_tagged(posix_acl_hdr, hdr,
32+
refcount_t a_refcount;
33+
unsigned int a_count;
34+
struct rcu_head a_rcu;
35+
);
3336
struct posix_acl_entry a_entries[] __counted_by(a_count);
3437
};
38+
static_assert(offsetof(struct posix_acl, a_entries) == sizeof(struct posix_acl_hdr),
39+
"struct member likely outside of struct_group_tagged()");
3540

3641
#define FOREACH_ACL_ENTRY(pa, acl, pe) \
3742
for(pa=(acl)->a_entries, pe=pa+(acl)->a_count; pa<pe; pa++)

0 commit comments

Comments
 (0)