* [PATCH] crypto: use designated initializers for report structs
@ 2026-05-08 10:57 Thorsten Blum
2026-05-08 18:40 ` Eric Biggers
0 siblings, 1 reply; 3+ messages in thread
From: Thorsten Blum @ 2026-05-08 10:57 UTC (permalink / raw)
To: Herbert Xu, David S. Miller; +Cc: Thorsten Blum, linux-crypto, linux-kernel
Use designated initializers for the report structs instead of clearing
the struct with memset() and then copying fixed strings with strscpy()
at runtime.
This keeps the structs zero-initialized, lets the compiler diagnose
oversized string literals, and makes the code easier to read.
Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
---
crypto/acompress.c | 8 +++-----
crypto/aead.c | 10 ++++------
crypto/ahash.c | 8 +++-----
crypto/akcipher.c | 8 +++-----
crypto/crypto_user.c | 14 ++++++--------
crypto/kpp.c | 8 +++-----
crypto/lskcipher.c | 10 ++++------
crypto/rng.c | 8 +++-----
crypto/scompress.c | 8 +++-----
crypto/shash.c | 8 +++-----
crypto/sig.c | 6 +++---
crypto/skcipher.c | 10 ++++------
12 files changed, 42 insertions(+), 64 deletions(-)
diff --git a/crypto/acompress.c b/crypto/acompress.c
index 6025c1acce49..032de704eb2c 100644
--- a/crypto/acompress.c
+++ b/crypto/acompress.c
@@ -51,11 +51,9 @@ static inline struct acomp_alg *crypto_acomp_alg(struct crypto_acomp *tfm)
static int __maybe_unused crypto_acomp_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_acomp racomp;
-
- memset(&racomp, 0, sizeof(racomp));
-
- strscpy(racomp.type, "acomp", sizeof(racomp.type));
+ struct crypto_report_acomp racomp = {
+ .type = "acomp",
+ };
return nla_put(skb, CRYPTOCFGA_REPORT_ACOMP, sizeof(racomp), &racomp);
}
diff --git a/crypto/aead.c b/crypto/aead.c
index e009937bf3a5..045b74c3779f 100644
--- a/crypto/aead.c
+++ b/crypto/aead.c
@@ -136,13 +136,11 @@ static int crypto_aead_init_tfm(struct crypto_tfm *tfm)
static int __maybe_unused crypto_aead_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_aead raead;
struct aead_alg *aead = container_of(alg, struct aead_alg, base);
-
- memset(&raead, 0, sizeof(raead));
-
- strscpy(raead.type, "aead", sizeof(raead.type));
- strscpy(raead.geniv, "<none>", sizeof(raead.geniv));
+ struct crypto_report_aead raead = {
+ .type = "aead",
+ .geniv = "<none>",
+ };
raead.blocksize = alg->cra_blocksize;
raead.maxauthsize = aead->maxauthsize;
diff --git a/crypto/ahash.c b/crypto/ahash.c
index 7a730324c50e..dd56b0e45c0d 100644
--- a/crypto/ahash.c
+++ b/crypto/ahash.c
@@ -789,11 +789,9 @@ static void crypto_ahash_free_instance(struct crypto_instance *inst)
static int __maybe_unused crypto_ahash_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_hash rhash;
-
- memset(&rhash, 0, sizeof(rhash));
-
- strscpy(rhash.type, "ahash", sizeof(rhash.type));
+ struct crypto_report_hash rhash = {
+ .type = "ahash",
+ };
rhash.blocksize = alg->cra_blocksize;
rhash.digestsize = __crypto_hash_alg_common(alg)->digestsize;
diff --git a/crypto/akcipher.c b/crypto/akcipher.c
index dfe87b3ce183..630bb19738be 100644
--- a/crypto/akcipher.c
+++ b/crypto/akcipher.c
@@ -36,11 +36,9 @@ struct crypto_akcipher_sync_data {
static int __maybe_unused crypto_akcipher_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_akcipher rakcipher;
-
- memset(&rakcipher, 0, sizeof(rakcipher));
-
- strscpy(rakcipher.type, "akcipher", sizeof(rakcipher.type));
+ struct crypto_report_akcipher rakcipher = {
+ .type = "akcipher",
+ };
return nla_put(skb, CRYPTOCFGA_REPORT_AKCIPHER,
sizeof(rakcipher), &rakcipher);
diff --git a/crypto/crypto_user.c b/crypto/crypto_user.c
index 3187e0d276f9..e8b6ae75f31f 100644
--- a/crypto/crypto_user.c
+++ b/crypto/crypto_user.c
@@ -70,11 +70,9 @@ static struct crypto_alg *crypto_alg_match(struct crypto_user_alg *p, int exact)
static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_cipher rcipher;
-
- memset(&rcipher, 0, sizeof(rcipher));
-
- strscpy(rcipher.type, "cipher", sizeof(rcipher.type));
+ struct crypto_report_cipher rcipher = {
+ .type = "cipher",
+ };
rcipher.blocksize = alg->cra_blocksize;
rcipher.min_keysize = alg->cra_cipher.cia_min_keysize;
@@ -103,10 +101,10 @@ static int crypto_report_one(struct crypto_alg *alg,
if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority))
goto nla_put_failure;
if (alg->cra_flags & CRYPTO_ALG_LARVAL) {
- struct crypto_report_larval rl;
+ struct crypto_report_larval rl = {
+ .type = "larval",
+ };
- memset(&rl, 0, sizeof(rl));
- strscpy(rl.type, "larval", sizeof(rl.type));
if (nla_put(skb, CRYPTOCFGA_REPORT_LARVAL, sizeof(rl), &rl))
goto nla_put_failure;
goto out;
diff --git a/crypto/kpp.c b/crypto/kpp.c
index 7451d39a7ad8..522c352a03af 100644
--- a/crypto/kpp.c
+++ b/crypto/kpp.c
@@ -20,11 +20,9 @@
static int __maybe_unused crypto_kpp_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_kpp rkpp;
-
- memset(&rkpp, 0, sizeof(rkpp));
-
- strscpy(rkpp.type, "kpp", sizeof(rkpp.type));
+ struct crypto_report_kpp rkpp = {
+ .type = "kpp",
+ };
return nla_put(skb, CRYPTOCFGA_REPORT_KPP, sizeof(rkpp), &rkpp);
}
diff --git a/crypto/lskcipher.c b/crypto/lskcipher.c
index bb166250b732..e4328df6e26c 100644
--- a/crypto/lskcipher.c
+++ b/crypto/lskcipher.c
@@ -264,12 +264,10 @@ static int __maybe_unused crypto_lskcipher_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
struct lskcipher_alg *skcipher = __crypto_lskcipher_alg(alg);
- struct crypto_report_blkcipher rblkcipher;
-
- memset(&rblkcipher, 0, sizeof(rblkcipher));
-
- strscpy(rblkcipher.type, "lskcipher", sizeof(rblkcipher.type));
- strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
+ struct crypto_report_blkcipher rblkcipher = {
+ .type = "lskcipher",
+ .geniv = "<none>",
+ };
rblkcipher.blocksize = alg->cra_blocksize;
rblkcipher.min_keysize = skcipher->co.min_keysize;
diff --git a/crypto/rng.c b/crypto/rng.c
index 1d4b9177bad4..eec786c45bdd 100644
--- a/crypto/rng.c
+++ b/crypto/rng.c
@@ -65,11 +65,9 @@ static unsigned int seedsize(struct crypto_alg *alg)
static int __maybe_unused crypto_rng_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_rng rrng;
-
- memset(&rrng, 0, sizeof(rrng));
-
- strscpy(rrng.type, "rng", sizeof(rrng.type));
+ struct crypto_report_rng rrng = {
+ .type = "rng",
+ };
rrng.seedsize = seedsize(alg);
diff --git a/crypto/scompress.c b/crypto/scompress.c
index 253655ece83f..de54227203ce 100644
--- a/crypto/scompress.c
+++ b/crypto/scompress.c
@@ -48,11 +48,9 @@ static DECLARE_WORK(scomp_scratch_work, scomp_scratch_workfn);
static int __maybe_unused crypto_scomp_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_comp rscomp;
-
- memset(&rscomp, 0, sizeof(rscomp));
-
- strscpy(rscomp.type, "scomp", sizeof(rscomp.type));
+ struct crypto_report_comp rscomp = {
+ .type = "scomp",
+ };
return nla_put(skb, CRYPTOCFGA_REPORT_COMPRESS,
sizeof(rscomp), &rscomp);
diff --git a/crypto/shash.c b/crypto/shash.c
index 2f07d0bd1f61..d31a65570f69 100644
--- a/crypto/shash.c
+++ b/crypto/shash.c
@@ -333,12 +333,10 @@ static void crypto_shash_free_instance(struct crypto_instance *inst)
static int __maybe_unused crypto_shash_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
- struct crypto_report_hash rhash;
struct shash_alg *salg = __crypto_shash_alg(alg);
-
- memset(&rhash, 0, sizeof(rhash));
-
- strscpy(rhash.type, "shash", sizeof(rhash.type));
+ struct crypto_report_hash rhash = {
+ .type = "shash",
+ };
rhash.blocksize = alg->cra_blocksize;
rhash.digestsize = salg->digestsize;
diff --git a/crypto/sig.c b/crypto/sig.c
index beba745b6405..7d2048da5c3a 100644
--- a/crypto/sig.c
+++ b/crypto/sig.c
@@ -53,9 +53,9 @@ static void __maybe_unused crypto_sig_show(struct seq_file *m,
static int __maybe_unused crypto_sig_report(struct sk_buff *skb,
struct crypto_alg *alg)
{
- struct crypto_report_sig rsig = {};
-
- strscpy(rsig.type, "sig", sizeof(rsig.type));
+ struct crypto_report_sig rsig = {
+ .type = "sig",
+ };
return nla_put(skb, CRYPTOCFGA_REPORT_SIG, sizeof(rsig), &rsig);
}
diff --git a/crypto/skcipher.c b/crypto/skcipher.c
index 2b31d1d5d268..617e840432b1 100644
--- a/crypto/skcipher.c
+++ b/crypto/skcipher.c
@@ -591,12 +591,10 @@ static int __maybe_unused crypto_skcipher_report(
struct sk_buff *skb, struct crypto_alg *alg)
{
struct skcipher_alg *skcipher = __crypto_skcipher_alg(alg);
- struct crypto_report_blkcipher rblkcipher;
-
- memset(&rblkcipher, 0, sizeof(rblkcipher));
-
- strscpy(rblkcipher.type, "skcipher", sizeof(rblkcipher.type));
- strscpy(rblkcipher.geniv, "<none>", sizeof(rblkcipher.geniv));
+ struct crypto_report_blkcipher rblkcipher = {
+ .type = "skcipher",
+ .geniv = "<none>",
+ };
rblkcipher.blocksize = alg->cra_blocksize;
rblkcipher.min_keysize = skcipher->min_keysize;
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] crypto: use designated initializers for report structs
2026-05-08 10:57 [PATCH] crypto: use designated initializers for report structs Thorsten Blum
@ 2026-05-08 18:40 ` Eric Biggers
2026-05-08 20:11 ` Thorsten Blum
0 siblings, 1 reply; 3+ messages in thread
From: Eric Biggers @ 2026-05-08 18:40 UTC (permalink / raw)
To: Thorsten Blum; +Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel
On Fri, May 08, 2026 at 12:57:17PM +0200, Thorsten Blum wrote:
> Use designated initializers for the report structs instead of clearing
> the struct with memset() and then copying fixed strings with strscpy()
> at runtime.
>
> This keeps the structs zero-initialized, lets the compiler diagnose
> oversized string literals, and makes the code easier to read.
>
> Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
Did you verify that none of these structs contain any implicit padding?
- Eric
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: [PATCH] crypto: use designated initializers for report structs
2026-05-08 18:40 ` Eric Biggers
@ 2026-05-08 20:11 ` Thorsten Blum
0 siblings, 0 replies; 3+ messages in thread
From: Thorsten Blum @ 2026-05-08 20:11 UTC (permalink / raw)
To: Eric Biggers; +Cc: Herbert Xu, David S. Miller, linux-crypto, linux-kernel
On Fri, May 08, 2026 at 06:40:13PM +0000, Eric Biggers wrote:
> On Fri, May 08, 2026 at 12:57:17PM +0200, Thorsten Blum wrote:
> > Use designated initializers for the report structs instead of clearing
> > the struct with memset() and then copying fixed strings with strscpy()
> > at runtime.
> >
> > This keeps the structs zero-initialized, lets the compiler diagnose
> > oversized string literals, and makes the code easier to read.
> >
> > Signed-off-by: Thorsten Blum <thorsten.blum@linux.dev>
>
> Did you verify that none of these structs contain any implicit padding?
Yes, I checked the structs manually and with pahole, which also reported
no holes. The structs only use char[64] followed by unsigned ints, so
implicit padding shouldn't be an issue.
Thanks,
Thorsten
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2026-05-08 20:11 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-05-08 10:57 [PATCH] crypto: use designated initializers for report structs Thorsten Blum
2026-05-08 18:40 ` Eric Biggers
2026-05-08 20:11 ` Thorsten Blum
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox