2019-09-30 08:53:12

by tiantao (H)

[permalink] [raw]
Subject: [PATCH] crypto: fix comparison of unsigned expression warnings

This patch fixes the following warnings:
drivers/crypto/ccree/cc_aead.c:630:5-12: WARNING: Unsigned expression
compared with zero: seq_len > 0

Signed-off-by: Tian Tao <[email protected]>
---
drivers/crypto/ccree/cc_aead.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/crypto/ccree/cc_aead.c b/drivers/crypto/ccree/cc_aead.c
index d3e8faa..b19291d 100644
--- a/drivers/crypto/ccree/cc_aead.c
+++ b/drivers/crypto/ccree/cc_aead.c
@@ -546,7 +546,7 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
struct cc_crypto_req cc_req = {};
struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
- unsigned int seq_len = 0;
+ int seq_len = 0;
struct device *dev = drvdata_to_dev(ctx->drvdata);
const u8 *enckey, *authkey;
int rc;
--
2.7.4


2019-09-30 14:18:15

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH] crypto: fix comparison of unsigned expression warnings

On Mon, 30 Sep 2019 16:49:21 +0800
Tian Tao <[email protected]> wrote:

> This patch fixes the following warnings:
> drivers/crypto/ccree/cc_aead.c:630:5-12: WARNING: Unsigned expression
> compared with zero: seq_len > 0
>
> Signed-off-by: Tian Tao <[email protected]>

Apologies, I should have looked into this in more depth when you asked
me about it earlier rather than assuming it was 'obviously' the right
fix.

It's more complex than I expected given the warning, which I note
is > 0 so it's not always true. I'm curious, which compiler generates
that warning?

So there are two ways seq_len can be set to non 0, hmac_setkey which returns a
signed int, but one that is reality is >= 0. The other is xcbc_setkey
which returns an unsigned int.

So I would suggest that in addition to what you have here, a change
to the return type of hmac_setkey in order to make it clear that
never returns a negative anyway.

Can also use if (seq_len)
rather than if (seq_len > 0)

Thanks,

Jonathan



> ---
> drivers/crypto/ccree/cc_aead.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/crypto/ccree/cc_aead.c b/drivers/crypto/ccree/cc_aead.c
> index d3e8faa..b19291d 100644
> --- a/drivers/crypto/ccree/cc_aead.c
> +++ b/drivers/crypto/ccree/cc_aead.c
> @@ -546,7 +546,7 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
> struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
> struct cc_crypto_req cc_req = {};
> struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
> - unsigned int seq_len = 0;
> + int seq_len = 0;
> struct device *dev = drvdata_to_dev(ctx->drvdata);
> const u8 *enckey, *authkey;
> int rc;


2019-09-30 14:46:47

by Jonathan Cameron

[permalink] [raw]
Subject: Re: [PATCH] crypto: fix comparison of unsigned expression warnings

On Mon, 30 Sep 2019 15:17:02 +0100
Jonathan Cameron <[email protected]> wrote:

> On Mon, 30 Sep 2019 16:49:21 +0800
> Tian Tao <[email protected]> wrote:
>
> > This patch fixes the following warnings:
> > drivers/crypto/ccree/cc_aead.c:630:5-12: WARNING: Unsigned expression
> > compared with zero: seq_len > 0
> >
> > Signed-off-by: Tian Tao <[email protected]>
>
> Apologies, I should have looked into this in more depth when you asked
> me about it earlier rather than assuming it was 'obviously' the right
> fix.
>
> It's more complex than I expected given the warning, which I note
> is > 0 so it's not always true. I'm curious, which compiler generates
> that warning?
>
> So there are two ways seq_len can be set to non 0, hmac_setkey which returns a
> signed int, but one that is reality is >= 0. The other is xcbc_setkey
> which returns an unsigned int.
>
> So I would suggest that in addition to what you have here, a change
> to the return type of hmac_setkey in order to make it clear that
> never returns a negative anyway.

Hmm. Perhaps I shouldn't review with jetlag... That should have said
that I think the variable should be left unsigned as reality is that
it is always >= 0.

>
> Can also use if (seq_len)
> rather than if (seq_len > 0)
>
> Thanks,
>
> Jonathan
>
>
>
> > ---
> > drivers/crypto/ccree/cc_aead.c | 2 +-
> > 1 file changed, 1 insertion(+), 1 deletion(-)
> >
> > diff --git a/drivers/crypto/ccree/cc_aead.c b/drivers/crypto/ccree/cc_aead.c
> > index d3e8faa..b19291d 100644
> > --- a/drivers/crypto/ccree/cc_aead.c
> > +++ b/drivers/crypto/ccree/cc_aead.c
> > @@ -546,7 +546,7 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
> > struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
> > struct cc_crypto_req cc_req = {};
> > struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
> > - unsigned int seq_len = 0;
> > + int seq_len = 0;
> > struct device *dev = drvdata_to_dev(ctx->drvdata);
> > const u8 *enckey, *authkey;
> > int rc;
>


2019-10-02 12:05:02

by Gilad Ben-Yossef

[permalink] [raw]
Subject: Re: [PATCH] crypto: fix comparison of unsigned expression warnings

Hi,


