linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] driver core: platform: fix u32 greater or equal to zero comparison
@ 2020-01-16 17:57 Colin King
  2020-01-16 20:37 ` Rafael J. Wysocki
  2020-01-17  4:42 ` Dan Carpenter
  0 siblings, 2 replies; 5+ messages in thread
From: Colin King @ 2020-01-16 17:57 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Rafael J . Wysocki, Simon Schwartz
  Cc: kernel-janitors, linux-kernel

From: Colin Ian King <colin.king@canonical.com>

Currently the check that a u32 variable i is >= 0 is always true because
the unsigned variable will never be negative, causing the loop to run
forever.  Fix this by changing the pre-decrement check to a zero check on
i followed by a decrement of i.

Addresses-Coverity: ("Unsigned compared against 0")
Fixes: 39cc539f90d0 ("driver core: platform: Prevent resouce overflow from causing infinite loops")
Signed-off-by: Colin Ian King <colin.king@canonical.com>
---
 drivers/base/platform.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/drivers/base/platform.c b/drivers/base/platform.c
index 864b53b3d598..7fa654f1288b 100644
--- a/drivers/base/platform.c
+++ b/drivers/base/platform.c
@@ -571,7 +571,7 @@ int platform_device_add(struct platform_device *pdev)
 		pdev->id = PLATFORM_DEVID_AUTO;
 	}
 
