diff options
author | Rich Felker <dalias@aerifal.cx> | 2018-09-16 13:46:46 -0400 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2018-09-16 14:37:22 -0400 |
commit | 849e7603e9004fd292a93df64dd3524025f2987a (patch) | |
tree | 4a8e8b168be59045998b430f7654464025affeaf /src/stdio/getdelim.c | |
parent | 5cd309f0cc3c92f3fabbaa499652a8329137c4de (diff) | |
download | musl-849e7603e9004fd292a93df64dd3524025f2987a.tar.gz musl-849e7603e9004fd292a93df64dd3524025f2987a.tar.xz musl-849e7603e9004fd292a93df64dd3524025f2987a.zip |
fix null pointer subtraction and comparison in stdio
morally, for null pointers a and b, a-b, a<b, and a>b should all be defined as 0; however, C does not define any of them. the stdio implementation makes heavy use of such pointer comparison and subtraction for buffer logic, and also uses null pos/base/end pointers to indicate that the FILE is not in the corresponding (read or write) mode ready for accesses through the buffer. all of the comparisons are fixed trivially by using != in place of the relational operators, since the opposite relation (e.g. pos>end) is logically impossible. the subtractions have been reviewed to check that they are conditional the stream being in the appropriate reading- or writing-through-buffer mode, with checks added where needed. in fgets and getdelim, the checks added should improve performance for unbuffered streams by avoiding a do-nothing call to memchr, and should be negligible for buffered streams.
Diffstat (limited to 'src/stdio/getdelim.c')
-rw-r--r-- | src/stdio/getdelim.c | 9 |
1 files changed, 7 insertions, 2 deletions
diff --git a/src/stdio/getdelim.c b/src/stdio/getdelim.c index 60c6cc18..c313775d 100644 --- a/src/stdio/getdelim.c +++ b/src/stdio/getdelim.c @@ -25,8 +25,13 @@ ssize_t getdelim(char **restrict s, size_t *restrict n, int delim, FILE *restric if (!*s) *n=0; for (;;) { - z = memchr(f->rpos, delim, f->rend - f->rpos); - k = z ? z - f->rpos + 1 : f->rend - f->rpos; + if (f->rpos != f->rend) { + z = memchr(f->rpos, delim, f->rend - f->rpos); + k = z ? z - f->rpos + 1 : f->rend - f->rpos; + } else { + z = 0; + k = 0; + } if (i+k+1 >= *n) { if (k >= SIZE_MAX/2-i) goto oom; size_t m = i+k+2; |