Browse Source

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 <dvlasenk@redhat.com>
Denys Vlasenko 8 years ago
parent
commit
5d64581e10
9 changed files with 78 additions and 42 deletions
  1. 2
    1
      desc.c
  2. 4
    2
      file.c
  3. 2
    2
      io.c
  4. 29
    15
      strace.c
  5. 4
    2
      syscall.c
  6. 4
    2
      test/procpollable.c
  7. 2
    1
      test/sfd.c
  8. 20
    10
      time.c
  9. 11
    7
      util.c

+ 2
- 1
desc.c View File

@@ -538,7 +538,8 @@ decode_select(struct tcb *tcp, long *args, enum bitness_t bitness)
538 538
 		if (syserror(tcp))
539 539
 			return 0;
540 540
 
541
-		if ((nfds = tcp->u_rval) == 0) {
541
+		nfds = tcp->u_rval;
542
+		if (nfds == 0) {
542 543
 			tcp->auxstr = "Timeout";
543 544
 			return RVAL_STR;
544 545
 		}

+ 4
- 2
file.c View File

@@ -738,7 +738,8 @@ sprinttime(time_t t)
738 738
 		strcpy(buf, "0");
739 739
 		return buf;
740 740
 	}
741
-	if ((tmp = localtime(&t)))
741
+	tmp = localtime(&t);
742
+	if (tmp)
742 743
 		snprintf(buf, sizeof buf, "%02d/%02d/%02d-%02d:%02d:%02d",
743 744
 			tmp->tm_year + 1900, tmp->tm_mon + 1, tmp->tm_mday,
744 745
 			tmp->tm_hour, tmp->tm_min, tmp->tm_sec);
@@ -2590,7 +2591,8 @@ sys_getdirentries(struct tcb *tcp)
2590 2591
 		return 0;
2591 2592
 	}
2592 2593
 	len = tcp->u_rval;
2593
-	if ((buf = malloc(len)) == NULL) {
2594
+	buf = malloc(len);
2595
+	if (buf == NULL) {
2594 2596
 		tprintf("%#lx, %lu, %#lx", tcp->u_arg[1], tcp->u_arg[2], tcp->u_arg[3]);
2595 2597
 		fprintf(stderr, "out of memory\n");
2596 2598
 		return 0;

+ 2
- 2
io.c View File

@@ -434,8 +434,8 @@ sys_ioctl(struct tcb *tcp)
434 434
 		ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
435 435
 	}
436 436
 	else {
437
-		int ret;
438
-		if (!(ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2])))
437
+		int ret = ioctl_decode(tcp, tcp->u_arg[1], tcp->u_arg[2]);
438
+		if (!ret)
439 439
 			tprintf(", %#lx", tcp->u_arg[2]);
440 440
 		else
441 441
 			return ret - 1;

+ 29
- 15
strace.c View File

@@ -744,9 +744,10 @@ test_ptrace_setoptions_followfork(void)
744 744
 					  PTRACE_O_TRACEFORK |
745 745
 					  PTRACE_O_TRACEVFORK;
746 746
 
747
-	if ((pid = fork()) < 0)
747
+	pid = fork();
748
+	if (pid < 0)
748 749
 		perror_msg_and_die("fork");