-	while (--i >= 0) {
+	while (i--) {
 		struct resource *r = &pdev->resource[i];
 		if (r->parent)
 			release_resource(r);
-- 
2.24.0


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

* Re: [PATCH][next] driver core: platform: fix u32 greater or equal to zero comparison
  2020-01-16 17:57 [PATCH][next] driver core: platform: fix u32 greater or equal to zero comparison Colin King
@ 2020-01-16 20:37 ` Rafael J. Wysocki
  2020-01-17  4:42 ` Dan Carpenter
  1 sibling, 0 replies; 5+ messages in thread
From: Rafael J. Wysocki @ 2020-01-16 20:37 UTC (permalink / raw)
  To: Colin King
  Cc: Greg Kroah-Hartman, Rafael J . Wysocki, Simon Schwartz,
	kernel-janitors, Linux Kernel Mailing List

On Thu, Jan 16, 2020 at 6:58 PM Colin King <colin.king@canonical.com> wrote:
>
> From: Colin Ian King <colin.king@canonical.com>
>
> Currently the check that a u32 variable i is >= 0 is always true because
> the unsigned variable will never be negative, causing the loop to run
> forever.  Fix this by changing the pre-decrement check to a zero check on
> i followed by a decrement of i.
>
> Addresses-Coverity: ("Unsigned compared against 0")
> Fixes: 39cc539f90d0 ("driver core: platform: Prevent resouce overflow from causing infinite loops")
> Signed-off-by: Colin Ian King <colin.king@canonical.com>

Reviewed-by: Rafael J. Wysocki <rafael.j.wysocki@intel.com>

> ---
>  drivers/base/platform.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/drivers/base/platform.c b/drivers/base/platform.c
> index 864b53b3d598..7fa654f1288b 100644
> --- a/drivers/base/platform.c
> +++ b/drivers/base/platform.c
> @@ -571,7 +571,7 @@ int platform_device_add(struct platform_device *pdev)
>                 pdev->id = PLATFORM_DEVID_AUTO;
>         }
>
> -       while (--i >= 0) {
> +       while (i--) {
>                 struct resource *r = &pdev->resource[i];
>                 if (r->parent)
>                         release_resource(r);
> --
> 2.24.0
>

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

* Re: [PATCH][next] driver core: platform: fix u32 greater or equal to zero comparison
  2020-01-16 17:57 [PATCH][next] driver core: platform: fix u32 greater or equal to zero comparison Colin King
  2020-01-16 20:37 ` Rafael J. Wysocki
@ 2020-01-17  4:42 ` Dan Carpenter
  2020-01-17  4:46   ` Dan Carpenter
  2020-01-17  5:06   ` Dan Carpenter
  1 sibling, 2 replies; 5+ messages in thread
From: Dan Carpenter @ 2020-01-17  4:42 UTC (permalink / raw)
  To: Colin King
  Cc: Greg Kroah-Hartman, Rafael J . Wysocki, Simon Schwartz,
	kernel-janitors, linux-kernel

On Thu, Jan 16, 2020 at 05:57:58PM +0000, Colin King wrote:
> From: Colin Ian King <colin.king@canonical.com>
> 
> Currently the check that a u32 variable i is >= 0 is always true because
> the unsigned variable will never be negative, causing the loop to run
> forever.  Fix this by changing the pre-decrement check to a zero check on
> i followed by a decrement of i.
> 
> Addresses-Coverity: ("Unsigned compared against 0")
> Fixes: 39cc539f90d0 ("driver core: platform: Prevent resouce overflow from causing infinite loops")

A better fix would be to revert this patch.  It doesn't fix a real bug.
The ->num_resources is typically under 5.  It's not going to overflow
INT_MAX any time soon.  There are "architectures with smaller ints."

It should always be "int i" unless there is a valid real life reason.
People think that declaring everything as u32 will fix bugs but it
normally just introduces bugs as it does here.  u32 makes the code
harder to read.

This is a sore spot for me because apparently there is a static
analysis tool which tells people to use "u32 i;" everywhere.  It's bad
advice.  I have asked around but I haven't found which tool it is, but
which  we should find the tool and delete it to prevent this kind of
stuff in the future.

regards,
dan carpenter


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

* Re: [PATCH][next] driver core: platform: fix u32 greater or equal to zero comparison
  2020-01-17  4:42 ` Dan Carpenter
@ 2020-01-17  4:46   ` Dan Carpenter
  2020-01-17  5:06   ` Dan Carpenter
  1 sibling, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2020-01-17  4:46 UTC (permalink / raw)
  To: Colin King
  Cc: Greg Kroah-Hartman, Rafael J . Wysocki, Simon Schwartz,
	kernel-janitors, linux-kernel

On Fri, Jan 17, 2020 at 07:42:16AM +0300, Dan Carpenter wrote:
> On Thu, Jan 16, 2020 at 05:57:58PM +0000, Colin King wrote:
> > From: Colin Ian King <colin.king@canonical.com>
> > 
> > Currently the check that a u32 variable i is >= 0 is always true because
> > the unsigned variable will never be negative, causing the loop to run
> > forever.  Fix this by changing the pre-decrement check to a zero check on
> > i followed by a decrement of i.
> > 
> > Addresses-Coverity: ("Unsigned compared against 0")
> > Fixes: 39cc539f90d0 ("driver core: platform: Prevent resouce overflow from causing infinite loops")
> 
> A better fix would be to revert this patch.  It doesn't fix a real bug.
> The ->num_resources is typically under 5.  It's not going to overflow
> INT_MAX any time soon.  There are "architectures with smaller ints."

I left out a word.  There are *no* "architectures with smaller ints."...

I mean there used to be systems with 16 bit int but that was before I
was born.  You could never run linux on them.

regards,
dan carpenter


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

* Re: [PATCH][next] driver core: platform: fix u32 greater or equal to zero comparison
  2020-01-17  4:42 ` Dan Carpenter
  2020-01-17  4:46   ` Dan Carpenter
@ 2020-01-17  5:06   ` Dan Carpenter
  1 sibling, 0 replies; 5+ messages in thread
From: Dan Carpenter @ 2020-01-17  5:06 UTC (permalink / raw)
  To: Colin King
  Cc: Greg Kroah-Hartman, Rafael J . Wysocki, Simon Schwartz,
	kernel-janitors, linux-kernel

On Fri, Jan 17, 2020 at 07:42:16AM +0300, Dan Carpenter wrote:
> On Thu, Jan 16, 2020 at 05:57:58PM +0000, Colin King wrote:
> > From: Colin Ian King <colin.king@canonical.com>
> > 
> > Currently the check that a u32 variable i is >= 0 is always true because
> > the unsigned variable will never be negative, causing the loop to run
> > forever.  Fix this by changing the pre-decrement check to a zero check on
> > i followed by a decrement of i.
> > 
> > Addresses-Coverity: ("Unsigned compared against 0")
> > Fixes: 39cc539f90d0 ("driver core: platform: Prevent resouce overflow from causing infinite loops")
> 

Also by the way say you have:

	unsigned int limit = (unsigned)INT_MAX + 4;
	int i;

	for (i = 0; i < limit; i++)
		;
	printf("%d\n", i);


The loop will work the same way regardless of if int is signed or not
because of type promotion.

regards,
dan carpenter

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

end of thread, other threads:[~2020-01-17  5:04 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-01-16 17:57 [PATCH][next] driver core: platform: fix u32 greater or equal to zero comparison Colin King
2020-01-16 20:37 ` Rafael J. Wysocki
2020-01-17  4:42 ` Dan Carpenter
2020-01-17  4:46   ` Dan Carpenter
2020-01-17  5:06   ` Dan Carpenter

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).