nfs-utils: fix endptr check in closeall
Subsystems (2)
PATCH 1/1 nfs-utils: fix endptr check in closeall
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.
--- support/nfs/closeall.c | 2 +- 1 files changed, 1 insertions(+), 1 deletions(-)
support/nfs/closeall.c +1 -1
@@ -19,7 +19,7 @@ closeall(int min)while ((d =readdir (dir)) !=NULL ) {char *endp;long n =strtol (d->d_name, &endp,10 ); -if (*endp !='\0' && n >= min && n != dfd) +if (*endp =='\0' && n >= min && n != dfd) (void )close (n); }closedir (dir);
Signature · 1 line
1.6.2.2
Quoted 8 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.
In this case, that is true. In general, endptr points to the first non-numeric byte in the input string. Since we are dealing with a string that should be nothing more than a single integer, we know we got the whole string if endptr is pointing to '\0' after the conversion. However, this doesn't detect the (possibly rare) case where "" is the input string, for example.
Quoted 3 lines
I believe this check
should instead be requiring that endptr is pointing to '\0' before closing the fd.
When I use the strto* functions, I usually set errno to zero before calling, then check if it's still zero afterwards. And, if endptr points to the first byte in the string, that means there were no numerics in the input to convert.
Just a thought. It really depends on how forgiving we want this code to be. Personally, I think this function might be just a bit too clever for its own good.
Quoted 26 lines
Reported-by Chuck Lever <[email protected]>Signed-off-by Jeff Layton <[email protected]>---
support/nfs/closeall.c| 2 +-1 files changed, 1 insertions(+), 1 deletions(-)support/nfs/closeall.c +1
-1@@ -19,7 +19,7 @@ closeall(int min)while ((d =readdir (dir)) !=NULL ) {char *endp;long n =strtol (d->d_name, &endp,10 ); -if (*endp !='\0' && n >= min && n != dfd) +if (*endp =='\0' && n >= min && n != dfd) (void )close (n); }closedir (dir);Signature · 6 lines
1.6.2.2 -- To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html
--
Chuck Lever
chuck[dot]lever[at]oracle[dot]com
Quoted 26 lines · On Wed, 3 Jun 2009 14:32:25 -0400 Chuck Lever <[email protected]> wrote:
Quoted 10 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.In this case, that is true. In general, endptr points to the first non-numeric byte in the input string. Since we are dealing with a string that should be nothing more than a single integer, we know we got the whole string if endptr is pointing to '\0' after the conversion. However, this doesn't detect the (possibly rare) case where "" is the input string, for example.
Quoted 4 lines
I believe this check
should instead be requiring that endptr is pointing to '\0' before closing
the fd.
Quoted 9 lines
When I use the strto* functions, I usually set errno to zero before calling, then check if it's still zero afterwards. And, if endptr points to the first byte in the string, that means there were no numerics in the input to convert.
Just a thought. It really depends on how forgiving we want this code to be. Personally, I think this function might be just a bit too clever for its own good.
Agreed on the too clever part...
I think it makes sense to go ahead and commit this patch since it's clearly not working the way it was intended. Going forward though, we probably ought to phase this function out and just have the individual programs close the right filehandles "manually".
Quoted 34 lines
Quoted 27 lines
Reported-by Chuck Lever <[email protected]>Signed-off-by Jeff Layton <[email protected]>---
support/nfs/closeall.c| 2 +-1 files changed, 1 insertions(+), 1 deletions(-)support/nfs/closeall.c +1
-1@@ -19,7 +19,7 @@ closeall(int min)while ((d =readdir (dir)) !=NULL ) {char *endp;long n =strtol (d->d_name, &endp,10 ); -if (*endp !='\0' && n >= min && n != dfd) +if (*endp =='\0' && n >= min && n != dfd) (void )close (n); }closedir (dir);Signature · 7 lines
1.6.2.2 -- To unsubscribe from this list: send the line "unsubscribe linux-nfs" in the body of a message to [email protected] More majordomo info at http://vger.kernel.org/majordomo-info.html--
Chuck Lever
chuck[dot]lever[at]oracle[dot]com
Signature · 1 line
Jeff Layton <[email protected]>
A message that is not in the archive (sent off-list or not yet received): 20090603144551.492677d7-9yPaYZwiELC+kQycOl6kW4xkIHaj4LzF@public.gmane.org
Quoted 39 lines · Jeff Layton
Quoted 26 lines · On Wed, 3 Jun 2009 14:32:25 -0400 Chuck Lever <[email protected]> wrote:
Quoted 10 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.In this case, that is true. In general, endptr points to the first non-numeric byte in the input string. Since we are dealing with a string that should be nothing more than a single integer, we know we got the whole string if endptr is pointing to '\0' after the conversion. However, this doesn't detect the (possibly rare) case where "" is the input string, for example.
Quoted 4 lines
I believe this check
should instead be requiring that endptr is pointing to '\0' before closing
the fd.Quoted 9 lines
When I use the strto* functions, I usually set errno to zero before calling, then check if it's still zero afterwards. And, if endptr points to the first byte in the string, that means there were no numerics in the input to convert.
Just a thought. It really depends on how forgiving we want this code to be. Personally, I think this function might be just a bit too clever for its own good.
Quoted 6 lines
Agreed on the too clever part...
I think it makes sense to go ahead and commit this patch since it's clearly not working the way it was intended. Going forward though, we probably ought to phase this function out and just have the individual programs close the right filehandles "manually".
If we commit this fix now, what incentive is there to revisit and adjust individual programs? :-D
At least it should check errno here. strtol(3) returns zero if an error occurs, which is a valid file descriptor.
Quoted 22 lines
Quoted 22 lines
Quoted 22 lines
Reported-by Chuck Lever <[email protected]>Signed-off-by Jeff Layton <[email protected]>---
support/nfs/closeall.c| 2 +-1 files changed, 1 insertions(+), 1 deletions(-)support/nfs/closeall.c +1
-1@@ -19,7 +19,7 @@ closeall(int min)while ((d =readdir (dir)) !=NULL ) {char *endp;long n =strtol (d->d_name, &endp,10 ); -if (*endp !='\0' && n >= min && n != dfd) +if (*endp =='\0' && n >= min && n != dfd) (void )close (n); }closedir (dir);Signature · 1 line
1.6.2.2
Quoted 16 lines
Quoted 12 lines
Quoted 5 lines
--
To unsubscribe from this list: send the line "unsubscribe linux-nfs" in
the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html--
Chuck Lever
chuck[dot]lever[at]oracle[dot]comSignature · 1 line
Jeff Layton <[email protected]>
--
Chuck Lever
chuck[dot]lever[at]oracle[dot]com