From 3f50bbaf51f0cc6e9eebae0161f526324e7a9366 Mon Sep 17 00:00:00 2001 From: Pawel Jakub Dawidek Date: Sun, 26 Jan 2020 10:51:57 +0000 Subject: Check for duplicated PID without using additional variable. Sponsored by: Fudo Security --- bin/pwait/pwait.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) (limited to 'bin') diff --git a/bin/pwait/pwait.c b/bin/pwait/pwait.c index e31db4dded1f..4507aad99f27 100644 --- a/bin/pwait/pwait.c +++ b/bin/pwait/pwait.c @@ -66,7 +66,7 @@ main(int argc, char *argv[]) int kq; struct kevent *e; int tflag, verbose; - int opt, nleft, n, i, duplicate, status; + int opt, nleft, n, i, status; long pid; char *s, *end; double timeout; @@ -135,18 +135,19 @@ main(int argc, char *argv[]) warnx("%s: bad process id", s); continue; } - duplicate = 0; - for (i = 0; i < nleft; i++) + for (i = 0; i < nleft; i++) { if (e[i].ident == (uintptr_t)pid) - duplicate = 1; - if (!duplicate) { - EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, - 0, NULL); - if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) - warn("%ld", pid); - else - nleft++; + break; + } + if (i < nleft) { + /* Duplicate. */ + continue; } + EV_SET(e + nleft, pid, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL); + if (kevent(kq, e + nleft, 1, NULL, 0, NULL) == -1) + warn("%ld", pid); + else + nleft++; } if (tflag) { -- cgit v1.2.3