All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] nfs-utils: fix endptr check in closeall (try #4)
@ 2009-06-04 11:53 Jeff Layton
  2009-06-22 16:02 ` Steve Dickson
  0 siblings, 1 reply; 2+ messages in thread
From: Jeff Layton @ 2009-06-04 11:53 UTC (permalink / raw)
  To: steved; +Cc: linux-nfs, chuck.lever, trond.myklebust

The closeall function is broken in such a way that it almost never
closes any file descriptors. It's calling strtol on the text
representation of the file descriptor, and then checking to see if the
value of *endptr is not '\0' before trying to close the file. This check
is wrong.

When strtol returns an endptr that points to a NULL byte, that indicates
that the conversion was completely successful. I believe this check
should instead be requiring that endptr is pointing to '\0' before
closing the fd.

Also, fix up the function to check for conversion errors from strtol. If
one occurs, just skip the close on that entry.

Finally, as Trond pointed out, it's unlikely that readdir will return a
blank string in d_name but that situation wouldn't be detected by the
current code. This patch adds such a check and skips the close if it
occurs.

Reported-by: Chuck Lever <chuck.lever@oracle.com>
Signed-off-by: Jeff Layton <jlayton@redhat.com>
---
 support/nfs/closeall.c |   11 ++++++++---
 1 files changed, 8 insertions(+), 3 deletions(-)

diff --git a/support/nfs/closeall.c b/support/nfs/closeall.c
index cc7fb3b..38fb162 100644
--- a/support/nfs/closeall.c
+++ b/support/nfs/closeall.c
@@ -7,19 +7,24 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <dirent.h>
+#include <errno.h>
 
 void
 closeall(int min)
 {
+	char *endp;
+	long n;
 	DIR *dir = opendir("/proc/self/fd");
+
 	if (dir != NULL) {
 		int dfd = dirfd(dir);
 		struct dirent *d;
 
 		while ((d = readdir(dir)) != NULL) {
-			char *endp;
-			long n = strtol(d->d_name, &endp, 10);
-			if (*endp != '\0' && n >= min && n != dfd)
+			errno = 0;
+			n = strtol(d->d_name, &endp, 10);
+			if (!errno && *endp == '\0' && endp != d->d_name &&
+			    n >= min && n != dfd)
 				(void) close(n);
 		}
 		closedir(dir);
-- 
1.6.0.6


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

* Re: [PATCH] nfs-utils: fix endptr check in closeall (try #4)
  2009-06-04 11:53 [PATCH] nfs-utils: fix endptr check in closeall (try #4) Jeff Layton
@ 2009-06-22 16:02 ` Steve Dickson
  0 siblings, 0 replies; 2+ messages in thread
From: Steve Dickson @ 2009-06-22 16:02 UTC (permalink / raw)
  To: Jeff Layton; +Cc: linux-nfs, chuck.lever, trond.myklebust



Jeff Layton wrote:
> The closeall function is broken in such a way that it almost never
> closes any file descriptors. It's calling strtol on the text
> representation of the file descriptor, and then checking to see if the
> value of *endptr is not '\0' before trying to close the file. This check
> is wrong.
> 
> When strtol returns an endptr that points to a NULL byte, that indicates
> that the conversion was completely successful. I believe this check
> should instead be requiring that endptr is pointing to '\0' before
> closing the fd.
> 
> Also, fix up the function to check for conversion errors from strtol. If
> one occurs, just skip the close on that entry.
> 
> Finally, as Trond pointed out, it's unlikely that readdir will return a
> blank string in d_name but that situation wouldn't be detected by the
> current code. This patch adds such a check and skips the close if it
> occurs.
> 
> Reported-by: Chuck Lever <chuck.lever@oracle.com>
> Signed-off-by: Jeff Layton <jlayton@redhat.com>
> ---
Committed....

steved.

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

end of thread, other threads:[~2009-06-22 16:06 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-06-04 11:53 [PATCH] nfs-utils: fix endptr check in closeall (try #4) Jeff Layton
2009-06-22 16:02 ` Steve Dickson

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.