From da669bb2b9624949e1615a6b0f1249f25a02f3f8 Mon Sep 17 00:00:00 2001 From: Sam Hocevar Date: Thu, 15 Jan 2015 21:29:20 +0100 Subject: [PATCH] misc: C99 refactoring; put variable declarations closer to their first use --- src/libzzuf/lib-fd.c | 70 ++++++++--------- src/libzzuf/lib-mem.c | 34 ++++---- src/libzzuf/lib-signal.c | 8 +- src/libzzuf/lib-stream.c | 162 +++++++++++++++++---------------------- 4 files changed, 124 insertions(+), 150 deletions(-) diff --git a/src/libzzuf/lib-fd.c b/src/libzzuf/lib-fd.c index 32a13c5..1523d57 100644 --- a/src/libzzuf/lib-fd.c +++ b/src/libzzuf/lib-fd.c @@ -169,8 +169,9 @@ static int (*ORIG(close)) (int fd); #define ZZ_OPEN(myopen) \ do \ { \ - int mode = 0; \ LOADSYM(myopen); \ + \ + int mode = 0; \ if (oflag & O_CREAT) \ { \ va_list va; \ @@ -224,10 +225,9 @@ int NEW(__open64)(const char *file, int oflag, ...) #undef dup int NEW(dup)(int oldfd) { - int ret; - LOADSYM(dup); - ret = ORIG(dup)(oldfd); + + int ret = ORIG(dup)(oldfd); if (!_zz_ready || _zz_islocked(-1) || !_zz_iswatched(oldfd) || !_zz_isactive(oldfd)) return ret; @@ -246,10 +246,9 @@ int NEW(dup)(int oldfd) #undef dup2 int NEW(dup2)(int oldfd, int newfd) { - int ret; - LOADSYM(dup2); - ret = ORIG(dup2)(oldfd, newfd); + + int ret = ORIG(dup2)(oldfd, newfd); if (!_zz_ready || _zz_islocked(-1) || !_zz_iswatched(oldfd) || !_zz_isactive(oldfd)) return ret; @@ -273,10 +272,9 @@ int NEW(dup2)(int oldfd, int newfd) #undef accept int NEW(accept)(int sockfd, SOCKADDR_T *addr, SOCKLEN_T *addrlen) { - int ret; - LOADSYM(accept); - ret = ORIG(accept)(sockfd, addr, addrlen); + + int ret = ORIG(accept)(sockfd, addr, addrlen); if (!_zz_ready || _zz_islocked(-1) || !_zz_network || !_zz_iswatched(sockfd) || !_zz_isactive(sockfd)) return ret; @@ -305,6 +303,7 @@ int NEW(accept)(int sockfd, SOCKADDR_T *addr, SOCKLEN_T *addrlen) do \ { \ LOADSYM(myconnect); \ + \ ret = ORIG(myconnect)(sockfd, addr, addrlen); \ if (!_zz_ready || _zz_islocked(-1) || !_zz_network) \ return ret; \ @@ -357,10 +356,9 @@ int NEW(connect)(int sockfd, const SOCKADDR_T *serv_addr, #undef socket int NEW(socket)(int domain, int type, int protocol) { - int ret; - LOADSYM(socket); - ret = ORIG(socket)(domain, type, protocol); + + int ret = ORIG(socket)(domain, type, protocol); if (!_zz_ready || _zz_islocked(-1) || !_zz_network) return ret; @@ -378,6 +376,7 @@ int NEW(socket)(int domain, int type, int protocol) do \ { \ LOADSYM(myrecv); \ + \ ret = ORIG(myrecv) myargs; \ if (!_zz_ready || !_zz_iswatched(s) || !_zz_hostwatched(s) \ || _zz_islocked(s) || !_zz_isactive(s)) \ @@ -420,6 +419,7 @@ RECV_T NEW(__recv_chk)(int s, void *buf, size_t len, size_t buflen, int flags) do \ { \ LOADSYM(myrecvfrom); \ + \ ret = ORIG(myrecvfrom) myargs; \ if (!_zz_ready || !_zz_iswatched(s) || !_zz_hostwatched(s) \ || _zz_islocked(s) || !_zz_isactive(s)) \ @@ -474,10 +474,9 @@ RECV_T NEW(__recvfrom_chk)(int s, void *buf, size_t len, size_t buflen, #undef recvmsg RECV_T NEW(recvmsg)(int s, struct msghdr *hdr, int flags) { - ssize_t ret; - LOADSYM(recvmsg); - ret = ORIG(recvmsg)(s, hdr, flags); + + ssize_t ret = ORIG(recvmsg)(s, hdr, flags); if (!_zz_ready || !_zz_iswatched(s) || !_zz_hostwatched(s) || _zz_islocked(s) || !_zz_isactive(s)) return ret; @@ -493,6 +492,7 @@ RECV_T NEW(recvmsg)(int s, struct msghdr *hdr, int flags) do \ { \ LOADSYM(myread); \ + \ ret = ORIG(myread) myargs; \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_hostwatched(fd) \ || _zz_islocked(fd) || !_zz_isactive(fd)) \ @@ -541,10 +541,9 @@ ssize_t NEW(__read_chk)(int fd, void *buf, size_t count, size_t buflen) #undef readv ssize_t NEW(readv)(int fd, const struct iovec *iov, int count) { - ssize_t ret; - LOADSYM(readv); - ret = ORIG(readv)(fd, iov, count); + + ssize_t ret = ORIG(readv)(fd, iov, count); if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) || !_zz_isactive(fd)) return ret; @@ -561,10 +560,9 @@ ssize_t NEW(readv)(int fd, const struct iovec *iov, int count) #undef pread ssize_t NEW(pread)(int fd, void *buf, size_t count, off_t offset) { - int ret; - LOADSYM(pread); - ret = ORIG(pread)(fd, buf, count, offset); + + int ret = ORIG(pread)(fd, buf, count, offset); if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) || !_zz_isactive(fd)) return ret; @@ -598,6 +596,7 @@ ssize_t NEW(pread)(int fd, void *buf, size_t count, off_t offset) do \ { \ LOADSYM(mylseek); \ + \ ret = ORIG(mylseek)(fd, offset, whence); \ if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \ || !_zz_isactive(fd)) \ @@ -636,15 +635,14 @@ off64_t NEW(__lseek64)(int fd, off64_t offset, int whence) #undef aio_read int NEW(aio_read)(struct aiocb *aiocbp) { - int ret; - int fd = aiocbp->aio_fildes; - LOADSYM(aio_read); + + int fd = aiocbp->aio_fildes; if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) return ORIG(aio_read)(aiocbp); _zz_lockfd(fd); - ret = ORIG(aio_read)(aiocbp); + int ret = ORIG(aio_read)(aiocbp); debug("%s({%i, %i, %i, %p, %li, ..., %li}) = %i", __func__, fd, aiocbp->aio_lio_opcode, aiocbp->aio_reqprio, aiocbp->aio_buf, @@ -656,14 +654,13 @@ int NEW(aio_read)(struct aiocb *aiocbp) #undef aio_return ssize_t NEW(aio_return)(struct aiocb *aiocbp) { - ssize_t ret; - int fd = aiocbp->aio_fildes; - LOADSYM(aio_return); + + int fd = aiocbp->aio_fildes; if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd)) return ORIG(aio_return)(aiocbp); - ret = ORIG(aio_return)(aiocbp); + ssize_t ret = ORIG(aio_return)(aiocbp); _zz_unlock(fd); /* FIXME: make sure we’re actually *reading* */ @@ -686,14 +683,13 @@ ssize_t NEW(aio_return)(struct aiocb *aiocbp) #undef close int NEW(close)(int fd) { - int ret; + LOADSYM(close); /* Hey, it’s our debug channel! Silently pretend we closed it. */ if (fd == _zz_debugfd) return 0; - LOADSYM(close); - ret = ORIG(close)(fd); + int ret = ORIG(close)(fd); if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd)) return ret; @@ -731,13 +727,13 @@ static void offset_check(int fd) { int orig_errno = errno; #if defined HAVE_LSEEK64 - off64_t ret; LOADSYM(lseek64); - ret = ORIG(lseek64)(fd, 0, SEEK_CUR); + + off64_t ret = ORIG(lseek64)(fd, 0, SEEK_CUR); #else - off_t ret; LOADSYM(lseek); - ret = ORIG(lseek)(fd, 0, SEEK_CUR); + + off_t ret = ORIG(lseek)(fd, 0, SEEK_CUR); #endif if (ret != -1 && ret != _zz_getpos(fd)) debug("warning: offset inconsistency"); diff --git a/src/libzzuf/lib-mem.c b/src/libzzuf/lib-mem.c index e0769d3..2c1286e 100644 --- a/src/libzzuf/lib-mem.c +++ b/src/libzzuf/lib-mem.c @@ -156,7 +156,6 @@ void _zz_mem_init(void) #undef calloc void *NEW(calloc)(size_t nmemb, size_t size) { - void *ret; if (!ORIG(calloc)) { /* Store the chunk length just before the buffer we'll return */ @@ -164,14 +163,15 @@ void *NEW(calloc)(size_t nmemb, size_t size) memcpy(dummy_buffer + dummy_offset, &lsize, sizeof(size_t)); dummy_offset++; - ret = dummy_buffer + dummy_offset; + void *ret = dummy_buffer + dummy_offset; memset(ret, 0, nmemb * size); dummy_offset += (nmemb * size + DUMMY_ALIGNMENT - 1) / DUMMY_ALIGNMENT; debug("%s(%li, %li) = %p", __func__, (long int)nmemb, (long int)size, ret); return ret; } - ret = ORIG(calloc)(nmemb, size); + + void *ret = ORIG(calloc)(nmemb, size); if (ret == NULL && _zz_memory && errno == ENOMEM) raise(SIGKILL); return ret; @@ -219,7 +219,6 @@ void NEW(free)(void *ptr) #undef realloc void *NEW(realloc)(void *ptr, size_t size) { - void *ret; if (!ORIG(realloc) || ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP)) { @@ -229,7 +228,7 @@ void *NEW(realloc)(void *ptr, size_t size) memcpy(dummy_buffer + dummy_offset, &size, sizeof(size_t)); dummy_offset++; - ret = dummy_buffer + dummy_offset; + void *ret = dummy_buffer + dummy_offset; if ((uintptr_t)ptr >= DUMMY_START && (uintptr_t)ptr < DUMMY_STOP) memcpy(&oldsize, (DUMMY_TYPE *)ptr - 1, sizeof(size_t)); else @@ -239,8 +238,10 @@ void *NEW(realloc)(void *ptr, size_t size) debug("%s(%p, %li) = %p", __func__, ptr, (long int)size, ret); return ret; } + LOADSYM(realloc); - ret = ORIG(realloc)(ptr, size); + + void *ret = ORIG(realloc)(ptr, size); if (_zz_memory && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); @@ -251,9 +252,9 @@ void *NEW(realloc)(void *ptr, size_t size) #undef valloc void *NEW(valloc)(size_t size) { - void *ret; LOADSYM(valloc); - ret = ORIG(valloc)(size); + + void *ret = ORIG(valloc)(size); if (_zz_memory && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); @@ -265,9 +266,9 @@ void *NEW(valloc)(size_t size) #undef memalign void *NEW(memalign)(size_t boundary, size_t size) { - void *ret; LOADSYM(memalign); - ret = ORIG(memalign)(boundary, size); + + void *ret = ORIG(memalign)(boundary, size); if (_zz_memory && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); @@ -279,9 +280,9 @@ void *NEW(memalign)(size_t boundary, size_t size) #undef posix_memalign int NEW(posix_memalign)(void **memptr, size_t alignment, size_t size) { - int ret; LOADSYM(posix_memalign); - ret = ORIG(posix_memalign)(memptr, alignment, size); + + int ret = ORIG(posix_memalign)(memptr, alignment, size); if (_zz_memory && ((!ret && errno == ENOMEM) || (ret && memory_exceeded()))) raise(SIGKILL); @@ -295,8 +296,9 @@ int nbmaps = 0; #define ZZ_MMAP(mymmap, off_t) \ do { \ - char *b = MAP_FAILED; \ LOADSYM(mymmap); \ + \ + char *b = MAP_FAILED; \ if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) \ || !_zz_isactive(fd)) \ return ORIG(mymmap)(start, length, prot, flags, fd, offset); \ @@ -368,6 +370,7 @@ void *NEW(mmap64)(void *start, size_t length, int prot, int flags, int NEW(munmap)(void *start, size_t length) { LOADSYM(munmap); + for (int i = 0; i < nbmaps; ++i) { if (maps[i] != start) @@ -390,10 +393,9 @@ int NEW(munmap)(void *start, size_t length) kern_return_t NEW(map_fd)(int fd, vm_offset_t offset, vm_offset_t *addr, boolean_t find_space, vm_size_t numbytes) { - kern_return_t ret; - LOADSYM(map_fd); - ret = ORIG(map_fd)(fd, offset, addr, find_space, numbytes); + + kern_return_t ret = ORIG(map_fd)(fd, offset, addr, find_space, numbytes); if (!_zz_ready || !_zz_iswatched(fd) || _zz_islocked(fd) || !_zz_isactive(fd)) return ret; diff --git a/src/libzzuf/lib-signal.c b/src/libzzuf/lib-signal.c index 2495c1f..b3675cf 100644 --- a/src/libzzuf/lib-signal.c +++ b/src/libzzuf/lib-signal.c @@ -93,14 +93,12 @@ static int isfatal(int signum) #undef signal SIG_T NEW(signal)(int signum, SIG_T handler) { - SIG_T ret; - LOADSYM(signal); if (!_zz_signal) return ORIG(signal)(signum, handler); - ret = ORIG(signal)(signum, isfatal(signum) ? SIG_DFL : handler); + SIG_T ret = ORIG(signal)(signum, isfatal(signum) ? SIG_DFL : handler); debug("%s(%i, %p) = %p", __func__, signum, handler, ret); @@ -112,13 +110,13 @@ SIG_T NEW(signal)(int signum, SIG_T handler) int NEW(sigaction)(int signum, const struct sigaction *act, struct sigaction *oldact) { - int ret; - LOADSYM(sigaction); if (!_zz_signal) return ORIG(sigaction)(signum, act, oldact); + int ret; + if (act && isfatal(signum)) { struct sigaction newact; diff --git a/src/libzzuf/lib-stream.c b/src/libzzuf/lib-stream.c index 4ee1058..d53ab13 100644 --- a/src/libzzuf/lib-stream.c +++ b/src/libzzuf/lib-stream.c @@ -265,6 +265,7 @@ static inline void debug_stream(char const *prefix, FILE *stream) do \ { \ LOADSYM(myfopen); \ + \ if (!_zz_ready) \ return ORIG(myfopen)(path, mode); \ _zz_lockfd(-1); \ @@ -283,8 +284,9 @@ static inline void debug_stream(char const *prefix, FILE *stream) #define ZZ_FREOPEN(myfreopen) \ do \ { \ - int fd0 = -1, fd1 = -1, disp = 0; \ LOADSYM(myfreopen); \ + \ + int fd0 = -1, fd1 = -1, disp = 0; \ if (_zz_ready && (fd0 = fileno(stream)) >= 0 && _zz_iswatched(fd0)) \ { \ _zz_unregister(fd0); \ @@ -363,23 +365,21 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream) #define ZZ_FSEEK(myfseek) \ do \ { \ - int64_t oldpos, newpos; \ - int oldoff, oldcnt; \ - int fd; \ LOADSYM(myfseek); \ - fd = fileno(stream); \ + \ + int fd = fileno(stream); \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \ || _zz_islocked(fd)) \ return ORIG(myfseek)(stream, offset, whence); \ debug_stream("before", stream); \ /* FIXME: ftell() will return -1 on a pipe such as stdin */ \ - oldpos = ZZ_FTELL(stream); \ - oldoff = get_stream_off(stream); \ - oldcnt = get_stream_cnt(stream); \ + int64_t oldpos = ZZ_FTELL(stream); \ + int oldoff = get_stream_off(stream); \ + int oldcnt = get_stream_cnt(stream); \ _zz_lockfd(fd); \ ret = ORIG(myfseek)(stream, offset, whence); \ _zz_unlock(fd); \ - newpos = ZZ_FTELL(stream); \ + int64_t newpos = ZZ_FTELL(stream); \ if (newpos >= oldpos + oldcnt || newpos < oldpos - oldoff) \ { \ _zz_setpos(fd, newpos - get_stream_off(stream)); \ @@ -401,23 +401,21 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream) #define ZZ_FSETPOS(myfsetpos) \ do \ { \ - int64_t oldpos, newpos; \ - int oldoff, oldcnt; \ - int fd; \ LOADSYM(myfsetpos); \ - fd = fileno(stream); \ + \ + int fd = fileno(stream); \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \ || _zz_islocked(fd)) \ return ORIG(myfsetpos)(stream, pos); \ debug_stream("before", stream); \ /* FIXME: ftell() will return -1 on a pipe such as stdin */ \ - oldpos = ZZ_FTELL(stream); \ - oldoff = get_stream_off(stream); \ - oldcnt = get_stream_cnt(stream); \ + int64_t oldpos = ZZ_FTELL(stream); \ + int oldoff = get_stream_off(stream); \ + int oldcnt = get_stream_cnt(stream); \ _zz_lockfd(fd); \ ret = ORIG(myfsetpos)(stream, pos); \ _zz_unlock(fd); \ - newpos = ZZ_FTELL(stream); \ + int64_t newpos = ZZ_FTELL(stream); \ if (newpos >= oldpos + oldcnt || newpos < oldpos - oldoff) \ { \ _zz_setpos(fd, newpos - get_stream_off(stream)); \ @@ -434,11 +432,9 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream) #define ZZ_REWIND(myrewind) \ do \ { \ - int64_t oldpos, newpos; \ - int oldoff, oldcnt; \ - int fd; \ LOADSYM(rewind); \ - fd = fileno(stream); \ + \ + int fd = fileno(stream); \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \ || _zz_islocked(fd)) \ { \ @@ -447,13 +443,13 @@ FILE *NEW(__freopen64)(const char *path, const char *mode, FILE *stream) } \ debug_stream("before", stream); \ /* FIXME: ftell() will return -1 on a pipe such as stdin */ \ - oldpos = ZZ_FTELL(stream); \ - oldoff = get_stream_off(stream); \ - oldcnt = get_stream_cnt(stream); \ + int64_t oldpos = ZZ_FTELL(stream); \ + int oldoff = get_stream_off(stream); \ + int oldcnt = get_stream_cnt(stream); \ _zz_lockfd(fd); \ ORIG(rewind)(stream); \ _zz_unlock(fd); \ - newpos = ZZ_FTELL(stream); \ + int64_t newpos = ZZ_FTELL(stream); \ if (newpos >= oldpos + oldcnt || newpos < oldpos - oldoff) \ { \ _zz_setpos(fd, newpos - get_stream_off(stream)); \ @@ -529,23 +525,21 @@ void NEW(rewind)(FILE *stream) #define ZZ_FREAD(myfread, myargs) /* NEW */ \ do \ { \ - int64_t oldpos, newpos; \ - uint8_t *b = (uint8_t *)ptr;\ - int oldcnt; \ - int fd; \ LOADSYM(myfread); \ - fd = fileno(stream); \ + \ + uint8_t *b = (uint8_t *)ptr; \ + int fd = fileno(stream); \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \ || _zz_islocked(fd)) \ return ORIG(myfread) myargs; \ debug_stream("before", stream); \ /* FIXME: ftell() will return -1 on a pipe such as stdin */ \ - oldpos = ZZ_FTELL(stream); \ - oldcnt = get_stream_cnt(stream); \ + int64_t oldpos = ZZ_FTELL(stream); \ + int oldcnt = get_stream_cnt(stream); \ _zz_lockfd(fd); \ ret = ORIG(myfread) myargs; \ _zz_unlock(fd); \ - newpos = ZZ_FTELL(stream); \ + int64_t newpos = ZZ_FTELL(stream); \ if (newpos >= oldpos + oldcnt) \ { \ /* Fuzz returned data that wasn't in the old internal buffer */ \ @@ -620,21 +614,19 @@ size_t NEW(__fread_unlocked_chk)(void *ptr, size_t ptrlen, size_t size, #define ZZ_FGETC(myfgetc, s, arg) \ do { \ - int64_t oldpos, newpos; \ - int oldcnt; \ - int fd; \ LOADSYM(myfgetc); \ - fd = fileno(s); \ + \ + int fd = fileno(s); \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \ || _zz_islocked(fd)) \ return ORIG(myfgetc)(arg); \ debug_stream("before", s); \ - oldpos = ZZ_FTELL(s); \ - oldcnt = get_stream_cnt(s); \ + int64_t oldpos = ZZ_FTELL(s); \ + int oldcnt = get_stream_cnt(s); \ _zz_lockfd(fd); \ ret = ORIG(myfgetc)(arg); \ _zz_unlock(fd); \ - newpos = ZZ_FTELL(s); \ + int64_t newpos = ZZ_FTELL(s); \ if (oldcnt == 0 && ret != EOF) \ { \ /* Fuzz returned data that wasn't in the old internal buffer */ \ @@ -715,20 +707,18 @@ int NEW(fgetc_unlocked)(FILE *stream) #define ZZ_FGETS(myfgets, myfgetc, myargs) \ do \ { \ - int64_t oldpos, newpos; \ - int oldcnt; \ - int fd; \ - ret = s; \ LOADSYM(myfgets); \ LOADSYM(myfgetc); \ - fd = fileno(stream); \ + \ + ret = s; \ + int fd = fileno(stream); \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \ || _zz_islocked(fd)) \ return ORIG(myfgets) myargs; \ debug_stream("before", stream); \ - oldpos = ZZ_FTELL(stream); \ - oldcnt = get_stream_cnt(stream); \ - newpos = oldpos; \ + int64_t oldpos = ZZ_FTELL(stream); \ + int oldcnt = get_stream_cnt(stream); \ + int64_t newpos = oldpos; \ if (size <= 0) \ ret = NULL; \ else if (size == 1) \ @@ -822,18 +812,17 @@ char *NEW(__fgets_unlocked_chk)(char *s, size_t ptrlen, int size, FILE *stream) #undef ungetc int NEW(ungetc)(int c, FILE *stream) { - int oldpos, ret, fd; - LOADSYM(ungetc); - fd = fileno(stream); + + int fd = fileno(stream); if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) || _zz_islocked(fd)) return ORIG(ungetc)(c, stream); debug_stream("before", stream); - oldpos = ZZ_FTELL(stream); + int oldpos = ZZ_FTELL(stream); _zz_lockfd(fd); - ret = ORIG(ungetc)(c, stream); + int ret = ORIG(ungetc)(c, stream); _zz_unlock(fd); _zz_setpos(fd, oldpos - 1); @@ -852,16 +841,15 @@ int NEW(ungetc)(int c, FILE *stream) #undef fclose int NEW(fclose)(FILE *fp) { - int ret, fd; - LOADSYM(fclose); - fd = fileno(fp); + + int fd = fileno(fp); if (!_zz_ready || !_zz_iswatched(fd)) return ORIG(fclose)(fp); debug_stream("before", fp); _zz_lockfd(fd); - ret = ORIG(fclose)(fp); + int ret = ORIG(fclose)(fp); _zz_unlock(fd); debug("%s([%i]) = %i", __func__, fd, ret); _zz_unregister(fd); @@ -875,25 +863,23 @@ int NEW(fclose)(FILE *fp) #define ZZ_GETDELIM(mygetdelim, delim, need_delim) \ do { \ - int64_t oldpos, newpos; \ - char *line; \ - ssize_t done, size; \ - int oldcnt; \ - int fd, finished = 0; \ LOADSYM(mygetdelim); \ LOADSYM(getdelim); \ LOADSYM(fgetc); \ - fd = fileno(stream); \ + \ + int fd = fileno(stream); \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \ || _zz_islocked(fd)) \ return ORIG(getdelim)(lineptr, n, delim, stream); \ debug_stream("before", stream); \ - oldpos = ZZ_FTELL(stream); \ - oldcnt = get_stream_cnt(stream); \ - newpos = oldpos; \ - line = *lineptr; \ - size = line ? *n : 0; \ - ret = done = finished = 0; \ + int64_t oldpos = ZZ_FTELL(stream); \ + int oldcnt = get_stream_cnt(stream); \ + int64_t newpos = oldpos; \ + char *line = *lineptr; \ + ssize_t size = line ? *n : 0; \ + ssize_t done = 0; \ + int finished = 0; \ + ret = 0; \ for (;;) \ { \ int chr; \ @@ -986,28 +972,24 @@ ssize_t NEW(__getdelim)(char **lineptr, size_t *n, int delim, FILE *stream) #undef fgetln char *NEW(fgetln)(FILE *stream, size_t *len) { - int64_t oldpos, newpos; - char *ret; - struct fuzz *fuzz; - size_t i, size; - int oldoff, oldcnt, fd; - LOADSYM(fgetln); LOADSYM(fgetc); - fd = fileno(stream); + + int fd = fileno(stream); if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) || _zz_islocked(fd)) return ORIG(fgetln)(stream, len); debug_stream("before", stream); - oldpos = ZZ_FTELL(stream); - oldoff = get_stream_off(stream); - oldcnt = get_stream_cnt(stream); - newpos = oldpos; + int64_t oldpos = ZZ_FTELL(stream); + int oldoff = get_stream_off(stream); + int oldcnt = get_stream_cnt(stream); + int64_t newpos = oldpos; - fuzz = _zz_getfuzz(fd); + struct fuzz *fuzz = _zz_getfuzz(fd); - for (i = size = 0; ; /* i is incremented below */) + size_t i = 0, size = 0; + do { int chr; @@ -1042,13 +1024,11 @@ char *NEW(fgetln)(FILE *stream, size_t *len) fuzz->tmp = realloc(fuzz->tmp, (size += 80)); fuzz->tmp[i] = (char)(unsigned char)chr; - - if (fuzz->tmp[i++] == '\n') - break; } + while (fuzz->tmp[i++] != '\n') *len = i; - ret = fuzz->tmp; + char *ret = fuzz->tmp; debug_stream("after", stream); debug("%s([%i], &%li) = %p", __func__, fd, (long int)*len, ret); @@ -1069,19 +1049,17 @@ char *NEW(fgetln)(FILE *stream, size_t *len) #define ZZ_REFILL(myrefill, fn_advances) \ do \ { \ - int64_t pos; \ - off_t newpos; \ - int fd; \ LOADSYM(myrefill); \ - fd = fileno(fp); \ + \ + int fd = fileno(fp); \ if (!_zz_ready || !_zz_iswatched(fd) || !_zz_isactive(fd) \ || _zz_islocked(fd)) \ return ORIG(myrefill)(fp); \ debug_stream("before", fp); \ - pos = _zz_getpos(fd); \ + int64_t pos = _zz_getpos(fd); \ _zz_lockfd(fd); \ ret = ORIG(myrefill)(fp); \ - newpos = lseek(fd, 0, SEEK_CUR); \ + off_t newpos = lseek(fd, 0, SEEK_CUR); \ _zz_unlock(fd); \ if (ret != EOF) \ { \