All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] soc: aspeed: fix a ternary sign expansion bug
@ 2021-04-23  0:09 Joel Stanley
  2021-04-23  9:30 ` patchwork-bot+linux-soc
  0 siblings, 1 reply; 12+ messages in thread
From: Joel Stanley @ 2021-04-23  0:09 UTC (permalink / raw)
  To: soc; +Cc: Dan Carpenter, Patrick Venture

From: Dan Carpenter <dan.carpenter@oracle.com>

The intent here was to return negative error codes but it actually
returns positive values.  The problem is that type promotion with
ternary operations is quite complicated.

"ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
returns long.  What happens is that "ret" is cast to u32 and becomes
positive then it's cast to long and it's still positive.

Fix this by removing the ternary so that "ret" is type promoted directly
to long.

Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Reviewed-by: Patrick Venture <venture@google.com>
Link: https://lore.kernel.org/r/YIE90PSXsMTa2Y8n@mwanda
Signed-off-by: Joel Stanley <joel@jms.id.au>
---
Soc maintainers, can you please apply this fix.

 drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
index 210455efb321..eceeaf8dfbeb 100644
--- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
+++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
@@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
 			return -EINTR;
 	}
 	ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
+	if (ret)
+		return ret;
 
-	return ret ? ret : copied;
+	return copied;
 }
 
 static __poll_t snoop_file_poll(struct file *file,
-- 
2.30.2


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

* Re: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-23  0:09 [PATCH] soc: aspeed: fix a ternary sign expansion bug Joel Stanley
@ 2021-04-23  9:30 ` patchwork-bot+linux-soc
  0 siblings, 0 replies; 12+ messages in thread
From: patchwork-bot+linux-soc @ 2021-04-23  9:30 UTC (permalink / raw)
  To: Joel Stanley; +Cc: soc

Hello:

This patch was applied to soc/soc.git (refs/heads/for-next):

On Fri, 23 Apr 2021 09:39:19 +0930 you wrote:
> From: Dan Carpenter <dan.carpenter@oracle.com>
> 
> The intent here was to return negative error codes but it actually
> returns positive values.  The problem is that type promotion with
> ternary operations is quite complicated.
> 
> "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> returns long.  What happens is that "ret" is cast to u32 and becomes
> positive then it's cast to long and it's still positive.
> 
> [...]

Here is the summary with links:
  - soc: aspeed: fix a ternary sign expansion bug
    https://git.kernel.org/soc/soc/c/d42805807be7

You are awesome, thank you!
--
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



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

* Re: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-23 10:45   ` Sergey Organov
  2021-04-23 10:54     ` David Laight
@ 2021-04-23 11:14     ` Dan Carpenter
  1 sibling, 0 replies; 12+ messages in thread
From: Dan Carpenter @ 2021-04-23 11:14 UTC (permalink / raw)
  To: Sergey Organov
  Cc: David Laight, Joel Stanley, Andrew Jeffery, Chia-Wei, Wang,
	Jae Hyun Yoo, John Wang, Brad Bishop, Patrick Venture,
	Benjamin Fair, Greg Kroah-Hartman, Robert Lippert, linux-aspeed,
	linux-kernel, kernel-janitors

On Fri, Apr 23, 2021 at 01:45:40PM +0300, Sergey Organov wrote:
> David Laight <David.Laight@ACULAB.COM> writes:
> 
> > From: Dan Carpenter
> >> Sent: 22 April 2021 10:12
> >> 
> >> The intent here was to return negative error codes but it actually
> >> returns positive values.  The problem is that type promotion with
> >> ternary operations is quite complicated.
> >> 
> >> "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> >> returns long.  What happens is that "ret" is cast to u32 and becomes
> >> positive then it's cast to long and it's still positive.
> >> 
> >> Fix this by removing the ternary so that "ret" is type promoted directly
> >> to long.
> >> 
> >> Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
> >> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> >> ---
> >>  drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
> >>  1 file changed, 3 insertions(+), 1 deletion(-)
> >> 
> >> diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> >> index 210455efb321..eceeaf8dfbeb 100644
> >> --- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
> >> +++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> >> @@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
> >>  			return -EINTR;
> >>  	}
> >>  	ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
> >> +	if (ret)
> >> +		return ret;
> >> 
> >> -	return ret ? ret : copied;
> >> +	return copied;
> >
> > I wonder if changing it to:
> > 	return ret ? ret + 0L : copied;
> >
> > Might make people think in the future and not convert it back
> > as an 'optimisation'.
> 
> It rather made me think: "what the heck is going on here?!"
> 
> Shouldn't it better be:
> 
>  	return ret ? ret : (long)copied;
> 
> or even:
> 
>         return ret ?: (long)copied;

I work with Greg a lot and his bias against ternaries has rubbed off a
bit.  They're sort of Perl-ish.  And I have nothing against Perl.  It's
a perfectly fine programming language, but when I write Perl I write it
in C.

regards,
dan carpenter

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

* RE: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-23 10:45   ` Sergey Organov
@ 2021-04-23 10:54     ` David Laight
  2021-04-23 11:14     ` Dan Carpenter
  1 sibling, 0 replies; 12+ messages in thread
