]> granicus.if.org Git - strace/commitdiff
Improve code readability by avoiding assignments inside if()
authorDenys Vlasenko <dvlasenk@redhat.com>
Sat, 20 Aug 2011 10:48:18 +0000 (12:48 +0200)
committerDenys Vlasenko <dvlasenk@redhat.com>
Tue, 23 Aug 2011 10:53:01 +0000 (12:53 +0200)
* 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 <dvlasenk@redhat.com>
desc.c
file.c
io.c
strace.c
syscall.c
test/procpollable.c
test/sfd.c
time.c
util.c

diff --git a/desc.c b/desc.c
index 00bf37bfe584517b4da9eee51217150eb116578c..4276a8148c0597324484e72259a13f4b21ffe2a4 100644 (file)
--- 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 38bf976441db0e020b20e7d1ca394ced8e6295e8..5d5a0feb1935a46d1eabd33c0577431982d9e2ab 100644 (file)
--- 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 7f2f243fed0cb026f8a63b03a926346b62813179..e3cad664500e032dbdb5d972c8187cb7685cfb02 100644 (file)
--- 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;
index bfa9a0d1c0369e5a5b3ab89261cc800128c96544..84bcddb1ed8579daed048aab94100f80dbbf6234 100644 (file)
--- 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
index e22e391eebdb66767b2f559194d5d1480248f6b8..cbe9f124b20e46a0bf84a9ec3cfb5c0595b0a7d0 100644 (file)
--- 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;
                }
index a841af147c9eb3f118fe76b71567628bac7c64d4..7bc5efafdf73c5d32784d8193a1b82522ea780a6 100644 (file)
@@ -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)
index b5ff847a41a228d2f7a10386e5f744594bd0f2e0..815da80bdd0019fe6f9a5b5694dc360b1b016883 100644 (file)
@@ -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 257d04b2ca09ee72d162ea552575bae1dbcf9c78..a4599f708f02c5df0bae0e96d5263c58f9a54edb 100644 (file)
--- 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 bfa2856a1d5d87e41814c54814c1b03117701db0..9ccc5c9303ae9e74fbacd6207e4e89cd71e61992 100644 (file)
--- 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;
        }