* [PATCH] CacheFiles: Cleanup redundant tests on unsigned
@ 2009-10-23 16:30 Roel Kluin
0 siblings, 0 replies; 3+ messages in thread
From: Roel Kluin @ 2009-10-23 16:30 UTC (permalink / raw)
To: David Howells, linux-cachefs, linux-kernel, Andrew Morton
The variables are unsigned so the test `>= 0' is always true,
the `< 0' test always fails. The other part of
the test catches wrapped values.
Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
---
fs/cachefiles/bind.c | 6 ++----
fs/cachefiles/daemon.c | 6 +++---
2 files changed, 5 insertions(+), 7 deletions(-)
diff --git a/fs/cachefiles/bind.c b/fs/cachefiles/bind.c
index 3797e00..ce776f6 100644
--- a/fs/cachefiles/bind.c
+++ b/fs/cachefiles/bind.c
@@ -39,13 +39,11 @@ int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
args);
/* start by checking things over */
- ASSERT(cache->fstop_percent >= 0 &&
- cache->fstop_percent < cache->fcull_percent &&
+ ASSERT(cache->fstop_percent < cache->fcull_percent &&
cache->fcull_percent < cache->frun_percent &&
cache->frun_percent < 100);
- ASSERT(cache->bstop_percent >= 0 &&
- cache->bstop_percent < cache->bcull_percent &&
+ ASSERT(cache->bstop_percent < cache->bcull_percent &&
cache->bcull_percent < cache->brun_percent &&
cache->brun_percent < 100);
diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c
index 4618516..bb30d01 100644
--- a/fs/cachefiles/daemon.c
+++ b/fs/cachefiles/daemon.c
@@ -220,7 +220,7 @@ static ssize_t cachefiles_daemon_write(struct file *file,
if (test_bit(CACHEFILES_DEAD, &cache->flags))
return -EIO;
- if (datalen < 0 || datalen > PAGE_SIZE - 1)
+ if (datalen > PAGE_SIZE - 1)
return -EOPNOTSUPP;
/* drag the command string into the kernel so we can parse it */
@@ -385,7 +385,7 @@ static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
if (args[0] != '%' || args[1] != '\0')
return -EINVAL;
- if (fstop < 0 || fstop >= cache->fcull_percent)
+ if (fstop >= cache->fcull_percent)
return cachefiles_daemon_range_error(cache, args);
cache->fstop_percent = fstop;
@@ -457,7 +457,7 @@ static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
if (args[0] != '%' || args[1] != '\0')
return -EINVAL;
- if (bstop < 0 || bstop >= cache->bcull_percent)
+ if (bstop >= cache->bcull_percent)
return cachefiles_daemon_range_error(cache, args);
cache->bstop_percent = bstop;
^ permalink raw reply related [flat|nested] 3+ messages in thread
* [PATCH] CacheFiles: Cleanup redundant tests on unsigned
@ 2009-10-26 15:58 David Howells
2009-10-29 15:31 ` Linus Torvalds
0 siblings, 1 reply; 3+ messages in thread
From: David Howells @ 2009-10-26 15:58 UTC (permalink / raw)
To: torvalds, akpm; +Cc: linux-cachefs, linux-kernel, Roel Kluin, David Howells
From: Roel Kluin <roel.kluin@gmail.com>
The variables are unsigned so the test `>= 0' is always true,
the `< 0' test always fails. The other part of
the test catches wrapped values.
Signed-off-by: Roel Kluin <roel.kluin@gmail.com>
Signed-off-by: David Howells <dhowells@redhat.com>
---
fs/cachefiles/bind.c | 6 ++----
fs/cachefiles/daemon.c | 6 +++---
2 files changed, 5 insertions(+), 7 deletions(-)
diff --git a/fs/cachefiles/bind.c b/fs/cachefiles/bind.c
index 3797e00..ce776f6 100644
--- a/fs/cachefiles/bind.c
+++ b/fs/cachefiles/bind.c
@@ -39,13 +39,11 @@ int cachefiles_daemon_bind(struct cachefiles_cache *cache, char *args)
args);
/* start by checking things over */
- ASSERT(cache->fstop_percent >= 0 &&
- cache->fstop_percent < cache->fcull_percent &&
+ ASSERT(cache->fstop_percent < cache->fcull_percent &&
cache->fcull_percent < cache->frun_percent &&
cache->frun_percent < 100);
- ASSERT(cache->bstop_percent >= 0 &&
- cache->bstop_percent < cache->bcull_percent &&
+ ASSERT(cache->bstop_percent < cache->bcull_percent &&
cache->bcull_percent < cache->brun_percent &&
cache->brun_percent < 100);
diff --git a/fs/cachefiles/daemon.c b/fs/cachefiles/daemon.c
index 4618516..bb30d01 100644
--- a/fs/cachefiles/daemon.c
+++ b/fs/cachefiles/daemon.c
@@ -220,7 +220,7 @@ static ssize_t cachefiles_daemon_write(struct file *file,
if (test_bit(CACHEFILES_DEAD, &cache->flags))
return -EIO;
- if (datalen < 0 || datalen > PAGE_SIZE - 1)
+ if (datalen > PAGE_SIZE - 1)
return -EOPNOTSUPP;
/* drag the command string into the kernel so we can parse it */
@@ -385,7 +385,7 @@ static int cachefiles_daemon_fstop(struct cachefiles_cache *cache, char *args)
if (args[0] != '%' || args[1] != '\0')
return -EINVAL;
- if (fstop < 0 || fstop >= cache->fcull_percent)
+ if (fstop >= cache->fcull_percent)
return cachefiles_daemon_range_error(cache, args);
cache->fstop_percent = fstop;
@@ -457,7 +457,7 @@ static int cachefiles_daemon_bstop(struct cachefiles_cache *cache, char *args)
if (args[0] != '%' || args[1] != '\0')
return -EINVAL;
- if (bstop < 0 || bstop >= cache->bcull_percent)
+ if (bstop >= cache->bcull_percent)
return cachefiles_daemon_range_error(cache, args);
cache->bstop_percent = bstop;
^ permalink raw reply related [flat|nested] 3+ messages in thread
* Re: [PATCH] CacheFiles: Cleanup redundant tests on unsigned
2009-10-26 15:58 David Howells
@ 2009-10-29 15:31 ` Linus Torvalds
0 siblings, 0 replies; 3+ messages in thread
From: Linus Torvalds @ 2009-10-29 15:31 UTC (permalink / raw)
To: David Howells; +Cc: akpm, linux-cachefs, linux-kernel, Roel Kluin
On Mon, 26 Oct 2009, David Howells wrote:
>
> From: Roel Kluin <roel.kluin@gmail.com>
>
> The variables are unsigned so the test `>= 0' is always true,
> the `< 0' test always fails. The other part of
> the test catches wrapped values.
This is an excellent example of why I think that some gcc warnings are
pure and utter sh*t, and why just blindly trying to avoid them then leads
to worse code.
> - if (datalen < 0 || datalen > PAGE_SIZE - 1)
> + if (datalen > PAGE_SIZE - 1)
> - if (fstop < 0 || fstop >= cache->fcull_percent)
> + if (fstop >= cache->fcull_percent)
> - if (bstop < 0 || bstop >= cache->bcull_percent)
> + if (bstop >= cache->bcull_percent)
You've now actively made the code more fragile, only to avoid a warning.
The old code was clearly correct. The new code subtle depends on the type
of comparison.
I _hate_ those idiotic warnings, and in this case the "warning-free" code
is actively worse than the original.
A smart compiler would see that it's a range check, and one that could
have been done as an unsigned comparison (well, for the constant compare
case) regardless of the type of the variable being tested. So a _smart_
compiler wouldn't complain, but it might use the signedness information to
silently simplify the comparison.
A _stupid_ compiler complains, and thus forces people to either ignore the
warning, or make the code worse.
Which one would you prefer?
Linus
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2009-10-29 15:31 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-10-23 16:30 [PATCH] CacheFiles: Cleanup redundant tests on unsigned Roel Kluin
-- strict thread matches above, loose matches on Subject: below --
2009-10-26 15:58 David Howells
2009-10-29 15:31 ` Linus Torvalds
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.