linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1
@ 2017-08-23 15:39 Colin King
  2017-08-24  8:48 ` Aviad Krawczyk
  2017-08-24 21:32 ` David Miller
  0 siblings, 2 replies; 6+ messages in thread
From: Colin King @ 2017-08-23 15:39 UTC (permalink / raw)
  To: Aviad Krawczyk, netdev; +Cc: kernel-janitors, linux-kernel

From: Colin Ian King <colin.king@canonical.com>

The comparison of hw_ioctxt.rx_buf_sz_idx == -1 is always false because
rx_buf_sz_idx is a uint16_t. Fix this by explicitly casting -1 to uint16_t.

Detected by CoverityScan, CID#1454559 ("Operands don't affect result")

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
index 09dec6de8dd5..71e26070fb7f 100644
--- a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
+++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
@@ -352,7 +352,7 @@ static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
 		}
 	}
 
-	if (hw_ioctxt.rx_buf_sz_idx == -1)
+	if (hw_ioctxt.rx_buf_sz_idx == (uint16_t)-1)
 		return -EINVAL;
 
 	hw_ioctxt.sq_depth  = ilog2(sq_depth);
-- 
2.14.1

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1
  2017-08-23 15:39 [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1 Colin King
@ 2017-08-24  8:48 ` Aviad Krawczyk
  2017-08-24  8:54   ` Colin Ian King
  2017-08-24 21:32 ` David Miller
  1 sibling, 1 reply; 6+ messages in thread
From: Aviad Krawczyk @ 2017-08-24  8:48 UTC (permalink / raw)
  To: Colin King, netdev; +Cc: kernel-janitors, linux-kernel

On 8/23/2017 6:39 PM, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The comparison of hw_ioctxt.rx_buf_sz_idx == -1 is always false because
> rx_buf_sz_idx is a uint16_t. Fix this by explicitly casting -1 to uint16_t.
> 
> Detected by CoverityScan, CID#1454559 ("Operands don't affect result")
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> index 09dec6de8dd5..71e26070fb7f 100644
> --- a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> +++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> @@ -352,7 +352,7 @@ static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
>  		}
>  	}
>  
> -	if (hw_ioctxt.rx_buf_sz_idx == -1)
> +	if (hw_ioctxt.rx_buf_sz_idx == (uint16_t)-1)
>  		return -EINVAL;
>  
>  	hw_ioctxt.sq_depth  = ilog2(sq_depth);
> 

Many thanks, Colin.
I prefer to avoid casting when possible, what do you think about replacing the condition by:

if (rx_buf_sz_table[i].sz != HINIC_RX_BUF_SZ)
	return -EINVAL;

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1
  2017-08-24  8:48 ` Aviad Krawczyk
@ 2017-08-24  8:54   ` Colin Ian King
  2017-08-24  9:29     ` Dan Carpenter
  0 siblings, 1 reply; 6+ messages in thread
From: Colin Ian King @ 2017-08-24  8:54 UTC (permalink / raw)
  To: Aviad Krawczyk, netdev; +Cc: kernel-janitors, linux-kernel

On 24/08/17 09:48, Aviad Krawczyk wrote:
> On 8/23/2017 6:39 PM, Colin King wrote:
>> From: Colin Ian King <colin.king@canonical.com>
>>
>> The comparison of hw_ioctxt.rx_buf_sz_idx == -1 is always false because
>> rx_buf_sz_idx is a uint16_t. Fix this by explicitly casting -1 to uint16_t.
>>
>> Detected by CoverityScan, CID#1454559 ("Operands don't affect result")
>>
>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>> ---
>>  drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c | 2 +-
>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>
>> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
>> index 09dec6de8dd5..71e26070fb7f 100644
>> --- a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
>> +++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
>> @@ -352,7 +352,7 @@ static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
>>  		}
>>  	}
>>  
>> -	if (hw_ioctxt.rx_buf_sz_idx == -1)
>> +	if (hw_ioctxt.rx_buf_sz_idx == (uint16_t)-1)
>>  		return -EINVAL;
>>  
>>  	hw_ioctxt.sq_depth  = ilog2(sq_depth);
>>
> 
> Many thanks, Colin.
> I prefer to avoid casting when possible, what do you think about replacing the condition by:
> 
> if (rx_buf_sz_table[i].sz != HINIC_RX_BUF_SZ)
> 	return -EINVAL;
> 

