Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: nx - Avoid -Wflex-array-member-not-at-end warning

-Wflex-array-member-not-at-end is coming in GCC-14, and we are getting
ready to enable it globally. So, we are deprecating flexible-array
members in the middle of another structure.

There is currently an object (`header`) in `struct nx842_crypto_ctx`
that contains a flexible structure (`struct nx842_crypto_header`):

struct nx842_crypto_ctx {
...
struct nx842_crypto_header header;
struct nx842_crypto_header_group group[NX842_CRYPTO_GROUP_MAX];
...
};

So, in order to avoid ending up with a flexible-array member in the
middle of another struct, we use the `struct_group_tagged()` helper to
separate the flexible array from the rest of the members in the flexible
structure:

struct nx842_crypto_header {
struct_group_tagged(nx842_crypto_header_hdr, hdr,

... the rest of the members

);
struct nx842_crypto_header_group group[];
} __packed;

With the change described above, we can now declare an object of the
type of the tagged struct, without embedding the flexible array in the
middle of another struct:

struct nx842_crypto_ctx {
...
struct nx842_crypto_header_hdr header;
struct nx842_crypto_header_group group[NX842_CRYPTO_GROUP_MAX];
...
} __packed;

We also use `container_of()` whenever we need to retrieve a pointer to
the flexible structure, through which we can access the flexible
array if needed.

So, with these changes, fix the following warning:

In file included from drivers/crypto/nx/nx-842.c:55:
drivers/crypto/nx/nx-842.h:174:36: warning: structure containing a flexible array member is not at the end of another structure [-Wflex-array-member-not-at-end]
174 | struct nx842_crypto_header header;
| ^~~~~~

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

authored by

Gustavo A. R. Silva and committed by
Herbert Xu
1e6b251c 7467147e

+10 -6
+4 -2
drivers/crypto/nx/nx-842.c
··· 251 251 u8 *dst, unsigned int *dlen) 252 252 { 253 253 struct nx842_crypto_ctx *ctx = crypto_tfm_ctx(tfm); 254 - struct nx842_crypto_header *hdr = &ctx->header; 254 + struct nx842_crypto_header *hdr = 255 + container_of(&ctx->header, 256 + struct nx842_crypto_header, hdr); 255 257 struct nx842_crypto_param p; 256 258 struct nx842_constraints c = *ctx->driver->constraints; 257 259 unsigned int groups, hdrsize, h; ··· 492 490 } 493 491 494 492 memcpy(&ctx->header, src, hdr_len); 495 - hdr = &ctx->header; 493 + hdr = container_of(&ctx->header, struct nx842_crypto_header, hdr); 496 494 497 495 for (n = 0; n < hdr->groups; n++) { 498 496 /* ignore applies to last group */
+6 -4
drivers/crypto/nx/nx-842.h
··· 157 157 } __packed; 158 158 159 159 struct nx842_crypto_header { 160 - __be16 magic; /* NX842_CRYPTO_MAGIC */ 161 - __be16 ignore; /* decompressed end bytes to ignore */ 162 - u8 groups; /* total groups in this header */ 160 + struct_group_tagged(nx842_crypto_header_hdr, hdr, 161 + __be16 magic; /* NX842_CRYPTO_MAGIC */ 162 + __be16 ignore; /* decompressed end bytes to ignore */ 163 + u8 groups; /* total groups in this header */ 164 + ); 163 165 struct nx842_crypto_header_group group[]; 164 166 } __packed; 165 167 ··· 173 171 u8 *wmem; 174 172 u8 *sbounce, *dbounce; 175 173 176 - struct nx842_crypto_header header; 174 + struct nx842_crypto_header_hdr header; 177 175 struct nx842_crypto_header_group group[NX842_CRYPTO_GROUP_MAX]; 178 176 179 177 struct nx842_driver *driver;