From 5d64581e106f47c474707001f924ee15ef22830b Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Sat, 20 Aug 2011 12:48:18 +0200 Subject: Improve code readability by avoiding assignments inside if() * desc.c (decode_select): Move assignment out of if() condition. * file.c (sprinttime): Likewise. (sys_getdirentries): Likewise. * io.c (sys_ioctl): Likewise. * strace.c (test_ptrace_setoptions_followfork): Likewise. (main): Likewise. (proc_open): Likewise. (detach): Likewise. (proc_poll): Likewise. (trace): Likewise. * syscall.c (qualify): Likewise. (sys_indir): Likewise. * test/procpollable.c (main): Likewise. * test/sfd.c (main): Likewise. * time.c (printtv_bitness): Likewise. (sprinttv): Likewise. (print_timespec): Likewise. (void sprint_timespec): Likewise. (printitv_bitness): Likewise. * util.c (dumpstr): Likewise. (umovestr): Likewise. (fixvfork): Likewise. Signed-off-by: Denys Vlasenko --- desc.c | 3 ++- file.c | 6 ++++-- io.c | 4 ++-- strace.c | 44 +++++++++++++++++++++++++++++--------------- syscall.c | 6 ++++-- test/procpollable.c | 6 ++++-- test/sfd.c | 3 ++- time.c | 30 ++++++++++++++++++++---------- util.c | 18 +++++++++++------- 9 files changed, 78 insertions(+), 42 deletions(-) diff --git a/desc.c b/desc.c index 00bf37b..4276a81 100644 --- a/desc.c +++ b/desc.c @@ -538,7 +538,8 @@ decode_select(struct tcb *tcp, long *args, enum bitness_t bitness) if (syserror(tcp)) return 0; - if ((nfds = tcp->u_rval) == 0) { + nfds = tcp->u_rval; + if (nfds == 0) { tcp->auxstr = "Timeout"; return RVAL_STR; } diff --git a/file.c b/file.c index 38bf976..5d5a0fe 100644 --- a/file.c +++ b/file.c @@ -738,7 +738,8 @@ sprinttime(time_t t) strcpy(buf, "0"); return buf; } - if ((tmp = localtime(&t))) + tmp = localtime(&t); + if (tmp) snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d", tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday, tmp->tm_hour, tmp->tm_min, tmp->tm_sec); @@ -2590,7 +2591,8 @@ sys_getdirentries(struct tcb *tcp) return 0; } len = tcp->u_rval; - if ((buf = malloc(len)) == NULL) { + buf = malloc(len); + if (buf == NULL) { tprintf("%#lx, %lu, %#lx", tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]); fprintf(stderr, "out of memory\n"); return 0; diff --git a/io.c b/io.c index 7f2f243..e3cad66 100644 --- a/io.c +++ b/io.c @@ -434,8 +434,8 @@ sys_ioctl(struct tcb *tcp) ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]); } else { - int ret; - if (!(ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]))) + int ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]); + if (!ret) tprintf(", %#lx", tcp->u_arg[2]); else return ret - 1; diff --git a/strace.c b/strace.c index bfa9a0d..84bcddb 100644 --- a/strace.c +++ b/strace.c @@ -744,9 +744,10 @@ test_ptrace_setoptions_followfork(void) PTRACE_O_TRACEFORK | PTRACE_O_TRACEVFORK; - if ((pid = fork()) < 0) + pid = fork(); + if (pid < 0) perror_msg_and_die("fork"); - else if (pid == 0) { + if (pid == 0) { pid = getpid(); if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0) perror_msg_and_die("%s: PTRACE_TRACEME doesn't work", @@ -1043,7 +1044,8 @@ main(int argc, char *argv[]) set_overhead(atoi(optarg)); break; case 'p': - if ((pid = atoi(optarg)) <= 0) { + pid = atoi(optarg); + if (pid <= 0) { error_msg("Invalid process id: '%s'", optarg); break; } @@ -1105,7 +1107,8 @@ main(int argc, char *argv[]) if (getuid() != 0 || geteuid() != 0) { error_msg_and_die("You must be root to use the -u option"); } - if ((pent = getpwnam(username)) == NULL) { + pent = getpwnam(username); + if (pent == NULL) { error_msg_and_die("Cannot find user '%s'", username); } run_uid = pent->pw_uid; @@ -1284,19 +1287,22 @@ proc_open(struct tcb *tcp, int attaching) #ifdef HAVE_MP_PROCFS /* Open the process pseudo-files in /proc. */ sprintf(proc, "/proc/%d/ctl", tcp->pid); - if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) { + tcp->pfd = open(proc, O_WRONLY|O_EXCL); + if (tcp->pfd < 0) { perror("strace: open(\"/proc/...\", ...)"); return -1; } set_cloexec_flag(tcp->pfd); sprintf(proc, "/proc/%d/status", tcp->pid); - if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) { + tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL); + if (tcp->pfd_stat < 0) { perror("strace: open(\"/proc/...\", ...)"); return -1; } set_cloexec_flag(tcp->pfd_stat); sprintf(proc, "/proc/%d/as", tcp->pid); - if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) { + tcp->pfd_as = open(proc, O_RDONLY|O_EXCL); + if (tcp->pfd_as < 0) { perror("strace: open(\"/proc/...\", ...)"); return -1; } @@ -1318,13 +1324,15 @@ proc_open(struct tcb *tcp, int attaching) #endif #ifdef FREEBSD sprintf(proc, "/proc/%d/regs", tcp->pid); - if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) { + tcp->pfd_reg = open(proc, O_RDONLY); + if (tcp->pfd_reg < 0) { perror("strace: open(\"/proc/.../regs\", ...)"); return -1; } if (cflag) { sprintf(proc, "/proc/%d/status", tcp->pid); - if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) { + tcp->pfd_status = open(proc, O_RDONLY); + if (tcp->pfd_status < 0) { perror("strace: open(\"/proc/.../status\", ...)"); return -1; } @@ -1662,7 +1670,8 @@ detach(struct tcb *tcp, int sig) * detached process would be left stopped (process state T). */ catch_sigstop = (tcp->flags & TCB_STARTUP); - if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) { + error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig); + if (error == 0) { /* On a clear day, you can see forever. */ } else if (errno != ESRCH) { @@ -1887,7 +1896,8 @@ proc_poll(struct pollfd *pollv, int nfds, int timeout) int n; struct proc_pollfd pollinfo; - if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0) + n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo)); + if (n < 0) return n; if (n != sizeof(struct proc_pollfd)) { error_msg_and_die("panic: short read: %d", n); @@ -2072,7 +2082,8 @@ trace(void) in_syscall = NULL; pv.fd = tcp->pfd; pv.events = POLLWANT; - if ((what = poll(&pv, 1, 1)) < 0) { + what = poll(&pv, 1, 1); + if (what < 0) { if (interrupted) return 0; continue; @@ -2102,7 +2113,8 @@ trace(void) } /* Look up `pfd' in our table. */ - if ((tcp = pfd2tcb(pfd)) == NULL) { + tcp = pfd2tcb(pfd); + if (tcp == NULL) { error_msg_and_die("unknown pfd: %u", pfd); } #ifdef POLL_HACK @@ -2175,7 +2187,8 @@ trace(void) char buf[1024]; int len; - if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) { + len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0); + if (len > 0) { buf[len] = '\0'; sscanf(buf, "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld", @@ -2374,7 +2387,8 @@ trace() } /* Look up `pid' in our table. */ - if ((tcp = pid2tcb(pid)) == NULL) { + tcp = pid2tcb(pid); + if (tcp == NULL) { #ifdef LINUX if (followfork) { /* This is needed to go with the CLONE_PTRACE diff --git a/syscall.c b/syscall.c index e22e391..cbe9f12 100644 --- a/syscall.c +++ b/syscall.c @@ -470,7 +470,8 @@ qualify(const char *s) for (i = 0; i < MAX_QUALS; i++) { qualify_one(i, opt->bitflag, !not, -1); } - if (!(copy = strdup(s))) { + copy = strdup(s); + if (!copy) { fprintf(stderr, "out of memory\n"); exit(1); } @@ -2775,7 +2776,8 @@ sys_indir(struct tcb *tcp) int i, scno, nargs; if (entering(tcp)) { - if ((scno = tcp->u_arg[0]) > nsyscalls) { + scno = tcp->u_arg[0]; + if (scno > nsyscalls) { fprintf(stderr, "Bogus syscall: %u\n", scno); return 0; } diff --git a/test/procpollable.c b/test/procpollable.c index a841af1..7bc5efa 100644 --- a/test/procpollable.c +++ b/test/procpollable.c @@ -12,14 +12,16 @@ int main(int argc, char *argv[]) FILE *pfp; struct pollfd pfd; - if ((pid = fork()) == 0) { + pid = fork(); + if (pid == 0) { pause(); exit(0); } sprintf(proc, "/proc/%d", pid); - if ((pfp = fopen(proc, "r+")) == NULL) + pfp = fopen(proc, "r+"); + if (pfp == NULL) goto fail; if (ioctl(fileno(pfp), PIOCSTOP, NULL) < 0) diff --git a/test/sfd.c b/test/sfd.c index b5ff847..815da80 100644 --- a/test/sfd.c +++ b/test/sfd.c @@ -13,7 +13,8 @@ int main(int argc, char *argv[]) sprintf(sname, "/proc/%d/stat", pid); - if ((sfd = open(sname, O_RDONLY)) == -1) { + sfd = open(sname, O_RDONLY); + if (sfd == -1) { perror(sname); return 1; } diff --git a/time.c b/time.c index 257d04b..a4599f7 100644 --- a/time.c +++ b/time.c @@ -81,7 +81,8 @@ printtv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness, int special) { struct timeval32 tv; - if ((rc = umove(tcp, addr, &tv)) >= 0) { + rc = umove(tcp, addr, &tv); + if (rc >= 0) { if (special && tv.tv_sec == 0 && tv.tv_usec == UTIME_NOW) tprintf("UTIME_NOW"); @@ -94,7 +95,8 @@ printtv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness, int special) } else { struct timeval tv; - if ((rc = umove(tcp, addr, &tv)) >= 0) { + rc = umove(tcp, addr, &tv); + if (rc >= 0) { if (special && tv.tv_sec == 0 && tv.tv_usec == UTIME_NOW) tprintf("UTIME_NOW"); @@ -128,13 +130,15 @@ sprinttv(struct tcb *tcp, long addr, enum bitness_t bitness, char *buf) { struct timeval32 tv; - if ((rc = umove(tcp, addr, &tv)) >= 0) + rc = umove(tcp, addr, &tv); + if (rc >= 0) sprintf(buf, "{%u, %u}", tv.tv_sec, tv.tv_usec); } else { struct timeval tv; - if ((rc = umove(tcp, addr, &tv)) >= 0) + rc = umove(tcp, addr, &tv); + if (rc >= 0) sprintf(buf, "{%lu, %lu}", (unsigned long) tv.tv_sec, (unsigned long) tv.tv_usec); @@ -157,7 +161,8 @@ void print_timespec(struct tcb *tcp, long addr) if (personality_wordsize[current_personality] == 4) { struct timeval32 tv; - if ((rc = umove(tcp, addr, &tv)) >= 0) + rc = umove(tcp, addr, &tv); + if (rc >= 0) tprintf("{%u, %u}", tv.tv_sec, tv.tv_usec); } else @@ -165,7 +170,8 @@ void print_timespec(struct tcb *tcp, long addr) { struct timespec ts; - if ((rc = umove(tcp, addr, &ts)) >= 0) + rc = umove(tcp, addr, &ts); + if (rc >= 0) tprintf("{%lu, %lu}", (unsigned long) ts.tv_sec, (unsigned long) ts.tv_nsec); @@ -188,7 +194,8 @@ void sprint_timespec(char *buf, struct tcb *tcp, long addr) if (personality_wordsize[current_personality] == 4) { struct timeval32 tv; - if ((rc = umove(tcp, addr, &tv)) >= 0) + rc = umove(tcp, addr, &tv); + if (rc >= 0) sprintf(buf, "{%u, %u}", tv.tv_sec, tv.tv_usec); } else @@ -196,7 +203,8 @@ void sprint_timespec(char *buf, struct tcb *tcp, long addr) { struct timespec ts; - if ((rc = umove(tcp, addr, &ts)) >= 0) + rc = umove(tcp, addr, &ts); + if (rc >= 0) sprintf(buf, "{%lu, %lu}", (unsigned long) ts.tv_sec, (unsigned long) ts.tv_nsec); @@ -349,7 +357,8 @@ printitv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness) struct timeval32 it_interval, it_value; } itv; - if ((rc = umove(tcp, addr, &itv)) >= 0) { + rc = umove(tcp, addr, &itv); + if (rc >= 0) { tprintf("{it_interval="); tprint_timeval32(tcp, &itv.it_interval); tprintf(", it_value="); @@ -359,7 +368,8 @@ printitv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness) } else { struct itimerval itv; - if ((rc = umove(tcp, addr, &itv)) >= 0) { + rc = umove(tcp, addr, &itv); + if (rc >= 0) { tprintf("{it_interval="); tprint_timeval(tcp, &itv.it_interval); tprintf(", it_value="); diff --git a/util.c b/util.c index bfa2856..9ccc5c9 100644 --- a/util.c +++ b/util.c @@ -687,9 +687,9 @@ dumpstr(struct tcb *tcp, long addr, int len) int i, j; if (strsize < len) { - if (str) - free(str); - if ((str = malloc(len)) == NULL) { + free(str); + str = malloc(len); + if (str == NULL) { fprintf(stderr, "out of memory\n"); return; } @@ -848,10 +848,13 @@ umovestr(struct tcb *tcp, long addr, int len, char *laddr) lseek(fd, addr, SEEK_SET); while (left) { - if (move > left) move = left; - if ((move = read(fd, laddr, move)) <= 0) + if (move > left) + move = left; + move = read(fd, laddr, move); + if (move <= 0) return left != len ? 0 : -1; - if (memchr(laddr, 0, move)) break; + if (memchr(laddr, 0, move)) + break; left -= move; laddr += move; addr += move; @@ -1703,7 +1706,8 @@ fixvfork(struct tcb *tcp) fprintf(stderr, "Cannot read link_dynamic_2\n"); return -1; } - if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) { + strtab = malloc((unsigned)ld.ld_symb_size); + if (strtab == NULL) { fprintf(stderr, "out of memory\n"); return -1; } -- cgit v1.2.3