Does that work as expected when rx_buf_sz_table[i].sz == -1?

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1
  2017-08-24  8:54   ` Colin Ian King
@ 2017-08-24  9:29     ` Dan Carpenter
  2017-08-24  9:38       ` Aviad Krawczyk
  0 siblings, 1 reply; 6+ messages in thread
From: Dan Carpenter @ 2017-08-24  9:29 UTC (permalink / raw)
  To: Colin Ian King; +Cc: Aviad Krawczyk, netdev, kernel-janitors, linux-kernel

On Thu, Aug 24, 2017 at 09:54:03AM +0100, Colin Ian King wrote:
> On 24/08/17 09:48, Aviad Krawczyk wrote:
> > On 8/23/2017 6:39 PM, Colin King wrote:
> >> From: Colin Ian King <colin.king@canonical.com>
> >>
> >> The comparison of hw_ioctxt.rx_buf_sz_idx == -1 is always false because
> >> rx_buf_sz_idx is a uint16_t. Fix this by explicitly casting -1 to uint16_t.
> >>
> >> Detected by CoverityScan, CID#1454559 ("Operands don't affect result")
> >>
> >> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> >> ---
> >>  drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c | 2 +-
> >>  1 file changed, 1 insertion(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> >> index 09dec6de8dd5..71e26070fb7f 100644
> >> --- a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> >> +++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> >> @@ -352,7 +352,7 @@ static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
> >>  		}
> >>  	}
> >>  
> >> -	if (hw_ioctxt.rx_buf_sz_idx == -1)
> >> +	if (hw_ioctxt.rx_buf_sz_idx == (uint16_t)-1)
> >>  		return -EINVAL;
> >>  
> >>  	hw_ioctxt.sq_depth  = ilog2(sq_depth);
> >>
> > 
> > Many thanks, Colin.
> > I prefer to avoid casting when possible, what do you think about replacing the condition by:
> > 
> > if (rx_buf_sz_table[i].sz != HINIC_RX_BUF_SZ)
> > 	return -EINVAL;
> > 
> 
> Does that work as expected when rx_buf_sz_table[i].sz == -1?

No it doesn't.  Please, don't ask rhetorical questions.  I have a
toddler and I constantly ask him toddler level questions and it drives
me nuts that all the adults in the room will answer me...  "Yes, I
already know that's a cow.  I was quizing my son.  But thank you!"
Meanwhile I can't resist answering questions myself...

The code looks like this:

drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
   345          hw_ioctxt.rq_depth  = ilog2(rq_depth);
   346  
   347          for (i = 0; ; i++) {
   348                  if ((rx_buf_sz_table[i].sz == HINIC_RX_BUF_SZ) ||
   349                      (rx_buf_sz_table[i].sz == -1)) {
   350                          hw_ioctxt.rx_buf_sz_idx = rx_buf_sz_table[i].idx;
   351                          break;
   352                  }
   353          }
   354  
   355          if (hw_ioctxt.rx_buf_sz_idx == -1)
   356                  return -EINVAL;
   357  

The loop doesn't make sense.  We are looping through rx_buf_sz_table[]
until we hit 2048 or -1.  But 2048 comes first so we always get there
and break.

We may as well replace all that code with:

		hw_ioctxt.rx_buf_sz_idx = 11;

Something is very wrong.

regards,
dan carpenter

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1
  2017-08-24  9:29     ` Dan Carpenter