From: David Laight @ 2021-04-23 10:54 UTC (permalink / raw)
  To: 'Sergey Organov'
  Cc: 'Dan Carpenter',
	Joel Stanley, Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo,
	John Wang, Brad Bishop, Patrick Venture, Benjamin Fair,
	Greg Kroah-Hartman, Robert Lippert, linux-aspeed, linux-kernel,
	kernel-janitors

From: Sergey Organov
> Sent: 23 April 2021 11:46
> 
> David Laight <David.Laight@ACULAB.COM> writes:
> 
> > From: Dan Carpenter
> >> Sent: 22 April 2021 10:12
> >>
> >> The intent here was to return negative error codes but it actually
> >> returns positive values.  The problem is that type promotion with
> >> ternary operations is quite complicated.
> >>
> >> "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> >> returns long.  What happens is that "ret" is cast to u32 and becomes
> >> positive then it's cast to long and it's still positive.
> >>
> >> Fix this by removing the ternary so that "ret" is type promoted directly
> >> to long.
> >>
> >> Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
> >> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> >> ---
> >>  drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
> >>  1 file changed, 3 insertions(+), 1 deletion(-)
> >>
> >> diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> >> index 210455efb321..eceeaf8dfbeb 100644
> >> --- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
> >> +++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> >> @@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
> >>  			return -EINTR;
> >>  	}
> >>  	ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
> >> +	if (ret)
> >> +		return ret;
> >>
> >> -	return ret ? ret : copied;
> >> +	return copied;
> >
> > I wonder if changing it to:
> > 	return ret ? ret + 0L : copied;
> >
> > Might make people think in the future and not convert it back
> > as an 'optimisation'.
> 
> It rather made me think: "what the heck is going on here?!"
> 
> Shouldn't it better be:
> 
>  	return ret ? ret : (long)copied;
> 
> or even:
> 
>         return ret ?: (long)copied;

Or change the return type to int ?

The problem is that ?: doesn't behave the way most people expect.
The two possible values have to be converted to the same type.

Together with the broken decision of the original ANSI C committee
to change from K&R's 'sign preserving' to 'value preserving'
integer promotions causes grief here and elsewhere.
(Not no mention breaking existing code!)

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


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

* Re: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-22 16:21 ` David Laight
  2021-04-23  0:07   ` Joel Stanley
  2021-04-23  7:43   ` Dan Carpenter
@ 2021-04-23 10:45   ` Sergey Organov
  2021-04-23 10:54     ` David Laight
  2021-04-23 11:14     ` Dan Carpenter
  2 siblings, 2 replies; 12+ messages in thread
From: Sergey Organov @ 2021-04-23 10:45 UTC (permalink / raw)
  To: David Laight
  Cc: 'Dan Carpenter',
	Joel Stanley, Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo,
	John Wang, Brad Bishop, Patrick Venture, Benjamin Fair,
	Greg Kroah-Hartman, Robert Lippert, linux-aspeed, linux-kernel,
	kernel-janitors

David Laight <David.Laight@ACULAB.COM> writes:

> From: Dan Carpenter
>> Sent: 22 April 2021 10:12
>> 
>> The intent here was to return negative error codes but it actually
>> returns positive values.  The problem is that type promotion with
>> ternary operations is quite complicated.
>> 
>> "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
>> returns long.  What happens is that "ret" is cast to u32 and becomes
>> positive then it's cast to long and it's still positive.
>> 
>> Fix this by removing the ternary so that "ret" is type promoted directly
>> to long.
>> 
>> Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
>> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
>> ---
>>  drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
>>  1 file changed, 3 insertions(+), 1 deletion(-)
>> 
>> diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
>> index 210455efb321..eceeaf8dfbeb 100644
>> --- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
>> +++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
>> @@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
>>  			return -EINTR;
>>  	}
>>  	ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
>> +	if (ret)
>> +		return ret;
>> 
>> -	return ret ? ret : copied;
>> +	return copied;
>
> I wonder if changing it to:
> 	return ret ? ret + 0L : copied;
>
> Might make people think in the future and not convert it back
> as an 'optimisation'.

