From owner-svn-src-all@freebsd.org Thu Jan 25 23:31:05 2018 Return-Path: Delivered-To: svn-src-all@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id B2277ED5198; Thu, 25 Jan 2018 23:31:05 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from cell.glebi.us (glebi.us [96.95.210.25]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "cell.glebi.us", Issuer "cell.glebi.us" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id 47DE877C03; Thu, 25 Jan 2018 23:31:04 +0000 (UTC) (envelope-from glebius@FreeBSD.org) Received: from cell.glebi.us (localhost [127.0.0.1]) by cell.glebi.us (8.15.2/8.15.2) with ESMTPS id w0PNV3jg083027 (version=TLSv1.2 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=NO); Thu, 25 Jan 2018 15:31:03 -0800 (PST) (envelope-from glebius@FreeBSD.org) Received: (from glebius@localhost) by cell.glebi.us (8.15.2/8.15.2/Submit) id w0PNV2ht083026; Thu, 25 Jan 2018 15:31:02 -0800 (PST) (envelope-from glebius@FreeBSD.org) X-Authentication-Warning: cell.glebi.us: glebius set sender to glebius@FreeBSD.org using -f Date: Thu, 25 Jan 2018 15:31:02 -0800 From: Gleb Smirnoff To: Kristof Provost Cc: Ian Lepore , src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: Re: svn commit: r328313 - head/sys/netpfil/pf Message-ID: <20180125233102.GN8113@FreeBSD.org> References: <201801240429.w0O4THIl059440@repo.freebsd.org> <20180125001310.GJ8113@FreeBSD.org> <1516840498.42536.213.camel@freebsd.org> <8FA39DD7-FD83-49D5-B7FC-3637B42129BE@FreeBSD.org> <14734657-ABC2-437B-9830-724901066342@FreeBSD.org> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <14734657-ABC2-437B-9830-724901066342@FreeBSD.org> User-Agent: Mutt/1.9.1 (2017-09-22) X-BeenThere: svn-src-all@freebsd.org X-Mailman-Version: 2.1.25 Precedence: list List-Id: "SVN commit messages for the entire src tree \(except for " user" and " projects" \)" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 25 Jan 2018 23:31:05 -0000 On Thu, Jan 25, 2018 at 02:46:14PM +1100, Kristof Provost wrote: K> On 25 Jan 2018, at 12:08, Kristof Provost wrote: K> > On 25 Jan 2018, at 11:34, Ian Lepore wrote: K> >> On Wed, 2018-01-24 at 16:13 -0800, Gleb Smirnoff wrote: K> >>> (r328313) K> >>> K> @@ -1613,6 +1613,7 @@ int K> >>> K>  pf_unlink_state(struct pf_state *s, u_int flags) K> >>> K>  { K> >>> K>   struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(s)]; K> >>> K> + int last; K> >>> K>   K> >>> K>   if ((flags & PF_ENTER_LOCKED) == 0) K> >>> K>   PF_HASHROW_LOCK(ih); K> >>> K> @@ -1653,7 +1654,8 @@ pf_unlink_state(struct pf_state *s, u_int K> >>> flags) K> >>> K>   PF_HASHROW_UNLOCK(ih); K> >>> K>   K> >>> K>   pf_detach_state(s); K> >>> K> - refcount_release(&s->refs); K> >>> K> + last = refcount_release(&s->refs); K> >>> K> + KASSERT(last == 0, ("Incorrect state reference count")); K> >>> K>   K> >>> K>   return (pf_release_state(s)); K> >>> K>  } K> >>> K> >>> IMHO, we shouldn't emit extra code to please Coverity. We can mark K> >>> it K> >>> as a false positive in the interface. It may make sense to add a K> >>> comment K> >>> for a human to explain why return isn't checked here. K> >>> K> >> K> >> Not to mention that when KASSERT compiles to nothing, what you're K> >> left K> >> with is a "defined but not used" warning for 'last'. K> >> K> > I’d really like to keep the KASSERT(), because this is the sort of K> > thing that could go wrong, and the assertion would be helpful. K> > K> > I suppose I could wrap last in #ifdef INVARIANTS, but that’s rather K> > ugly too. K> > K> > Asserting that the refcount is at least 1 when entering K> > pf_release_state() would express the same, but that’s also K> > problematic. K> > Of course, errors should trigger the KASSERT() in refcount_release(), K> > so I think I may have convinced myself that the KASSERT() can in fact K> > be removed and replaced with (void)refcount_release() and a comment K> > explaining why this refcount_release() can never return 1. K> > K> So this: K> K> diff --git a/sys/netpfil/pf/pf.c b/sys/netpfil/pf/pf.c K> index 55ae1145835..0dbf1fe7f66 100644 K> --- a/sys/netpfil/pf/pf.c K> +++ b/sys/netpfil/pf/pf.c K> @@ -1623,7 +1623,6 @@ int K> pf_unlink_state(struct pf_state *s, u_int flags) K> { K> struct pf_idhash *ih = &V_pf_idhash[PF_IDHASH(s)]; K> - int last; K> K> if ((flags & PF_ENTER_LOCKED) == 0) K> PF_HASHROW_LOCK(ih); K> @@ -1664,8 +1663,9 @@ pf_unlink_state(struct pf_state *s, u_int flags) K> PF_HASHROW_UNLOCK(ih); K> K> pf_detach_state(s); K> - last = refcount_release(&s->refs); K> - KASSERT(last == 0, ("Incorrect state reference count")); K> + /* pf_state_insert() initialises refs to 2, so we can never K> release the K> + * last reference here, only in pf_release_state(). */ K> + (void)refcount_release(&s->refs); K> K> return (pf_release_state(s)); K> } K> K> I do assume that (void) will tell Coverity I’m deliberately ignoring K> the return value. It’s a fairly common idiom, so I’d expect it to K> understand. My vote goes for this one. A slightly better then warning when compiling w/o INVARIANTS. -- Gleb Smirnoff