On Mon, Sep 30, 2019 at 11:52 AM Tian Tao <[email protected]> wrote:
>
> This patch fixes the following warnings:
> drivers/crypto/ccree/cc_aead.c:630:5-12: WARNING: Unsigned expression
> compared with zero: seq_len > 0
>

Thanks for the report!

Can you please share which compiler/arch/config you use that produces
this warning?

I'm not seeing it on my end.

Many thanks,
Gilad

> Signed-off-by: Tian Tao <[email protected]>
> ---
> drivers/crypto/ccree/cc_aead.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/crypto/ccree/cc_aead.c b/drivers/crypto/ccree/cc_aead.c
> index d3e8faa..b19291d 100644
> --- a/drivers/crypto/ccree/cc_aead.c
> +++ b/drivers/crypto/ccree/cc_aead.c
> @@ -546,7 +546,7 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
> struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
> struct cc_crypto_req cc_req = {};
> struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
> - unsigned int seq_len = 0;
> + int seq_len = 0;
> struct device *dev = drvdata_to_dev(ctx->drvdata);
> const u8 *enckey, *authkey;
> int rc;
> --
> 2.7.4
>


--
Gilad Ben-Yossef
Chief Coffee Drinker

values of β will give rise to dom!

2019-10-08 01:02:36

by tiantao (H)

[permalink] [raw]
Subject: 答复: [PATCH] crypto: fix comparison of uns igned expression warnings

Hi,

I found this warning using the command "make coccicheck COCCI=scripts/coccinelle/tests/unsigned_lesser_than_zero.cocci MODE=patch"

Best

-----邮件原件-----
发件人: Gilad Ben-Yossef [mailto:[email protected]]
发送时间: 2019年10月2日 18:00
收件人: tiantao (H) <[email protected]>
抄送: Herbert Xu <[email protected]>; David Miller <[email protected]>; Linux Crypto Mailing List <[email protected]>; Linuxarm <[email protected]>
主题: Re: [PATCH] crypto: fix comparison of unsigned expression warnings

Hi,


On Mon, Sep 30, 2019 at 11:52 AM Tian Tao <[email protected]> wrote:
>
> This patch fixes the following warnings:
> drivers/crypto/ccree/cc_aead.c:630:5-12: WARNING: Unsigned expression
> compared with zero: seq_len > 0
>

Thanks for the report!

Can you please share which compiler/arch/config you use that produces this warning?

I'm not seeing it on my end.

Many thanks,
Gilad

> Signed-off-by: Tian Tao <[email protected]>
> ---
> drivers/crypto/ccree/cc_aead.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/crypto/ccree/cc_aead.c
> b/drivers/crypto/ccree/cc_aead.c index d3e8faa..b19291d 100644
> --- a/drivers/crypto/ccree/cc_aead.c
> +++ b/drivers/crypto/ccree/cc_aead.c
> @@ -546,7 +546,7 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
> struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
> struct cc_crypto_req cc_req = {};
> struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
> - unsigned int seq_len = 0;
> + int seq_len = 0;
> struct device *dev = drvdata_to_dev(ctx->drvdata);
> const u8 *enckey, *authkey;
> int rc;
> --
> 2.7.4
>


--
Gilad Ben-Yossef
Chief Coffee Drinker

values of β will give rise to dom!

2019-10-18 16:12:29

by Gilad Ben-Yossef

[permalink] [raw]
Subject: Re: [PATCH] crypto: fix comparison of unsigned expression warnings

On Mon, Sep 30, 2019 at 11:52 AM Tian Tao <[email protected]> wrote:
>
> This patch fixes the following warnings:
> drivers/crypto/ccree/cc_aead.c:630:5-12: WARNING: Unsigned expression
> compared with zero: seq_len > 0

Thank you very much for the patch. Please accept my apologies that it
took me some time to respond.

I'm sorry, but I don't think this is the right solution here:

seq_len here can never get a negative value so the warning is a false one.

Having said that, I suspect it would be better code to:
1. change hmac_setkey() return type to unsigned int as well, as it
doesn't ever return a negative number.
2. change the predicate in the if statement the warning mentions to
seq_len != 0.

That would be a clearer code and possibly will also silence the false warning

So NACK for this one but I would accept a patch doing he above if you send one.

Thanks again for taking the time to do this.

Gilad

>
> Signed-off-by: Tian Tao <[email protected]>
> ---
> drivers/crypto/ccree/cc_aead.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/crypto/ccree/cc_aead.c b/drivers/crypto/ccree/cc_aead.c
> index d3e8faa..b19291d 100644
> --- a/drivers/crypto/ccree/cc_aead.c
> +++ b/drivers/crypto/ccree/cc_aead.c
> @@ -546,7 +546,7 @@ static int cc_aead_setkey(struct crypto_aead *tfm, const u8 *key,
> struct cc_aead_ctx *ctx = crypto_aead_ctx(tfm);
> struct cc_crypto_req cc_req = {};
> struct cc_hw_desc desc[MAX_AEAD_SETKEY_SEQ];
> - unsigned int seq_len = 0;
> + int seq_len = 0;
> struct device *dev = drvdata_to_dev(ctx->drvdata);
> const u8 *enckey, *authkey;
> int rc;
> --
> 2.7.4
>


--
Gilad Ben-Yossef
Chief Coffee Drinker

values of β will give rise to dom!