[PATCH 2/2] crypto: asymmetric_keys - simplify asymmetric_key_hex_to_key_id

From: Thorsten Blum

Date: Tue Oct 07 2025 - 14:52:56 EST


Use struct_size() to calculate the number of bytes to allocate for the
asymmetric key id. Add a local variable to store the hex data length
instead of recalculating it.

The variable 'ret', whose name implies a return variable, is only used
to temporarily store the result of __asymmetric_key_hex_to_key_id(). Use
the result directly and remove the local variable.

No functional changes.

Signed-off-by: Thorsten Blum <thorsten.blum@xxxxxxxxx>
---
crypto/asymmetric_keys/asymmetric_type.c | 9 ++++-----
1 file changed, 4 insertions(+), 5 deletions(-)

diff --git a/crypto/asymmetric_keys/asymmetric_type.c b/crypto/asymmetric_keys/asymmetric_type.c
index aea925c88973..b73b8b983bfa 100644
--- a/crypto/asymmetric_keys/asymmetric_type.c
+++ b/crypto/asymmetric_keys/asymmetric_type.c
@@ -228,7 +228,7 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
{
struct asymmetric_key_id *match_id;
size_t asciihexlen;
- int ret;
+ size_t hexlen;

if (!*id)
return ERR_PTR(-EINVAL);
@@ -236,12 +236,11 @@ struct asymmetric_key_id *asymmetric_key_hex_to_key_id(const char *id)
if (asciihexlen & 1)
return ERR_PTR(-EINVAL);

- match_id = kmalloc(sizeof(struct asymmetric_key_id) + asciihexlen / 2,
- GFP_KERNEL);
+ hexlen = asciihexlen / 2;
+ match_id = kmalloc(struct_size(match_id, data, hexlen), GFP_KERNEL);
if (!match_id)
return ERR_PTR(-ENOMEM);
- ret = __asymmetric_key_hex_to_key_id(id, match_id, asciihexlen / 2);
- if (ret < 0) {
+ if (__asymmetric_key_hex_to_key_id(id, match_id, hexlen) < 0) {
kfree(match_id);
return ERR_PTR(-EINVAL);
}
--
2.51.0