Skip to contentSkip to conversation

keys: Remove unnecessary local variable from ca_keys_setup

PATCH In review Thorsten Blum 3 participants 4 messages Oct 23, 2025 – Oct 27, 2025

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 keys: Remove unnecessary local variable from ca_keys_setup

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.

---
 crypto/asymmetric_keys/restrict.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
crypto/asymmetric_keys/restrict.c +1 -3
index afcd4d101ac5..57ee2021fef7 100644
@@ -29,15 +29,13 @@ static int __init ca_keys_setup(char *str)
 	if (strncmp(str, "id:", 3) == 0) {
 		struct asymmetric_key_id *p = &cakey.id;
 		size_t hexlen = (strlen(str) - 3) / 2;
-		int ret;
 
 		if (hexlen == 0 || hexlen > sizeof(cakey.data)) {
 			pr_err("Missing or invalid ca_keys id\n");
 			return 1;
 		}
 
-		ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
-		if (ret < 0)
+		if (__asymmetric_key_hex_to_key_id(str + 3, p, hexlen) < 0)
 			pr_err("Unparsable ca_keys id hex string\n");
 		else
 			ca_keyid = p;	/* owner key 'id:xxxxxx' */
Signature · 1 line
2.51.0
#1 lore raw

PATCH 2/2 keys: Remove unnecessary local variable from proc_keys_show

The local variable 'rc' is only used to temporary store the result of calling key_task_permission(). Use the result directly and remove the local variable.

---
 security/keys/proc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
security/keys/proc.c +1 -3
index 4f4e2c1824f1..39af57af2aad 100644
@@ -160,7 +160,6 @@ static int proc_keys_show(struct seq_file *m, void *v)
 	char xbuf[16];
 	short state;
 	u64 timo;
-	int rc;
 
 	struct keyring_search_context ctx = {
 		.index_key		= key->index_key,
@@ -188,8 +187,7 @@ static int proc_keys_show(struct seq_file *m, void *v)
 	}
 
 	/* check whether the current task is allowed to view the key */
-	rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
-	if (rc < 0)
+	if (key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW) < 0)
 		return 0;
 
 	now = ktime_get_real_seconds();
Signature · 1 line
2.51.0
maintainer
#3 lore raw
In reply to Thorsten Blum
Quoted 3 lines · Thorsten Blum

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.

[...]

Quoted 8 lines

+++ b/crypto/asymmetric_keys/restrict.c

crypto/asymmetric_keys/restrict.c +0 -1
@@ -29,15 +29,13 @@ static int __init ca_keys_setup(char *str)
 	if (strncmp(str, "id:", 3) == 0) {
 		struct asymmetric_key_id *p = &cakey.id;
 		size_t hexlen = (strlen(str) - 3) / 2;
-		int ret;
 
 		if (hexlen == 0 || hexlen > sizeof(cakey.data)) {

crypto/asymmetric_keys/restrict.c static int __init ca_keys_setup(char *str) · line 41

Quoted 10 lines
crypto/asymmetric_keys/restrict.c +1 -2
 			pr_err("Missing or invalid ca_keys id\n");
 			return 1;
 		}
 
-		ret = __asymmetric_key_hex_to_key_id(str + 3, p, hexlen);
-		if (ret < 0)
+		if (__asymmetric_key_hex_to_key_id(str + 3, p, hexlen) < 0)
 			pr_err("Unparsable ca_keys id hex string\n");
 		else
 			ca_keyid = p;	/* owner key 'id:xxxxxx' */

Quite honestly I don't think this change constitutes a worthwhile improvement.

Those "if (ret)" checks are everywhere in the kernel, it's a pattern that developers have grown accustomed to and immediately understand when reading the code. If it takes an extra variable declaration, so be it.

For people (like me) who frequently have to dig in the git history with recursive "git blame", changes like this one make life harder because it introduces an extra step when trying to understand from which commit a particular line of code originated.

And so changes like this one which are merely motivated by personal stylistic preferences become a net negative.

Thanks,

Lukas

maintainer
#2 lore raw

Re: [PATCH 2/2] keys: Remove unnecessary local variable from proc_keys_show

In reply to Thorsten Blum
Quoted 22 lines · Thorsten Blum

The local variable 'rc' is only used to temporary store the result of calling key_task_permission(). Use the result directly and remove the local variable.

---
 security/keys/proc.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
security/keys/proc.c +0 -1
index 4f4e2c1824f1..39af57af2aad 100644
@@ -160,7 +160,6 @@ static int proc_keys_show(struct seq_file *m, void *v)
 	char xbuf[16];
 	short state;
 	u64 timo;
-	int rc;
 
 	struct keyring_search_context ctx = {
 		.index_key		= key->index_key,

security/keys/proc.c static int proc_keys_show(struct seq_file *m, void *v) · line 191

Quoted 7 lines
security/keys/proc.c +1 -2
@@ -188,8 +187,7 @@ static int proc_keys_show(struct seq_file *m, void *v)
 	}
 
 	/* check whether the current task is allowed to view the key */
-	rc = key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW);
-	if (rc < 0)
+	if (key_task_permission(key_ref, ctx.cred, KEY_NEED_VIEW) < 0)
 		return 0;

I absolutely dislike combining return value check to the call.

The old version documents that we ignore the return value, which is convoluted in the new version.

Quoted 5 lines
security/keys/proc.c +0 -0
 	now = ktime_get_real_seconds();
Signature · 1 line
2.51.0

BR, Jarkko