It rather made me think: "what the heck is going on here?!"

Shouldn't it better be:

 	return ret ? ret : (long)copied;

or even:

        return ret ?: (long)copied;

?

-- Sergey Organov

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

* Re: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-22 16:21 ` David Laight
  2021-04-23  0:07   ` Joel Stanley
@ 2021-04-23  7:43   ` Dan Carpenter
  2021-04-23 10:45   ` Sergey Organov
  2 siblings, 0 replies; 12+ messages in thread
From: Dan Carpenter @ 2021-04-23  7:43 UTC (permalink / raw)
  To: David Laight, Aurélien Aptel
  Cc: Joel Stanley, Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo,
	John Wang, Brad Bishop, Patrick Venture, Benjamin Fair,
	Greg Kroah-Hartman, Robert Lippert, linux-aspeed, linux-kernel,
	kernel-janitors

On Thu, Apr 22, 2021 at 04:21:40PM +0000, David Laight wrote:
> From: Dan Carpenter
> > Sent: 22 April 2021 10:12
> > 
> > The intent here was to return negative error codes but it actually
> > returns positive values.  The problem is that type promotion with
> > ternary operations is quite complicated.
> > 
> > "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> > returns long.  What happens is that "ret" is cast to u32 and becomes
> > positive then it's cast to long and it's still positive.
> > 
> > Fix this by removing the ternary so that "ret" is type promoted directly
> > to long.
> > 
> > Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >  drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> > 
> > diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> > index 210455efb321..eceeaf8dfbeb 100644
> > --- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
> > +++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> > @@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
> >  			return -EINTR;
> >  	}
> >  	ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
> > +	if (ret)
> > +		return ret;
> > 
> > -	return ret ? ret : copied;
> > +	return copied;
> 
> I wonder if changing it to:
> 	return ret ? ret + 0L : copied;
> 
> Might make people think in the future and not convert it back
> as an 'optimisation'.

This is from a Smatch test that Aurélien Aptel requested.  The test is
pretty good quality with few false positives so I will push it soon.

If someone converts it back then I expect the checker will catch it.

regards,
dan carepnter


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

* Re: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-22 16:21 ` David Laight
@ 2021-04-23  0:07   ` Joel Stanley
  2021-04-23  7:43   ` Dan Carpenter
  2021-04-23 10:45   ` Sergey Organov
  2 siblings, 0 replies; 12+ messages in thread
From: Joel Stanley @ 2021-04-23  0:07 UTC (permalink / raw)
  To: David Laight
  Cc: Dan Carpenter, Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo,
	John Wang, Brad Bishop, Patrick Venture, Benjamin Fair,
	Greg Kroah-Hartman, Robert Lippert, linux-aspeed, linux-kernel,
	kernel-janitors

On Thu, 22 Apr 2021 at 16:21, David Laight <David.Laight@aculab.com> wrote:
>
> From: Dan Carpenter
> > Sent: 22 April 2021 10:12
> >
> > The intent here was to return negative error codes but it actually
> > returns positive values.  The problem is that type promotion with
> > ternary operations is quite complicated.
> >
> > "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> > returns long.  What happens is that "ret" is cast to u32 and becomes
> > positive then it's cast to long and it's still positive.
> >
> > Fix this by removing the ternary so that "ret" is type promoted directly
> > to long.
> >
> > Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
> > Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> > ---
> >  drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
> >  1 file changed, 3 insertions(+), 1 deletion(-)
> >
> > diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> > index 210455efb321..eceeaf8dfbeb 100644
> > --- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
> > +++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> > @@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
> >                       return -EINTR;
> >       }
> >       ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
> > +     if (ret)
> > +             return ret;
> >
> > -     return ret ? ret : copied;
> > +     return copied;
>
> I wonder if changing it to:
>         return ret ? ret + 0L : copied;
>
> Might make people think in the future and not convert it back
> as an 'optimisation'.

I think the change that Dan posted is clear.

Thanks Dan! I'll get it queued up.

Cheers,

Joel

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

* RE: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-22  9:11 Dan Carpenter
  2021-04-22  9:24 ` Al Viro
  2021-04-22 14:56 ` Patrick Venture
