diff mbox

ASoC: Intel: Skylake: Re-order some code to silence a warning

Message ID 20171208115425.rqclgmhcph5gn47j@mwanda (mailing list archive)
State Accepted
Commit 87684d338a22d15e47b16ee68f569d74ad1d076e
Headers show

Commit Message

Dan Carpenter Dec. 8, 2017, 11:54 a.m. UTC
I get a Smatch warning here:

    sound/soc/intel/skylake/skl-nhlt.c:335 skl_get_ssp_clks()
    error: testing array offset 'j' after use.

The code is harmless, but the checker is right that we should swap these
two conditions so we verify that the offset is within bounds before we
use it.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>

Comments

Sriram Periyasamy Dec. 11, 2017, 10:33 a.m. UTC | #1
On Mon, Dec 11, 2017 at 04:09:44PM +0530, Vinod Koul wrote:
> On Fri, Dec 08, 2017 at 02:54:25PM +0300, Dan Carpenter wrote:
> > I get a Smatch warning here:
> > 
> >     sound/soc/intel/skylake/skl-nhlt.c:335 skl_get_ssp_clks()
> >     error: testing array offset 'j' after use.
> > 
> > The code is harmless, but the checker is right that we should swap these
> > two conditions so we verify that the offset is within bounds before we
> > use it.
> 
> Acked-By: Vinod Koul <vinod.koul@intel.com>

Reviewed-by: Sriram Periyasamy <sriramx.periyasamy@intel.com>

