diff mbox

crypto: rsa - remove unneeded initializations

Message ID 20180326115906.2076-1-tudor.ambarus@microchip.com (mailing list archive)
State Rejected
Delegated to: Herbert Xu
Headers show

Commit Message

Tudor Ambarus March 26, 2018, 11:59 a.m. UTC
Remove useless assignment of ret to -ENOMEM in rsa_verify.
Remove useless initialization of ret to zero at declaration in
rsa_enc/dec/sign/verify.

Benefit of the power of undefined values and set ret in branches in
rsa_enc/dec/sign.

Reported-by: Benjamin Bales <techsupport@mycode.ai>
Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
---
 crypto/rsa.c | 24 +++++++++++++-----------
 1 file changed, 13 insertions(+), 11 deletions(-)

Comments

Herbert Xu March 30, 2018, 5:27 p.m. UTC | #1
On Mon, Mar 26, 2018 at 02:59:06PM +0300, Tudor Ambarus wrote:
> Remove useless assignment of ret to -ENOMEM in rsa_verify.
> Remove useless initialization of ret to zero at declaration in
> rsa_enc/dec/sign/verify.
> 
> Benefit of the power of undefined values and set ret in branches in
> rsa_enc/dec/sign.
> 
> Reported-by: Benjamin Bales <techsupport@mycode.ai>
> Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>

The existing code looks fine.  If anything we should move the
assignments out of the if clause.

Thanks,
Tudor Ambarus April 3, 2018, 6:01 a.m. UTC | #2
On 03/30/2018 08:27 PM, Herbert Xu wrote:
> On Mon, Mar 26, 2018 at 02:59:06PM +0300, Tudor Ambarus wrote:
>> Remove useless assignment of ret to -ENOMEM in rsa_verify.
>> Remove useless initialization of ret to zero at declaration in
>> rsa_enc/dec/sign/verify.
>>
>> Benefit of the power of undefined values and set ret in branches in
>> rsa_enc/dec/sign.
>>
>> Reported-by: Benjamin Bales <techsupport@mycode.ai>
>> Signed-off-by: Tudor Ambarus <tudor.ambarus@microchip.com>
> 
> The existing code looks fine.  If anything we should move the
> assignments out of the if clause.

I set the err inside the if branch so that the compiler will
warn me in case of undefined value for err. Like here:

https://rusty.ozlabs.org/?p=232

Best,
ta
Herbert Xu April 3, 2018, 7:54 a.m. UTC | #3
On Tue, Apr 03, 2018 at 09:01:02AM +0300, Tudor Ambarus wrote:
>
> I set the err inside the if branch so that the compiler will
> warn me in case of undefined value for err. Like here:
> 
> https://rusty.ozlabs.org/?p=232

Yes but then sometimes the compiler will get it wrong and we will
then have to add the initialisation for it and we'll be back to
square one.

Cheers,
diff mbox

Patch

diff --git a/crypto/rsa.c b/crypto/rsa.c
index b067f3a..e75ce09 100644
--- a/crypto/rsa.c
+++ b/crypto/rsa.c
@@ -88,7 +88,7 @@  static int rsa_enc(struct akcipher_request *req)
 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
 	const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
 	MPI m, c = mpi_alloc(0);
-	int ret = 0;
+	int ret;
 	int sign;
 
 	if (!c)
@@ -99,10 +99,11 @@  static int rsa_enc(struct akcipher_request *req)
 		goto err_free_c;
 	}
 
-	ret = -ENOMEM;
 	m = mpi_read_raw_from_sgl(req->src, req->src_len);
-	if (!m)
+	if (!m) {
+		ret = -ENOMEM;
 		goto err_free_c;
+	}
 
 	ret = _rsa_enc(pkey, c, m);
 	if (ret)
@@ -127,7 +128,7 @@  static int rsa_dec(struct akcipher_request *req)
 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
 	const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
 	MPI c, m = mpi_alloc(0);
-	int ret = 0;
+	int ret;
 	int sign;
 
 	if (!m)
@@ -138,10 +139,11 @@  static int rsa_dec(struct akcipher_request *req)
 		goto err_free_m;
 	}
 
-	ret = -ENOMEM;
 	c = mpi_read_raw_from_sgl(req->src, req->src_len);
-	if (!c)
+	if (!c)	{
+		ret = -ENOMEM;
 		goto err_free_m;
+	}
 
 	ret = _rsa_dec(pkey, m, c);
 	if (ret)
@@ -165,7 +167,7 @@  static int rsa_sign(struct akcipher_request *req)
 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
 	const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
 	MPI m, s = mpi_alloc(0);
-	int ret = 0;
+	int ret;
 	int sign;
 
 	if (!s)
@@ -176,10 +178,11 @@  static int rsa_sign(struct akcipher_request *req)
 		goto err_free_s;
 	}
 
-	ret = -ENOMEM;
 	m = mpi_read_raw_from_sgl(req->src, req->src_len);
-	if (!m)
+	if (!m) {
+		ret = -ENOMEM;
 		goto err_free_s;
+	}
 
 	ret = _rsa_sign(pkey, s, m);
 	if (ret)
@@ -204,7 +207,7 @@  static int rsa_verify(struct akcipher_request *req)
 	struct crypto_akcipher *tfm = crypto_akcipher_reqtfm(req);
 	const struct rsa_mpi_key *pkey = rsa_get_key(tfm);
 	MPI s, m = mpi_alloc(0);
-	int ret = 0;
+	int ret;
 	int sign;
 
 	if (!m)
@@ -215,7 +218,6 @@  static int rsa_verify(struct akcipher_request *req)
 		goto err_free_m;
 	}
 
-	ret = -ENOMEM;
 	s = mpi_read_raw_from_sgl(req->src, req->src_len);
 	if (!s) {
 		ret = -ENOMEM;