@ 2021-04-22 16:21 ` David Laight
  2021-04-23  0:07   ` Joel Stanley
                     ` (2 more replies)
  2 siblings, 3 replies; 12+ messages in thread
From: David Laight @ 2021-04-22 16:21 UTC (permalink / raw)
  To: 'Dan Carpenter', Joel Stanley
  Cc: Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo, John Wang,
	Brad Bishop, Patrick Venture, Benjamin Fair, Greg Kroah-Hartman,
	Robert Lippert, linux-aspeed, linux-kernel, kernel-janitors

From: Dan Carpenter
> Sent: 22 April 2021 10:12
> 
> The intent here was to return negative error codes but it actually
> returns positive values.  The problem is that type promotion with
> ternary operations is quite complicated.
> 
> "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> returns long.  What happens is that "ret" is cast to u32 and becomes
> positive then it's cast to long and it's still positive.
> 
> Fix this by removing the ternary so that "ret" is type promoted directly
> to long.
> 
> Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
> ---
>  drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> index 210455efb321..eceeaf8dfbeb 100644
> --- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
> +++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> @@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
>  			return -EINTR;
>  	}
>  	ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
> +	if (ret)
> +		return ret;
> 
> -	return ret ? ret : copied;
> +	return copied;

I wonder if changing it to:
	return ret ? ret + 0L : copied;

Might make people think in the future and not convert it back
as an 'optimisation'.

I much prefer adding 0 to a cast to fix integer types.
In can go less wrong!

IMHO there are far too many casts in the kernel sources.
Especially the ones that are only there to appease sparse.
A functional notation for those would remove some of
the potential problems.

	David

-
Registered Address Lakeside, Bramley Road, Mount Farm, Milton Keynes, MK1 1PT, UK
Registration No: 1397386 (Wales)


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

* Re: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-22  9:11 Dan Carpenter
  2021-04-22  9:24 ` Al Viro
@ 2021-04-22 14:56 ` Patrick Venture
  2021-04-22 16:21 ` David Laight
  2 siblings, 0 replies; 12+ messages in thread
From: Patrick Venture @ 2021-04-22 14:56 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Joel Stanley, Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo,
	John Wang, Brad Bishop, Benjamin Fair, Greg Kroah-Hartman,
	Robert Lippert, linux-aspeed, Linux Kernel Mailing List,
	kernel-janitors

