diff mbox series

ASoC: wcd934x: Remove some unnecessary NULL checks

Message ID 20200204060143.23393-1-natechancellor@gmail.com (mailing list archive)
State New, archived
Headers show
Series ASoC: wcd934x: Remove some unnecessary NULL checks | expand

Commit Message

Nathan Chancellor Feb. 4, 2020, 6:01 a.m. UTC
Clang warns:

../sound/soc/codecs/wcd934x.c:1886:11: warning: address of array
'wcd->rx_chs' will always evaluate to 'true' [-Wpointer-bool-conversion]
        if (wcd->rx_chs) {
        ~~  ~~~~~^~~~~~
../sound/soc/codecs/wcd934x.c:1894:11: warning: address of array
'wcd->tx_chs' will always evaluate to 'true' [-Wpointer-bool-conversion]
        if (wcd->tx_chs) {
        ~~  ~~~~~^~~~~~
2 warnings generated.

Arrays that are in the middle of a struct are never NULL so they don't
need a check like this.

Fixes: a61f3b4f476e ("ASoC: wcd934x: add support to wcd9340/wcd9341 codec")
Link: https://github.com/ClangBuiltLinux/linux/issues/854
Signed-off-by: Nathan Chancellor <natechancellor@gmail.com>
---

Also, turns out this was fixed in the wcd9335 driver in
commit d22b4117538d ("ASoC: wcd9335: remove some unnecessary
NULL checks")...

 sound/soc/codecs/wcd934x.c | 20 ++++++++------------
 1 file changed, 8 insertions(+), 12 deletions(-)

Comments

Mark Brown Feb. 4, 2020, 10 a.m. UTC | #1
On Mon, Feb 03, 2020 at 11:01:44PM -0700, Nathan Chancellor wrote:
> Clang warns:
> 
> ../sound/soc/codecs/wcd934x.c:1886:11: warning: address of array
> 'wcd->rx_chs' will always evaluate to 'true' [-Wpointer-bool-conversion]
>         if (wcd->rx_chs) {
>         ~~  ~~~~~^~~~~~
> ../sound/soc/codecs/wcd934x.c:1894:11: warning: address of array
> 'wcd->tx_chs' will always evaluate to 'true' [-Wpointer-bool-conversion]
>         if (wcd->tx_chs) {
>         ~~  ~~~~~^~~~~~
> 2 warnings generated.
> 
> Arrays that are in the middle of a struct are never NULL so they don't
> need a check like this.

I'm not convincd this is a sensible warning, at the use site a
pointer to an array in a struct looks identical to an array
embedded in the struct so it's not such a bad idea to check and
refactoring of the struct could easily introduce problems.
Nathan Chancellor Feb. 4, 2020, 7:32 p.m. UTC | #2
On Tue, Feb 04, 2020 at 10:00:39AM +0000, Mark Brown wrote:
> On Mon, Feb 03, 2020 at 11:01:44PM -0700, Nathan Chancellor wrote:
> > Clang warns:
> > 
> > ../sound/soc/codecs/wcd934x.c:1886:11: warning: address of array
> > 'wcd->rx_chs' will always evaluate to 'true' [-Wpointer-bool-conversion]
> >         if (wcd->rx_chs) {
> >         ~~  ~~~~~^~~~~~
> > ../sound/soc/codecs/wcd934x.c:1894:11: warning: address of array
> > 'wcd->tx_chs' will always evaluate to 'true' [-Wpointer-bool-conversion]
> >         if (wcd->tx_chs) {
> >         ~~  ~~~~~^~~~~~
> > 2 warnings generated.
> > 
> > Arrays that are in the middle of a struct are never NULL so they don't
> > need a check like this.
> 
> I'm not convincd this is a sensible warning, at the use site a
> pointer to an array in a struct looks identical to an array
> embedded in the struct so it's not such a bad idea to check and
> refactoring of the struct could easily introduce problems.

There have been a few other bugs found with this warning:

9fcf2b3c1c02 ("drm/atmel-hlcdc: check stride values in the first plane")
44d7f1a77d8c ("media: pxa_camera: Fix check for pdev->dev.of_node")
8a72b81e6df5 ("isdn: isdnloop: fix pointer dereference bug")

Other static checkers like smatch will warn about this as well (since I
am sure that is how Dan Carpenter found the same issue in the wcd9335
driver). Isn't an antipattern in the kernel to do things "just in
case we do something later"? There are plenty of NULL checks removed
from the kernel because they do not do anything now.

I'd be fine with changing the check to something else that keeps the
same logic but doesn't create a warning; I am not exactly sure what that
would be because that is more of a specific driver logic thing, which I
am not familiar with.

Cheers,
Nathan
Mark Brown Feb. 5, 2020, 10:22 a.m. UTC | #3
On Tue, Feb 04, 2020 at 12:32:15PM -0700, Nathan Chancellor wrote:
> On Tue, Feb 04, 2020 at 10:00:39AM +0000, Mark Brown wrote:

> > I'm not convincd this is a sensible warning, at the use site a
> > pointer to an array in a struct looks identical to an array
> > embedded in the struct so it's not such a bad idea to check and
> > refactoring of the struct could easily introduce problems.

> Other static checkers like smatch will warn about this as well (since I
> am sure that is how Dan Carpenter found the same issue in the wcd9335
> driver). Isn't an antipattern in the kernel to do things "just in
> case we do something later"? There are plenty of NULL checks removed
> from the kernel because they do not do anything now.

I'm not convinced it is an antipattern - adding the checks would
be a bit silly but with the way C works the warnings feel like
false positives.  If the compiler were able to warn about missing
NULL checks in the case where the thing in the struct is a
pointer I'd be a lot happier with this.

> I'd be fine with changing the check to something else that keeps the
> same logic but doesn't create a warning; I am not exactly sure what that
> would be because that is more of a specific driver logic thing, which I
> am not familiar with.

I've queued the change to be applied since it's shuts the
compiler up but I'm really not convinced the compiler is helping
here.
Nathan Chancellor Feb. 5, 2020, 3:50 p.m. UTC | #4
On Wed, Feb 05, 2020 at 10:22:38AM +0000, Mark Brown wrote:
> On Tue, Feb 04, 2020 at 12:32:15PM -0700, Nathan Chancellor wrote:
> > On Tue, Feb 04, 2020 at 10:00:39AM +0000, Mark Brown wrote:
> 
> > > I'm not convincd this is a sensible warning, at the use site a
> > > pointer to an array in a struct looks identical to an array
> > > embedded in the struct so it's not such a bad idea to check and
> > > refactoring of the struct could easily introduce problems.
> 
> > Other static checkers like smatch will warn about this as well (since I
> > am sure that is how Dan Carpenter found the same issue in the wcd9335
> > driver). Isn't an antipattern in the kernel to do things "just in
> > case we do something later"? There are plenty of NULL checks removed
> > from the kernel because they do not do anything now.
> 
> I'm not convinced it is an antipattern - adding the checks would
> be a bit silly but with the way C works the warnings feel like
> false positives.  If the compiler were able to warn about missing
> NULL checks in the case where the thing in the struct is a
> pointer I'd be a lot happier with this.

Yes, that would definitely be nice. I am not entirely sure that this is
possible with clang due to its architecture but I am far from a clang
internal expert.

> > I'd be fine with changing the check to something else that keeps the
> > same logic but doesn't create a warning; I am not exactly sure what that
> > would be because that is more of a specific driver logic thing, which I
> > am not familiar with.
> 
> I've queued the change to be applied since it's shuts the
> compiler up but I'm really not convinced the compiler is helping
> here.

Thank you :)

Cheers,
Nathan
diff mbox series

Patch

diff --git a/sound/soc/codecs/wcd934x.c b/sound/soc/codecs/wcd934x.c
index 158e878abd6c..e780ecd554d2 100644
--- a/sound/soc/codecs/wcd934x.c
+++ b/sound/soc/codecs/wcd934x.c
@@ -1883,20 +1883,16 @@  static int wcd934x_set_channel_map(struct snd_soc_dai *dai,
 		return -EINVAL;
 	}
 
-	if (wcd->rx_chs) {
-		wcd->num_rx_port = rx_num;
-		for (i = 0; i < rx_num; i++) {
-			wcd->rx_chs[i].ch_num = rx_slot[i];
-			INIT_LIST_HEAD(&wcd->rx_chs[i].list);
-		}
+	wcd->num_rx_port = rx_num;
+	for (i = 0; i < rx_num; i++) {
+		wcd->rx_chs[i].ch_num = rx_slot[i];
+		INIT_LIST_HEAD(&wcd->rx_chs[i].list);
 	}
 
-	if (wcd->tx_chs) {
-		wcd->num_tx_port = tx_num;
-		for (i = 0; i < tx_num; i++) {
-			wcd->tx_chs[i].ch_num = tx_slot[i];
-			INIT_LIST_HEAD(&wcd->tx_chs[i].list);
-		}
+	wcd->num_tx_port = tx_num;
+	for (i = 0; i < tx_num; i++) {
+		wcd->tx_chs[i].ch_num = tx_slot[i];
+		INIT_LIST_HEAD(&wcd->tx_chs[i].list);
 	}
 
 	return 0;