> 
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > 
> > diff --git a/sound/soc/intel/skylake/skl-nhlt.c b/sound/soc/intel/skylake/skl-nhlt.c
> > index afa557a1c031..54f9bd630f4f 100644
> > --- a/sound/soc/intel/skylake/skl-nhlt.c
> > +++ b/sound/soc/intel/skylake/skl-nhlt.c
> > @@ -331,8 +331,8 @@ static void skl_get_ssp_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks,
> >  		rate = channels * bps * fs;
> >  
> >  		/* check if the rate is added already to the given SSP's sclk */
> > -		for (j = 0; (sclk[id].rate_cfg[j].rate != 0) &&
> > -				(j < SKL_MAX_CLK_RATES); j++) {
> > +		for (j = 0; (j < SKL_MAX_CLK_RATES) &&
> > +			    (sclk[id].rate_cfg[j].rate != 0); j++) {
> >  			if (sclk[id].rate_cfg[j].rate == rate) {
> >  				present = true;
> >  				break;
> 
> -- 
> ~Vinod
Vinod Koul Dec. 11, 2017, 10:39 a.m. UTC | #2
On Fri, Dec 08, 2017 at 02:54:25PM +0300, Dan Carpenter wrote:
> I get a Smatch warning here:
> 
>     sound/soc/intel/skylake/skl-nhlt.c:335 skl_get_ssp_clks()
>     error: testing array offset 'j' after use.
> 
> The code is harmless, but the checker is right that we should swap these
> two conditions so we verify that the offset is within bounds before we
> use it.

Acked-By: Vinod Koul <vinod.koul@intel.com>

> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> 
> diff --git a/sound/soc/intel/skylake/skl-nhlt.c b/sound/soc/intel/skylake/skl-nhlt.c
> index afa557a1c031..54f9bd630f4f 100644
> --- a/sound/soc/intel/skylake/skl-nhlt.c
> +++ b/sound/soc/intel/skylake/skl-nhlt.c
> @@ -331,8 +331,8 @@ static void skl_get_ssp_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks,
>  		rate = channels * bps * fs;
>  
>  		/* check if the rate is added already to the given SSP's sclk */
> -		for (j = 0; (sclk[id].rate_cfg[j].rate != 0) &&
> -				(j < SKL_MAX_CLK_RATES); j++) {
> +		for (j = 0; (j < SKL_MAX_CLK_RATES) &&
> +			    (sclk[id].rate_cfg[j].rate != 0); j++) {
>  			if (sclk[id].rate_cfg[j].rate == rate) {
>  				present = true;
>  				break;
Andy Shevchenko Dec. 11, 2017, 11:20 a.m. UTC | #3
On Fri, 2017-12-08 at 14:54 +0300, Dan Carpenter wrote:
> I get a Smatch warning here:
> 
>     sound/soc/intel/skylake/skl-nhlt.c:335 skl_get_ssp_clks()
>     error: testing array offset 'j' after use.
> 
> The code is harmless, but the checker is right that we should swap
> these
> two conditions so we verify that the offset is within bounds before we
> use it.

> -		for (j = 0; (sclk[id].rate_cfg[j].rate != 0) &&
> -				(j < SKL_MAX_CLK_RATES); j++) {
> +		for (j = 0; (j < SKL_MAX_CLK_RATES) &&
> +			    (sclk[id].rate_cfg[j].rate != 0); j++) {
>  			if (sclk[id].rate_cfg[j].rate == rate) {
>  				present = true;
>  				break;

I would rather remove also redundant parens and move the condition into
the loop.
Dan Carpenter Dec. 11, 2017, 11:33 a.m. UTC | #4
On Mon, Dec 11, 2017 at 01:20:23PM +0200, Andy Shevchenko wrote:
> On Fri, 2017-12-08 at 14:54 +0300, Dan Carpenter wrote:
> > I get a Smatch warning here:
> > 
> >     sound/soc/intel/skylake/skl-nhlt.c:335 skl_get_ssp_clks()
> >     error: testing array offset 'j' after use.
> > 
> > The code is harmless, but the checker is right that we should swap
> > these
> > two conditions so we verify that the offset is within bounds before we
> > use it.
> 
> > -		for (j = 0; (sclk[id].rate_cfg[j].rate != 0) &&
> > -				(j < SKL_MAX_CLK_RATES); j++) {
> > +		for (j = 0; (j < SKL_MAX_CLK_RATES) &&
> > +			    (sclk[id].rate_cfg[j].rate != 0); j++) {
> >  			if (sclk[id].rate_cfg[j].rate == rate) {
> >  				present = true;
> >  				break;
> 
> I would rather remove also redundant parens and move the condition into
> the loop.

I didn't like the way the code was written either but it's impossible
to know how much change people are going to accept.  Even though I think
that extra parenthesis shouldn't have been there, the original author
felt they helped, so I try to be accomodating...

Anyway, I can resend.

regards,
dan carpenter
Andy Shevchenko Dec. 11, 2017, 11:54 a.m. UTC | #5
On Mon, 2017-12-11 at 14:33 +0300, Dan Carpenter wrote:
> On Mon, Dec 11, 2017 at 01:20:23PM +0200, Andy Shevchenko wrote:
> > On Fri, 2017-12-08 at 14:54 +0300, Dan Carpenter wrote:
> > > 
> 


> I didn't like the way the code was written either but it's impossible
> to know how much change people are going to accept.  Even though I
> think
> that extra parenthesis shouldn't have been there, the original author
> felt they helped, so I try to be accomodating...
> 
> Anyway, I can resend.

Since Vinod ACKed this change, I'm fine with it.
Consider my comment as JFYI.
diff mbox

Patch

diff --git a/sound/soc/intel/skylake/skl-nhlt.c b/sound/soc/intel/skylake/skl-nhlt.c
index afa557a1c031..54f9bd630f4f 100644
--- a/sound/soc/intel/skylake/skl-nhlt.c
+++ b/sound/soc/intel/skylake/skl-nhlt.c
@@ -331,8 +331,8 @@  static void skl_get_ssp_clks(struct skl *skl, struct skl_ssp_clk *ssp_clks,
 		rate = channels * bps * fs;
 
 		/* check if the rate is added already to the given SSP's sclk */
-		for (j = 0; (sclk[id].rate_cfg[j].rate != 0) &&
-				(j < SKL_MAX_CLK_RATES); j++) {
+		for (j = 0; (j < SKL_MAX_CLK_RATES) &&
+			    (sclk[id].rate_cfg[j].rate != 0); j++) {
 			if (sclk[id].rate_cfg[j].rate == rate) {
 				present = true;
 				break;