Source-Changes-HG archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

[src/trunk]: src/usr.bin/pwait The proper way to validate a condition that's ...



details:   https://anonhg.NetBSD.org/src/rev/821337d11712
branches:  trunk
changeset: 355488:821337d11712
user:      dholland <dholland%NetBSD.org@localhost>
date:      Sun Jul 30 20:37:35 2017 +0000

description:
The proper way to validate a condition that's expected to be true is
to assert it, not to bolt it into the program logic in a way that will
cause strange behavior if it accidentally isn't true at some point.

diffstat:

 usr.bin/pwait/pwait.c |  9 +++++----
 1 files changed, 5 insertions(+), 4 deletions(-)

diffs (44 lines):

diff -r d8ce4ce77ac3 -r 821337d11712 usr.bin/pwait/pwait.c
--- a/usr.bin/pwait/pwait.c     Sun Jul 30 17:32:59 2017 +0000
+++ b/usr.bin/pwait/pwait.c     Sun Jul 30 20:37:35 2017 +0000
@@ -1,4 +1,4 @@
-/*     $NetBSD: pwait.c,v 1.5 2015/03/04 16:36:12 christos Exp $       */
+/*     $NetBSD: pwait.c,v 1.6 2017/07/30 20:37:35 dholland Exp $       */
 
 /*-
  * Copyright (c) 2004-2009, Jilles Tjoelker
@@ -37,7 +37,7 @@
 #ifdef __FBSDID
 __FBSDID("$FreeBSD: head/bin/pwait/pwait.c 245506 2013-01-16 18:15:25Z delphij $");
 #endif
-__RCSID("$NetBSD: pwait.c,v 1.5 2015/03/04 16:36:12 christos Exp $");
+__RCSID("$NetBSD: pwait.c,v 1.6 2017/07/30 20:37:35 dholland Exp $");
 
 #include <sys/types.h>
 #include <sys/event.h>
@@ -53,6 +53,7 @@
 #include <string.h>
 #include <sysexits.h>
 #include <unistd.h>
+#include <assert.h>
 
 static __dead void
 usage(void)
@@ -171,6 +172,7 @@
                        break;
                }
 
+               assert(n > 0);
                for (i = 0; i < n; i++) {
                        status = (int)e[i].data;
                        if (verbose) {
@@ -190,8 +192,7 @@
                                return status;
                }
                nleft -= n;
-               // n != 0 here, belt-n-suspenders...
-               if (immediately && n)
+               if (immediately)
                        break;
        }
 



Home | Main Index | Thread Index | Old Index