crypto: nx - Avoid -Wflex-array-member-not-at-end warning
authorGustavo A. R. Silva <gustavoars@kernel.org>
Tue, 5 Mar 2024 19:57:56 +0000 (13:57 -0600)
committerHerbert Xu <herbert@gondor.apana.org.au>
Tue, 2 Apr 2024 02:49:38 +0000 (10:49 +0800)
-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>
drivers/crypto/nx/nx-842.c
drivers/crypto/nx/nx-842.h

index 2ab90ec10e61ebe8784b77ea1281533ed8fca922..82214cde2bcd965b74111cc948a6afc9361227cd 100644 (file)
@@ -251,7 +251,9 @@ int nx842_crypto_compress(struct crypto_tfm *tfm,
                          u8 *dst, unsigned int *dlen)
 {
        struct nx842_crypto_ctx *ctx = crypto_tfm_ctx(tfm);
-       struct nx842_crypto_header *hdr = &ctx->header;
+       struct nx842_crypto_header *hdr =
+                               container_of(&ctx->header,
+                                            struct nx842_crypto_header, hdr);
        struct nx842_crypto_param p;
        struct nx842_constraints c = *ctx->driver->constraints;
        unsigned int groups, hdrsize, h;
@@ -490,7 +492,7 @@ int nx842_crypto_decompress(struct crypto_tfm *tfm,
        }
 
        memcpy(&ctx->header, src, hdr_len);
-       hdr = &ctx->header;
+       hdr = container_of(&ctx->header, struct nx842_crypto_header, hdr);
 
        for (n = 0; n < hdr->groups; n++) {
                /* ignore applies to last group */
index 7590bfb24d79bf427d9ed6a5b54874179dfa9e56..25fa70b2112cff06603a484d8ed2768366085941 100644 (file)
@@ -157,9 +157,11 @@ struct nx842_crypto_header_group {
 } __packed;
 
 struct nx842_crypto_header {
-       __be16 magic;           /* NX842_CRYPTO_MAGIC */
-       __be16 ignore;          /* decompressed end bytes to ignore */
-       u8 groups;              /* total groups in this header */
+       struct_group_tagged(nx842_crypto_header_hdr, hdr,
+               __be16 magic;           /* NX842_CRYPTO_MAGIC */
+               __be16 ignore;          /* decompressed end bytes to ignore */
+               u8 groups;              /* total groups in this header */
+       );
        struct nx842_crypto_header_group group[];
 } __packed;
 
@@ -171,7 +173,7 @@ struct nx842_crypto_ctx {
        u8 *wmem;
        u8 *sbounce, *dbounce;
 
-       struct nx842_crypto_header header;
+       struct nx842_crypto_header_hdr header;
        struct nx842_crypto_header_group group[NX842_CRYPTO_GROUP_MAX];
 
        struct nx842_driver *driver;