Skip to contentSkip to conversation

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

PATCH In review Jeff Layton 2 participants 4 messages Jun 3, 2009 2 messages not in the archive

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

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

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 |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
support/nfs/closeall.c +3 -1
index cc7fb3b..5fe07de 100644
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <dirent.h>
+#include <errno.h>
 
 void
 closeall(int min)
@@ -18,8 +19,9 @@ closeall(int min)
 
 		while ((d = readdir(dir)) != NULL) {
 			char *endp;
+			errno = 0;
 			long n = strtol(d->d_name, &endp, 10);
-			if (*endp != '\0' && n >= min && n != dfd)
+			if (!errno && *endp == '\0' && n >= min && n != dfd)
 				(void) close(n);
 		}
 		closedir(dir);
Signature · 1 line
1.6.0.6
#1 lore raw
Quoted 33 lines · Jeff Layton

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 |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
support/nfs/closeall.c +1 -0
index cc7fb3b..5fe07de 100644
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <dirent.h>
+#include <errno.h>
 
 void
 closeall(int min)

support/nfs/closeall.c closeall(int min) · line 27

Quoted 10 lines
support/nfs/closeall.c +2 -1
@@ -18,8 +19,9 @@ closeall(int min)
 
 		while ((d = readdir(dir)) != NULL) {
 			char *endp;
+			errno = 0;
 			long n = strtol(d->d_name, &endp, 10);
-			if (*endp != '\0' && n >= min && n != dfd)
+			if (!errno && *endp == '\0' && n >= min && n != dfd)
 				(void) close(n);
 		}
 		closedir(dir);

Shouldn't you also check the case endp == d->d_name? I don't think that actually sets errno...

Cheers
Trond

A message that is not in the archive (sent off-list or not yet received): 1244059421.5603.15.camel-rJ7iovZKK19ZJLDQqaL3InhyD016LWXt@public.gmane.org

#3 lore raw
Quoted 35 lines · On Wed, 03 Jun 2009 16:03:41 -0400 Trond Myklebust…
Quoted 34 lines · Jeff Layton

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 |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
support/nfs/closeall.c +1 -0
index cc7fb3b..5fe07de 100644
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <dirent.h>
+#include <errno.h>
 
 void
 closeall(int min)
@@ -18,8 +19,9 @@ closeall(int min)
 
Quoted 13 lines

support/nfs/closeall.c closeall(int min) · line 27

Quoted 9 lines
support/nfs/closeall.c +2 -1
 		while ((d = readdir(dir)) != NULL) {
 			char *endp;
+			errno = 0;
 			long n = strtol(d->d_name, &endp, 10);
-			if (*endp != '\0' && n >= min && n != dfd)
+			if (!errno && *endp == '\0' && n >= min && n != dfd)
 				(void) close(n);
 		}
 		closedir(dir);

Shouldn't you also check the case endp == d->d_name? I don't think that actually sets errno...

Hmm...for that to be a problem, readdir would have to return a name of "" (i.e. d->d_name would have to point to a NULL byte). Is that a possibility here? If not, then the *endp == '\0' check should catch that case too...

Signature · 1 line
Jeff Layton <[email protected]>

A message that is not in the archive (sent off-list or not yet received): 20090603161322.3da517bd-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org

#5 lore raw
Quoted 52 lines · Jeff Layton
Quoted 35 lines · On Wed, 03 Jun 2009 16:03:41 -0400 Trond Myklebust…
Quoted 34 lines · Jeff Layton

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 |    4 +++-
 1 files changed, 3 insertions(+), 1 deletions(-)
support/nfs/closeall.c +1 -0
index cc7fb3b..5fe07de 100644
@@ -7,6 +7,7 @@
 #include <unistd.h>
 #include <stdlib.h>
 #include <dirent.h>
+#include <errno.h>
 
 void
 closeall(int min)
@@ -18,8 +19,9 @@ closeall(int min)
 
Quoted 13 lines

support/nfs/closeall.c closeall(int min) · line 27

Quoted 9 lines
support/nfs/closeall.c +2 -1
 		while ((d = readdir(dir)) != NULL) {
 			char *endp;
+			errno = 0;
 			long n = strtol(d->d_name, &endp, 10);
-			if (*endp != '\0' && n >= min && n != dfd)
+			if (!errno && *endp == '\0' && n >= min && n != dfd)
 				(void) close(n);
 		}
 		closedir(dir);

Shouldn't you also check the case endp == d->d_name? I don't think that actually sets errno...

Quoted 4 lines

Hmm...for that to be a problem, readdir would have to return a name of "" (i.e. d->d_name would have to point to a NULL byte). Is that a possibility here? If not, then the *endp == '\0' check should catch that case too...

In theory, d->d_name cannot be an empty string.

Then again, if you believe theory, it shouldn't be necessary to check errno at all since (with exception of '.' and '..', which are caught by the *endp=='\0' test) the readdir entries in /proc/self/fd should always convert to positive integer values...

Trond