From 849e7603e9004fd292a93df64dd3524025f2987a Mon Sep 17 00:00:00 2001 From: Rich Felker Date: Sun, 16 Sep 2018 13:46:46 -0400 Subject: fix null pointer subtraction and comparison in stdio morally, for null pointers a and b, a-b, ab 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. --- src/internal/stdio_impl.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'src/internal') diff --git a/src/internal/stdio_impl.h b/src/internal/stdio_impl.h index 4afb7ea2..8c81fd53 100644 --- a/src/internal/stdio_impl.h +++ b/src/internal/stdio_impl.h @@ -100,10 +100,10 @@ hidden void __getopt_msg(const char *, const char *, const char *, size_t); #define ferror(f) ((f)->flags & F_ERR) #define getc_unlocked(f) \ - ( ((f)->rpos < (f)->rend) ? *(f)->rpos++ : __uflow((f)) ) + ( ((f)->rpos != (f)->rend) ? *(f)->rpos++ : __uflow((f)) ) #define putc_unlocked(c, f) \ - ( ((unsigned char)(c)!=(f)->lbf && (f)->wpos<(f)->wend) \ + ( ((unsigned char)(c)!=(f)->lbf && (f)->wpos!=(f)->wend) \ ? *(f)->wpos++ = (c) : __overflow((f),(c)) ) /* Caller-allocated FILE * operations */ -- cgit v1.2.1