Linux wireless drivers development
 help / color / mirror / Atom feed
From: Colin Ian King <colin.king@canonical.com>
To: Johannes Berg <johannes@sipsolutions.net>
Cc: "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	"linux-wireless@vger.kernel.org" <linux-wireless@vger.kernel.org>,
	"netdev@vger.kernel.org" <netdev@vger.kernel.org>
Subject: Re: Potential invalid ~ operator in net/mac80211/cfg.c
Date: Fri, 5 Feb 2021 18:19:10 +0000	[thread overview]
Message-ID: <1383c6f1-1317-daed-ecc7-e5cc3f309c41@canonical.com> (raw)
In-Reply-To: <15f435a791b0c4b853c8c6b284042c7057d6efaf.camel@sipsolutions.net>

On 05/02/2021 18:05, Johannes Berg wrote:
> Hi Colin,
> 
>> while working through a backlog of older static analysis reports from
>> Coverity
> 
> So ... yeah. Every time I look at Coverity (not frequently, I must
> admit) I see the same thing, and get confused.
> 
>> I found an interesting use of the ~ operator that looks
>> incorrect to me in function ieee80211_set_bitrate_mask():
>>
>>                 for (j = 0; j < IEEE80211_HT_MCS_MASK_LEN; j++) {
>>                         if (~sdata->rc_rateidx_mcs_mask[i][j]) {
>>                                 sdata->rc_has_mcs_mask[i] = true;
>>                                 break;
>>                         }
>>                 }
>>
>>                 for (j = 0; j < NL80211_VHT_NSS_MAX; j++) {
>>                         if (~sdata->rc_rateidx_vht_mcs_mask[i][j]) {
>>                                 sdata->rc_has_vht_mcs_mask[i] = true;
>>                                 break;
>>                         }
>>                 }
>>
>> For the ~ operator in both if stanzas, Coverity reports:
>>
>> Logical vs. bitwise operator (CONSTANT_EXPRESSION_RESULT)
>> logical_vs_bitwise:
>>
>> ~sdata->rc_rateidx_mcs_mask[i][j] is always 1/true regardless of the
>> values of its operand. This occurs as the logical operand of if.
>>     Did you intend to use ! rather than ~?
>>
>> I've checked the results of this and it does seem that ~ is incorrect
>> and always returns true for the if expression. So it probably should be
>> !, but I'm not sure if I'm missing something deeper here and wondering
>> why this has always worked.
> 
> But is it really always true?
> 
> I _think_ it was intended to check that it's not 0xffffffff or
> something?
> 
> https://lore.kernel.org/linux-wireless/516C0C7F.3000204@openwrt.org/
> 
> But maybe that isn't actually quite right due to integer promotion?
> OTOH, that's a u8, so it should do the ~ in u8 space, and then compare
> to 0 also?

rc_rateidx_vht_mcs_mask is a u64, so I think the expression could be
expressed as:

if ((uint16_t)~sdata->rc_rateidx_mcs_mask[i][j]) ..

this is only true if all the 16 bits in the mask are 0xffff

> 
> johannes
> 


  reply	other threads:[~2021-02-05 18:22 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-05 17:38 Potential invalid ~ operator in net/mac80211/cfg.c Colin Ian King
2021-02-05 18:05 ` Johannes Berg
2021-02-05 18:19   ` Colin Ian King [this message]
2021-02-05 18:20     ` Colin Ian King
2021-02-12 10:18       ` Johannes Berg

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1383c6f1-1317-daed-ecc7-e5cc3f309c41@canonical.com \
    --to=colin.king@canonical.com \
    --cc=davem@davemloft.net \
    --cc=johannes@sipsolutions.net \
    --cc=kuba@kernel.org \
    --cc=linux-wireless@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox