Skip to contentSkip to conversation

KEYS: encrypted: Return early on allocation failure and drop goto

PATCH In review Thorsten Blum 3 participants 3 messages Oct 29, 2025 – Nov 9, 2025

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

PATCH 1/1 KEYS: encrypted: Return early on allocation failure and drop goto

Return ERR_PTR(-ENOMEM) immediately if memory allocation fails, instead of using goto and returning a NULL pointer, and remove the now-unused 'out' label.

At the call site, check 'ascii_buf' with IS_ERR() and propagate the error code returned by datablob_format().

---
 security/keys/encrypted-keys/encrypted.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
security/keys/encrypted-keys/encrypted.c +3 -4
index be1f2118447c..25df00b7dbe9 100644
@@ -276,7 +276,7 @@ static char *datablob_format(struct encrypted_key_payload *epayload,
 
 	ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
 	if (!ascii_buf)
-		goto out;
+		return ERR_PTR(-ENOMEM);
 
 	ascii_buf[asciiblob_len] = '\0';
 
@@ -288,7 +288,6 @@ static char *datablob_format(struct encrypted_key_payload *epayload,
 	bufp = &ascii_buf[len];
 	for (i = 0; i < (asciiblob_len - len) / 2; i++)
 		bufp = hex_byte_pack(bufp, iv[i]);
-out:
 	return ascii_buf;
 }
 
@@ -932,8 +931,8 @@ static long encrypted_read(const struct key *key, char *buffer,
 		goto out;
 
 	ascii_buf = datablob_format(epayload, asciiblob_len);
-	if (!ascii_buf) {
-		ret = -ENOMEM;
+	if (IS_ERR(ascii_buf)) {
+		ret = PTR_ERR(ascii_buf);
 		goto out;
 	}
 
Signature · 1 line
2.51.0
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 8 lines · Thorsten Blum

Return ERR_PTR(-ENOMEM) immediately if memory allocation fails, instead of using goto and returning a NULL pointer, and remove the now-unused 'out' label.

At the call site, check 'ascii_buf' with IS_ERR() and propagate the error code returned by datablob_format().

It seems like purely personal preference, but I don't see any error in it, so in that sense

Quoted 38 lines
---
 security/keys/encrypted-keys/encrypted.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
security/keys/encrypted-keys/encrypted.c +3 -4
index be1f2118447c..25df00b7dbe9 100644
@@ -276,7 +276,7 @@ static char *datablob_format(struct encrypted_key_payload *epayload,
 
 	ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
 	if (!ascii_buf)
-		goto out;
+		return ERR_PTR(-ENOMEM);
 
 	ascii_buf[asciiblob_len] = '\0';
 
@@ -288,7 +288,6 @@ static char *datablob_format(struct encrypted_key_payload *epayload,
 	bufp = &ascii_buf[len];
 	for (i = 0; i < (asciiblob_len - len) / 2; i++)
 		bufp = hex_byte_pack(bufp, iv[i]);
-out:
 	return ascii_buf;
 }
 
@@ -932,8 +931,8 @@ static long encrypted_read(const struct key *key, char *buffer,
 		goto out;
 
 	ascii_buf = datablob_format(epayload, asciiblob_len);
-	if (!ascii_buf) {
-		ret = -ENOMEM;
+	if (IS_ERR(ascii_buf)) {
+		ret = PTR_ERR(ascii_buf);
 		goto out;
 	}
 
Signature · 1 line
2.51.0
maintainer
#2 lore raw
In reply to Thorsten Blum
Quoted 47 lines · Thorsten Blum

Return ERR_PTR(-ENOMEM) immediately if memory allocation fails, instead of using goto and returning a NULL pointer, and remove the now-unused 'out' label.

At the call site, check 'ascii_buf' with IS_ERR() and propagate the error code returned by datablob_format().

---
 security/keys/encrypted-keys/encrypted.c | 7 +++----
 1 file changed, 3 insertions(+), 4 deletions(-)
security/keys/encrypted-keys/encrypted.c +3 -4
index be1f2118447c..25df00b7dbe9 100644
@@ -276,7 +276,7 @@ static char *datablob_format(struct encrypted_key_payload *epayload,
 
 	ascii_buf = kmalloc(asciiblob_len + 1, GFP_KERNEL);
 	if (!ascii_buf)
-		goto out;
+		return ERR_PTR(-ENOMEM);
 
 	ascii_buf[asciiblob_len] = '\0';
 
@@ -288,7 +288,6 @@ static char *datablob_format(struct encrypted_key_payload *epayload,
 	bufp = &ascii_buf[len];
 	for (i = 0; i < (asciiblob_len - len) / 2; i++)
 		bufp = hex_byte_pack(bufp, iv[i]);
-out:
 	return ascii_buf;
 }
 
@@ -932,8 +931,8 @@ static long encrypted_read(const struct key *key, char *buffer,
 		goto out;
 
 	ascii_buf = datablob_format(epayload, asciiblob_len);
-	if (!ascii_buf) {
-		ret = -ENOMEM;
+	if (IS_ERR(ascii_buf)) {
+		ret = PTR_ERR(ascii_buf);
 		goto out;
 	}
 
Signature · 1 line
2.51.0

No thank you as this does not really fix anything.

BR, Jarkko