On Thu, Apr 22, 2021 at 2:12 AM Dan Carpenter <dan.carpenter@oracle.com> wrote:
>
> The intent here was to return negative error codes but it actually
> returns positive values.  The problem is that type promotion with
> ternary operations is quite complicated.
>
> "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> returns long.  What happens is that "ret" is cast to u32 and becomes
> positive then it's cast to long and it's still positive.
>
> Fix this by removing the ternary so that "ret" is type promoted directly
> to long.
>
> Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
> Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Reviewed-by: Patrick Venture <venture@google.com>
> ---
>  drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
>
> diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> index 210455efb321..eceeaf8dfbeb 100644
> --- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
> +++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
> @@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
>                         return -EINTR;
>         }
>         ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
> +       if (ret)
> +               return ret;
>
> -       return ret ? ret : copied;
> +       return copied;
>  }
>
>  static __poll_t snoop_file_poll(struct file *file,
> --
> 2.30.2
>

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

* Re: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-22  9:24 ` Al Viro
@ 2021-04-22  9:26   ` Al Viro
  0 siblings, 0 replies; 12+ messages in thread
From: Al Viro @ 2021-04-22  9:26 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Joel Stanley, Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo,
	John Wang, Brad Bishop, Patrick Venture, Benjamin Fair,
	Greg Kroah-Hartman, Robert Lippert, linux-aspeed, linux-kernel,
	kernel-janitors

On Thu, Apr 22, 2021 at 09:24:59AM +0000, Al Viro wrote:
> On Thu, Apr 22, 2021 at 12:11:44PM +0300, Dan Carpenter wrote:
> > The intent here was to return negative error codes but it actually
> > returns positive values.  The problem is that type promotion with
> > ternary operations is quite complicated.
> > 
> > "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> > returns long.  What happens is that "ret" is cast to u32 and becomes
> > positive then it's cast to long and it's still positive.
> > 
> > Fix this by removing the ternary so that "ret" is type promoted directly
> > to long.
> 
> Hmm...  Let's grep for kfifo_to_user() - smells like a possible recurring bug...
> Yup -
> 
> samples/kfifo/bytestream-example.c:138: ret = kfifo_to_user(&test, buf, count, &copied);
> samples/kfifo/inttype-example.c:131:    ret = kfifo_to_user(&test, buf, count, &copied);
> samples/kfifo/record-example.c:145:     ret = kfifo_to_user(&test, buf, count, &copied);
> 
> All three are exactly like that one.

Nevermind, you've already caught and posted that bunch.  Sorry for noise...

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

* Re: [PATCH] soc: aspeed: fix a ternary sign expansion bug
  2021-04-22  9:11 Dan Carpenter
@ 2021-04-22  9:24 ` Al Viro
  2021-04-22  9:26   ` Al Viro
  2021-04-22 14:56 ` Patrick Venture
  2021-04-22 16:21 ` David Laight
  2 siblings, 1 reply; 12+ messages in thread
From: Al Viro @ 2021-04-22  9:24 UTC (permalink / raw)
  To: Dan Carpenter
  Cc: Joel Stanley, Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo,
	John Wang, Brad Bishop, Patrick Venture, Benjamin Fair,
	Greg Kroah-Hartman, Robert Lippert, linux-aspeed, linux-kernel,
	kernel-janitors

On Thu, Apr 22, 2021 at 12:11:44PM +0300, Dan Carpenter wrote:
> The intent here was to return negative error codes but it actually
> returns positive values.  The problem is that type promotion with
> ternary operations is quite complicated.
> 
> "ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
> returns long.  What happens is that "ret" is cast to u32 and becomes
> positive then it's cast to long and it's still positive.
> 
> Fix this by removing the ternary so that "ret" is type promoted directly
> to long.

Hmm...  Let's grep for kfifo_to_user() - smells like a possible recurring bug...
Yup -

samples/kfifo/bytestream-example.c:138: ret = kfifo_to_user(&test, buf, count, &copied);
samples/kfifo/inttype-example.c:131:    ret = kfifo_to_user(&test, buf, count, &copied);
samples/kfifo/record-example.c:145:     ret = kfifo_to_user(&test, buf, count, &copied);

All three are exactly like that one.

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

* [PATCH] soc: aspeed: fix a ternary sign expansion bug
@ 2021-04-22  9:11 Dan Carpenter
  2021-04-22  9:24 ` Al Viro
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Dan Carpenter @ 2021-04-22  9:11 UTC (permalink / raw)
  To: Joel Stanley
  Cc: Andrew Jeffery, Chia-Wei, Wang, Jae Hyun Yoo, John Wang,
	Brad Bishop, Patrick Venture, Benjamin Fair, Greg Kroah-Hartman,
	Robert Lippert, linux-aspeed, linux-kernel, kernel-janitors

The intent here was to return negative error codes but it actually
returns positive values.  The problem is that type promotion with
ternary operations is quite complicated.

"ret" is an int.  "copied" is a u32.  And the snoop_file_read() function
returns long.  What happens is that "ret" is cast to u32 and becomes
positive then it's cast to long and it's still positive.

Fix this by removing the ternary so that "ret" is type promoted directly
to long.

Fixes: 3772e5da4454 ("drivers/misc: Aspeed LPC snoop output using misc chardev")
Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
---
 drivers/soc/aspeed/aspeed-lpc-snoop.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/soc/aspeed/aspeed-lpc-snoop.c b/drivers/soc/aspeed/aspeed-lpc-snoop.c
index 210455efb321..eceeaf8dfbeb 100644
--- a/drivers/soc/aspeed/aspeed-lpc-snoop.c
+++ b/drivers/soc/aspeed/aspeed-lpc-snoop.c
@@ -94,8 +94,10 @@ static ssize_t snoop_file_read(struct file *file, char __user *buffer,
 			return -EINTR;
 	}
 	ret = kfifo_to_user(&chan->fifo, buffer, count, &copied);
+	if (ret)
+		return ret;
 
-	return ret ? ret : copied;
+	return copied;
 }
 
 static __poll_t snoop_file_poll(struct file *file,
-- 
2.30.2


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

end of thread, other threads:[~2021-04-23 11:15 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-04-23  0:09 [PATCH] soc: aspeed: fix a ternary sign expansion bug Joel Stanley
2021-04-23  9:30 ` patchwork-bot+linux-soc
  -- strict thread matches above, loose matches on Subject: below --
2021-04-22  9:11 Dan Carpenter
2021-04-22  9:24 ` Al Viro
2021-04-22  9:26   ` Al Viro
2021-04-22 14:56 ` Patrick Venture
2021-04-22 16:21 ` David Laight
2021-04-23  0:07   ` Joel Stanley
2021-04-23  7:43   ` Dan Carpenter
2021-04-23 10:45   ` Sergey Organov
2021-04-23 10:54     ` David Laight
2021-04-23 11:14     ` Dan Carpenter

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.