Skip to contentSkip to conversation

nfs-utils: fix endptr check in closeall (try #3)

PATCH New Jeff Layton 1 participant 1 message Jun 3, 2009

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nfs-utils: fix endptr check in closeall (try #3)

Third attempt...this gets rid of the mixed declarations and code that the second patch introduced.

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.

---
 support/nfs/closeall.c |   10 +++++++---
 1 files changed, 7 insertions(+), 3 deletions(-)
support/nfs/closeall.c +7 -3
index cc7fb3b..3bdd2b6 100644
@@ -7,19 +7,23 @@
 #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' && n >= min && n != dfd)
 				(void) close(n);
 		}
 		closedir(dir);
Signature · 1 line
1.6.2.2