@ 2017-08-24  9:38       ` Aviad Krawczyk
  0 siblings, 0 replies; 6+ messages in thread
From: Aviad Krawczyk @ 2017-08-24  9:38 UTC (permalink / raw)
  To: Dan Carpenter, Colin Ian King; +Cc: netdev, kernel-janitors, linux-kernel

On 8/24/2017 12:29 PM, Dan Carpenter wrote:
> On Thu, Aug 24, 2017 at 09:54:03AM +0100, Colin Ian King wrote:
>> On 24/08/17 09:48, Aviad Krawczyk wrote:
>>> On 8/23/2017 6:39 PM, Colin King wrote:
>>>> From: Colin Ian King <colin.king@canonical.com>
>>>>
>>>> The comparison of hw_ioctxt.rx_buf_sz_idx == -1 is always false because
>>>> rx_buf_sz_idx is a uint16_t. Fix this by explicitly casting -1 to uint16_t.
>>>>
>>>> Detected by CoverityScan, CID#1454559 ("Operands don't affect result")
>>>>
>>>> Signed-off-by: Colin Ian King <colin.king@canonical.com>
>>>> ---
>>>>  drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c | 2 +-
>>>>  1 file changed, 1 insertion(+), 1 deletion(-)
>>>>
>>>> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
>>>> index 09dec6de8dd5..71e26070fb7f 100644
>>>> --- a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
>>>> +++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
>>>> @@ -352,7 +352,7 @@ static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
>>>>  		}
>>>>  	}
>>>>  
>>>> -	if (hw_ioctxt.rx_buf_sz_idx == -1)
>>>> +	if (hw_ioctxt.rx_buf_sz_idx == (uint16_t)-1)
>>>>  		return -EINVAL;
>>>>  
>>>>  	hw_ioctxt.sq_depth  = ilog2(sq_depth);
>>>>
>>>
>>> Many thanks, Colin.
>>> I prefer to avoid casting when possible, what do you think about replacing the condition by:
>>>
>>> if (rx_buf_sz_table[i].sz != HINIC_RX_BUF_SZ)
>>> 	return -EINVAL;
>>>
>>
>> Does that work as expected when rx_buf_sz_table[i].sz == -1?
> 
> No it doesn't.  Please, don't ask rhetorical questions.  I have a
> toddler and I constantly ask him toddler level questions and it drives
> me nuts that all the adults in the room will answer me...  "Yes, I
> already know that's a cow.  I was quizing my son.  But thank you!"
> Meanwhile I can't resist answering questions myself...
> 
> The code looks like this:
> 
> drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
>    345          hw_ioctxt.rq_depth  = ilog2(rq_depth);
>    346  
>    347          for (i = 0; ; i++) {
>    348                  if ((rx_buf_sz_table[i].sz == HINIC_RX_BUF_SZ) ||
>    349                      (rx_buf_sz_table[i].sz == -1)) {
>    350                          hw_ioctxt.rx_buf_sz_idx = rx_buf_sz_table[i].idx;
>    351                          break;
>    352                  }
>    353          }
>    354  
>    355          if (hw_ioctxt.rx_buf_sz_idx == -1)
>    356                  return -EINVAL;
>    357  
> 
> The loop doesn't make sense.  We are looping through rx_buf_sz_table[]
> until we hit 2048 or -1.  But 2048 comes first so we always get there
> and break.
> 
> We may as well replace all that code with:
> 
> 		hw_ioctxt.rx_buf_sz_idx = 11;
> 
> Something is very wrong.
> 
> regards,
> dan carpenter
> 
> 
> .
> 

Hi Dan,

What if HINIC_RX_BUF_SZ is changed to another value?
The test checks if the HINIC_RX_BUF_SZ is in the table, if not return -EINVAL.
Therefore I think the check of rx_buf_sz_table[i].sz != HINIC_RX_BUF_SZ is better.

Aviad

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1
  2017-08-23 15:39 [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1 Colin King
  2017-08-24  8:48 ` Aviad Krawczyk
@ 2017-08-24 21:32 ` David Miller
  1 sibling, 0 replies; 6+ messages in thread
From: David Miller @ 2017-08-24 21:32 UTC (permalink / raw)
  To: colin.king; +Cc: aviad.krawczyk, netdev, kernel-janitors, linux-kernel

From: Colin King <colin.king@canonical.com>
Date: Wed, 23 Aug 2017 16:39:36 +0100

> From: Colin Ian King <colin.king@canonical.com>
> 
> The comparison of hw_ioctxt.rx_buf_sz_idx == -1 is always false because
> rx_buf_sz_idx is a uint16_t. Fix this by explicitly casting -1 to uint16_t.
> 
> Detected by CoverityScan, CID#1454559 ("Operands don't affect result")
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>  drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> index 09dec6de8dd5..71e26070fb7f 100644
> --- a/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> +++ b/drivers/net/ethernet/huawei/hinic/hinic_hw_dev.c
> @@ -352,7 +352,7 @@ static int set_hw_ioctxt(struct hinic_hwdev *hwdev, unsigned int rq_depth,
>  		}
>  	}
>  
> -	if (hw_ioctxt.rx_buf_sz_idx == -1)
> +	if (hw_ioctxt.rx_buf_sz_idx == (uint16_t)-1)
>  		return -EINVAL;
>  
>  	hw_ioctxt.sq_depth  = ilog2(sq_depth);

This is really silly.

The code in question is trying to convert a size (HINIC_RX_BUF_SZ)
into a table index, using a loop.

It should just compute this at compile time and do away with this
overly confusing code.

Even a switch statement would be much better and the compiler
would optimize the whole away into a single assignment in
the generated code.

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2017-08-24 21:32 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-08-23 15:39 [PATCH][next] net: hinic: fix comparison of a uint16_t type with -1 Colin King
2017-08-24  8:48 ` Aviad Krawczyk
2017-08-24  8:54   ` Colin Ian King
2017-08-24  9:29     ` Dan Carpenter
2017-08-24  9:38       ` Aviad Krawczyk
2017-08-24 21:32 ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).