diff mbox

[next] ALSA: xen-front: fix unsigned error check on return from to_sndif_format

Message ID 20180527213219.24182-1-colin.king@canonical.com (mailing list archive)
State New, archived
Headers show

Commit Message

Colin King May 27, 2018, 9:32 p.m. UTC
From: Colin Ian King <colin.king@canonical.com>

The negative error return from the call to to_sndif_format is being
assigned to an unsigned 8 bit integer and hence the check for a negative
value is always going to be false.  Fix this by using ret as the error
return and hence the negative error can be detected and assign
the u8 sndif_format to ret if there is no error.

Detected by CoverityScan, CID#1469385 ("Unsigned compared against 0")

Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 sound/xen/xen_snd_front_alsa.c | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

Comments

Takashi Sakamoto May 27, 2018, 10:49 p.m. UTC | #1
Hi,

On May 28 2018 06:32, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> The negative error return from the call to to_sndif_format is being
> assigned to an unsigned 8 bit integer and hence the check for a negative
> value is always going to be false.  Fix this by using ret as the error
> return and hence the negative error can be detected and assign
> the u8 sndif_format to ret if there is no error.
> 
> Detected by CoverityScan, CID#1469385 ("Unsigned compared against 0")
> 
> Signed-off-by: Colin Ian King <colin.king@canonical.com>
> ---
>   sound/xen/xen_snd_front_alsa.c | 7 ++++---
>   1 file changed, 4 insertions(+), 3 deletions(-)
> 
> diff --git a/sound/xen/xen_snd_front_alsa.c b/sound/xen/xen_snd_front_alsa.c
> index 5041f83e98d2..5a2bd70a2fa1 100644
> --- a/sound/xen/xen_snd_front_alsa.c
> +++ b/sound/xen/xen_snd_front_alsa.c
> @@ -466,13 +466,14 @@ static int alsa_prepare(struct snd_pcm_substream *substream)
>   		u8 sndif_format;
>   		int ret;
>   
> -		sndif_format = to_sndif_format(runtime->format);
> -		if (sndif_format < 0) {
> +		ret = to_sndif_format(runtime->format);
> +		if (ret < 0) {
>   			dev_err(&stream->front_info->xb_dev->dev,
>   				"Unsupported sample format: %d\n",
>   				runtime->format);
> -			return sndif_format;
> +			return ret;
>   		}
> +		sndif_format = ret;
>   
>   		ret = xen_snd_front_stream_prepare(&stream->evt_pair->req,
>   						   &stream->sh_buf,

Indeed. A typical assignment mistake. Instead, we could change the type 
of 'sndif_format' to signed int, however in this case it's not the same 
as the third argument of xen_snd_front_stream_prepare() because it is 
'u8'. This patch looks good to me.

Reviewed-by: Takashi Sakamoto <o-takashi@sakamoccchi.jp>


Regards

Takashi Sakamoto
Takashi Iwai May 28, 2018, 9:30 a.m. UTC | #2
On Mon, 28 May 2018 00:49:03 +0200,
Takashi Sakamoto wrote:
> 
> Hi,
> 
> On May 28 2018 06:32, Colin King wrote:
> > From: Colin Ian King <colin.king@canonical.com>
> >
> > The negative error return from the call to to_sndif_format is being
> > assigned to an unsigned 8 bit integer and hence the check for a negative
> > value is always going to be false.  Fix this by using ret as the error
> > return and hence the negative error can be detected and assign
> > the u8 sndif_format to ret if there is no error.
> >
> > Detected by CoverityScan, CID#1469385 ("Unsigned compared against 0")
> >
> > Signed-off-by: Colin Ian King <colin.king@canonical.com>
> > ---
> >   sound/xen/xen_snd_front_alsa.c | 7 ++++---
> >   1 file changed, 4 insertions(+), 3 deletions(-)
> >
> > diff --git a/sound/xen/xen_snd_front_alsa.c b/sound/xen/xen_snd_front_alsa.c
> > index 5041f83e98d2..5a2bd70a2fa1 100644
> > --- a/sound/xen/xen_snd_front_alsa.c
> > +++ b/sound/xen/xen_snd_front_alsa.c
> > @@ -466,13 +466,14 @@ static int alsa_prepare(struct snd_pcm_substream *substream)
> >   		u8 sndif_format;
> >   		int ret;
> >   -		sndif_format = to_sndif_format(runtime->format);
> > -		if (sndif_format < 0) {
> > +		ret = to_sndif_format(runtime->format);
> > +		if (ret < 0) {
> >   			dev_err(&stream->front_info->xb_dev->dev,
> >   				"Unsupported sample format: %d\n",
> >   				runtime->format);
> > -			return sndif_format;
> > +			return ret;
> >   		}
> > +		sndif_format = ret;
> >     		ret =
> > xen_snd_front_stream_prepare(&stream->evt_pair->req,
> >   						   &stream->sh_buf,
> 
> Indeed. A typical assignment mistake. Instead, we could change the
> type of 'sndif_format' to signed int, however in this case it's not
> the same as the third argument of xen_snd_front_stream_prepare()
> because it is 'u8'. This patch looks good to me.
> 
> Reviewed-by: Takashi Sakamoto <o-takashi@sakamoccchi.jp>

Applied, thanks.


Takashi
diff mbox

Patch

diff --git a/sound/xen/xen_snd_front_alsa.c b/sound/xen/xen_snd_front_alsa.c
index 5041f83e98d2..5a2bd70a2fa1 100644
--- a/sound/xen/xen_snd_front_alsa.c
+++ b/sound/xen/xen_snd_front_alsa.c
@@ -466,13 +466,14 @@  static int alsa_prepare(struct snd_pcm_substream *substream)
 		u8 sndif_format;
 		int ret;
 
-		sndif_format = to_sndif_format(runtime->format);
-		if (sndif_format < 0) {
+		ret = to_sndif_format(runtime->format);
+		if (ret < 0) {
 			dev_err(&stream->front_info->xb_dev->dev,
 				"Unsupported sample format: %d\n",
 				runtime->format);
-			return sndif_format;
+			return ret;
 		}
+		sndif_format = ret;
 
 		ret = xen_snd_front_stream_prepare(&stream->evt_pair->req,
 						   &stream->sh_buf,