749
-	else if (pid == 0) {
750
+	if (pid == 0) {
750 751
 		pid = getpid();
751 752
 		if (ptrace(PTRACE_TRACEME, 0, 0, 0) < 0)
752 753
 			perror_msg_and_die("%s: PTRACE_TRACEME doesn't work",
@@ -1043,7 +1044,8 @@ main(int argc, char *argv[])
1043 1044
 			set_overhead(atoi(optarg));
1044 1045
 			break;
1045 1046
 		case 'p':
1046
-			if ((pid = atoi(optarg)) <= 0) {
1047
+			pid = atoi(optarg);
1048
+			if (pid <= 0) {
1047 1049
 				error_msg("Invalid process id: '%s'", optarg);
1048 1050
 				break;
1049 1051
 			}
@@ -1105,7 +1107,8 @@ main(int argc, char *argv[])
1105 1107
 		if (getuid() != 0 || geteuid() != 0) {
1106 1108
 			error_msg_and_die("You must be root to use the -u option");
1107 1109
 		}
1108
-		if ((pent = getpwnam(username)) == NULL) {
1110
+		pent = getpwnam(username);
1111
+		if (pent == NULL) {
1109 1112
 			error_msg_and_die("Cannot find user '%s'", username);
1110 1113
 		}
1111 1114
 		run_uid = pent->pw_uid;
@@ -1284,19 +1287,22 @@ proc_open(struct tcb *tcp, int attaching)
1284 1287
 #ifdef HAVE_MP_PROCFS
1285 1288
 	/* Open the process pseudo-files in /proc. */
1286 1289
 	sprintf(proc, "/proc/%d/ctl", tcp->pid);
1287
-	if ((tcp->pfd = open(proc, O_WRONLY|O_EXCL)) < 0) {
1290
+	tcp->pfd = open(proc, O_WRONLY|O_EXCL);
1291
+	if (tcp->pfd < 0) {
1288 1292
 		perror("strace: open(\"/proc/...\", ...)");
1289 1293
 		return -1;
1290 1294
 	}
1291 1295
 	set_cloexec_flag(tcp->pfd);
1292 1296
 	sprintf(proc, "/proc/%d/status", tcp->pid);
1293
-	if ((tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL)) < 0) {
1297
+	tcp->pfd_stat = open(proc, O_RDONLY|O_EXCL);
1298
+	if (tcp->pfd_stat < 0) {
1294 1299
 		perror("strace: open(\"/proc/...\", ...)");
1295 1300
 		return -1;
1296 1301
 	}
1297 1302
 	set_cloexec_flag(tcp->pfd_stat);
1298 1303
 	sprintf(proc, "/proc/%d/as", tcp->pid);
1299
-	if ((tcp->pfd_as = open(proc, O_RDONLY|O_EXCL)) < 0) {
1304
+	tcp->pfd_as = open(proc, O_RDONLY|O_EXCL);
1305
+	if (tcp->pfd_as < 0) {
1300 1306
 		perror("strace: open(\"/proc/...\", ...)");
1301 1307
 		return -1;
1302 1308
 	}
@@ -1318,13 +1324,15 @@ proc_open(struct tcb *tcp, int attaching)
1318 1324
 #endif
1319 1325
 #ifdef FREEBSD
1320 1326
 	sprintf(proc, "/proc/%d/regs", tcp->pid);
1321
-	if ((tcp->pfd_reg = open(proc, O_RDONLY)) < 0) {
1327
+	tcp->pfd_reg = open(proc, O_RDONLY);
1328
+	if (tcp->pfd_reg < 0) {
1322 1329
 		perror("strace: open(\"/proc/.../regs\", ...)");
1323 1330
 		return -1;
1324 1331
 	}
1325 1332
 	if (cflag) {
1326 1333
 		sprintf(proc, "/proc/%d/status", tcp->pid);
1327
-		if ((tcp->pfd_status = open(proc, O_RDONLY)) < 0) {
1334
+		tcp->pfd_status = open(proc, O_RDONLY);
1335
+		if (tcp->pfd_status < 0) {
1328 1336
 			perror("strace: open(\"/proc/.../status\", ...)");
1329 1337
 			return -1;
1330 1338
 		}
@@ -1662,7 +1670,8 @@ detach(struct tcb *tcp, int sig)
1662 1670
 	 * detached process would be left stopped (process state T).
1663 1671
 	 */
1664 1672
 	catch_sigstop = (tcp->flags & TCB_STARTUP);
1665
-	if ((error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig)) == 0) {
1673
+	error = ptrace(PTRACE_DETACH, tcp->pid, (char *) 1, sig);
1674
+	if (error == 0) {
1666 1675
 		/* On a clear day, you can see forever. */
1667 1676
 	}
1668 1677
 	else if (errno != ESRCH) {
@@ -1887,7 +1896,8 @@ proc_poll(struct pollfd *pollv, int nfds, int timeout)
1887 1896
 	int n;
1888 1897
 	struct proc_pollfd pollinfo;
1889 1898
 
1890
-	if ((n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo))) < 0)
1899
+	n = read(proc_poll_pipe[0], &pollinfo, sizeof(pollinfo));
1900
+	if (n < 0)
1891 1901
 		return n;
1892 1902
 	if (n != sizeof(struct proc_pollfd)) {
1893 1903
 		error_msg_and_die("panic: short read: %d", n);
@@ -2072,7 +2082,8 @@ trace(void)
2072 2082
 				in_syscall = NULL;
2073 2083
 				pv.fd = tcp->pfd;
2074 2084
 				pv.events = POLLWANT;
2075
-				if ((what = poll(&pv, 1, 1)) < 0) {
2085
+				what = poll(&pv, 1, 1);
2086
+				if (what < 0) {
2076 2087
 					if (interrupted)
2077 2088
 						return 0;
2078 2089
 					continue;
@@ -2102,7 +2113,8 @@ trace(void)
2102 2113
 		}
2103 2114
 
2104 2115
 		/* Look up `pfd' in our table. */
2105
-		if ((tcp = pfd2tcb(pfd)) == NULL) {
2116
+		tcp = pfd2tcb(pfd);
2117
+		if (tcp == NULL) {
2106 2118
 			error_msg_and_die("unknown pfd: %u", pfd);
2107 2119
 		}
2108 2120
 #ifdef POLL_HACK
@@ -2175,7 +2187,8 @@ trace(void)
2175 2187
 			char buf[1024];
2176 2188
 			int len;
2177 2189
 
2178
-			if ((len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0)) > 0) {
2190
+			len = pread(tcp->pfd_status, buf, sizeof(buf) - 1, 0);
2191
+			if (len > 0) {
2179 2192
 				buf[len] = '\0';
2180 2193
 				sscanf(buf,
2181 2194
 				       "%*s %*d %*d %*d %*d %*d,%*d %*s %*d,%*d %*d,%*d %ld,%ld",
@@ -2374,7 +2387,8 @@ trace()
2374 2387
 		}
2375 2388
 
2376 2389
 		/* Look up `pid' in our table. */
2377
-		if ((tcp = pid2tcb(pid)) == NULL) {
2390
+		tcp = pid2tcb(pid);
2391
+		if (tcp == NULL) {
2378 2392
 #ifdef LINUX
2379 2393
 			if (followfork) {
2380 2394
 				/* This is needed to go with the CLONE_PTRACE

+ 4
- 2
syscall.c View File

@@ -470,7 +470,8 @@ qualify(const char *s)
470 470
 	for (i = 0; i < MAX_QUALS; i++) {
471 471
 		qualify_one(i, opt->bitflag, !not, -1);
472 472
 	}
473
-	if (!(copy = strdup(s))) {
473
+	copy = strdup(s);
474
+	if (!copy) {
474 475
 		fprintf(stderr, "out of memory\n");
475 476
 		exit(1);
476 477
 	}
@@ -2775,7 +2776,8 @@ sys_indir(struct tcb *tcp)
2775 2776
 	int i, scno, nargs;
2776 2777
 
2777 2778
 	if (entering(tcp)) {
2778
-		if ((scno = tcp->u_arg[0]) > nsyscalls) {
2779
+		scno = tcp->u_arg[0];
2780
+		if (scno > nsyscalls) {
2779 2781
 			fprintf(stderr, "Bogus syscall: %u\n", scno);
2780 2782
 			return 0;
2781 2783
 		}

+ 4
- 2
test/procpollable.c View File

@@ -12,14 +12,16 @@ int main(int argc, char *argv[])
12 12
 	FILE *pfp;
13 13
 	struct pollfd pfd;
14 14
 
15
-	if ((pid = fork()) == 0) {
15
+	pid = fork();
16
+	if (pid == 0) {
16 17
 		pause();
17 18
 		exit(0);
18 19
 	}
19 20
 
20 21
 	sprintf(proc, "/proc/%d", pid);
21 22
 
22
-	if ((pfp = fopen(proc, "r+")) == NULL)
23
+	pfp = fopen(proc, "r+");
24
+	if (pfp == NULL)
23 25
 		goto fail;
24 26
 
25 27
 	if (ioctl(fileno(pfp), PIOCSTOP, NULL) < 0)

+ 2
- 1
test/sfd.c View File

@@ -13,7 +13,8 @@ int main(int argc, char *argv[])
13 13
 
14 14
 	sprintf(sname, "/proc/%d/stat", pid);
15 15
 
16
-	if ((sfd = open(sname, O_RDONLY)) == -1) {
16
+	sfd = open(sname, O_RDONLY);
17
+	if (sfd == -1) {
17 18
 		perror(sname);
18 19
 		return 1;
19 20
 	}

+ 20
- 10
time.c View File

@@ -81,7 +81,8 @@ printtv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness, int special)
81 81
 		{
82 82
 			struct timeval32 tv;
83 83
 
84
-			if ((rc = umove(tcp, addr, &tv)) >= 0) {
84
+			rc = umove(tcp, addr, &tv);
85
+			if (rc >= 0) {
85 86
 				if (special && tv.tv_sec == 0 &&
86 87
 				    tv.tv_usec == UTIME_NOW)
87 88
 					tprintf("UTIME_NOW");
@@ -94,7 +95,8 @@ printtv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness, int special)
94 95
 		} else {
95 96
 			struct timeval tv;
96 97
 
97
-			if ((rc = umove(tcp, addr, &tv)) >= 0) {
98
+			rc = umove(tcp, addr, &tv);
99
+			if (rc >= 0) {
98 100
 				if (special && tv.tv_sec == 0 &&
99 101
 				    tv.tv_usec == UTIME_NOW)
100 102
 					tprintf("UTIME_NOW");
@@ -128,13 +130,15 @@ sprinttv(struct tcb *tcp, long addr, enum bitness_t bitness, char *buf)
128 130
 		{
129 131
 			struct timeval32 tv;
130 132
 
131
-			if ((rc = umove(tcp, addr, &tv)) >= 0)
133
+			rc = umove(tcp, addr, &tv);
134
+			if (rc >= 0)
132 135
 				sprintf(buf, "{%u, %u}",
133 136
 					tv.tv_sec, tv.tv_usec);
134 137
 		} else {
135 138
 			struct timeval tv;
136 139
 
137
-			if ((rc = umove(tcp, addr, &tv)) >= 0)
140
+			rc = umove(tcp, addr, &tv);
141
+			if (rc >= 0)
138 142
 				sprintf(buf, "{%lu, %lu}",
139 143
 					(unsigned long) tv.tv_sec,
140 144
 					(unsigned long) tv.tv_usec);
@@ -157,7 +161,8 @@ void print_timespec(struct tcb *tcp, long addr)
157 161
 		if (personality_wordsize[current_personality] == 4) {
158 162
 			struct timeval32 tv;
159 163
 
160
-			if ((rc = umove(tcp, addr, &tv)) >= 0)
164
+			rc = umove(tcp, addr, &tv);
165
+			if (rc >= 0)
161 166
 				tprintf("{%u, %u}",
162 167
 					tv.tv_sec, tv.tv_usec);
163 168
 		} else
@@ -165,7 +170,8 @@ void print_timespec(struct tcb *tcp, long addr)
165 170
 		{
166 171
 			struct timespec ts;
167 172
 
168
-			if ((rc = umove(tcp, addr, &ts)) >= 0)
173
+			rc = umove(tcp, addr, &ts);
174
+			if (rc >= 0)
169 175
 				tprintf("{%lu, %lu}",
170 176
 					(unsigned long) ts.tv_sec,
171 177
 					(unsigned long) ts.tv_nsec);
@@ -188,7 +194,8 @@ void sprint_timespec(char *buf, struct tcb *tcp, long addr)
188 194
 		if (personality_wordsize[current_personality] == 4) {
189 195
 			struct timeval32 tv;
190 196
 
191
-			if ((rc = umove(tcp, addr, &tv)) >= 0)
197
+			rc = umove(tcp, addr, &tv);
198
+			if (rc >= 0)
192 199
 				sprintf(buf, "{%u, %u}",
193 200
 					tv.tv_sec, tv.tv_usec);
194 201
 		} else
@@ -196,7 +203,8 @@ void sprint_timespec(char *buf, struct tcb *tcp, long addr)
196 203
 		{
197 204
 			struct timespec ts;
198 205
 
199
-			if ((rc = umove(tcp, addr, &ts)) >= 0)
206
+			rc = umove(tcp, addr, &ts);
207
+			if (rc >= 0)
200 208
 				sprintf(buf, "{%lu, %lu}",
201 209
 					(unsigned long) ts.tv_sec,
202 210
 					(unsigned long) ts.tv_nsec);
@@ -349,7 +357,8 @@ printitv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness)
349 357
 				struct timeval32 it_interval, it_value;
350 358
 			} itv;
351 359
 
352
-			if ((rc = umove(tcp, addr, &itv)) >= 0) {
360
+			rc = umove(tcp, addr, &itv);
361
+			if (rc >= 0) {
353 362
 				tprintf("{it_interval=");
354 363
 				tprint_timeval32(tcp, &itv.it_interval);
355 364
 				tprintf(", it_value=");
@@ -359,7 +368,8 @@ printitv_bitness(struct tcb *tcp, long addr, enum bitness_t bitness)
359 368
 		} else {
360 369
 			struct itimerval itv;
361 370
 
362
-			if ((rc = umove(tcp, addr, &itv)) >= 0) {
371
+			rc = umove(tcp, addr, &itv);
372
+			if (rc >= 0) {
363 373
 				tprintf("{it_interval=");
364 374
 				tprint_timeval(tcp, &itv.it_interval);
365 375
 				tprintf(", it_value=");

+ 11
- 7
util.c View File

@@ -687,9 +687,9 @@ dumpstr(struct tcb *tcp, long addr, int len)
687 687
 	int i, j;
688 688
 
689 689
 	if (strsize < len) {
690
-		if (str)
691
-			free(str);
692
-		if ((str = malloc(len)) == NULL) {
690
+		free(str);
691
+		str = malloc(len);
692
+		if (str == NULL) {
693 693
 			fprintf(stderr, "out of memory\n");
694 694
 			return;
695 695
 		}
@@ -848,10 +848,13 @@ umovestr(struct tcb *tcp, long addr, int len, char *laddr)
848 848
 	lseek(fd, addr, SEEK_SET);
849 849
 
850 850
 	while (left) {
851
-		if (move > left) move = left;
852
-		if ((move = read(fd, laddr, move)) <= 0)
851
+		if (move > left)
852
+			move = left;
853
+		move = read(fd, laddr, move);
854
+		if (move <= 0)
853 855
 			return left != len ? 0 : -1;
854
-		if (memchr(laddr, 0, move)) break;
856
+		if (memchr(laddr, 0, move))
857
+			break;
855 858
 		left -= move;
856 859
 		laddr += move;
857 860
 		addr += move;
@@ -1703,7 +1706,8 @@ fixvfork(struct tcb *tcp)
1703 1706
 		fprintf(stderr, "Cannot read link_dynamic_2\n");
1704 1707
 		return -1;
1705 1708
 	}
1706
-	if ((strtab = malloc((unsigned)ld.ld_symb_size)) == NULL) {
1709
+	strtab = malloc((unsigned)ld.ld_symb_size);
1710
+	if (strtab == NULL) {
1707 1711
 		fprintf(stderr, "out of memory\n");
1708 1712
 		return -1;
1709 1713
 	}

Loading…
Cancel
Save