svn commit: r328313 - head/sys/netpfil/pf

Gleb Smirnoff glebius at FreeBSD.org
Thu Jan 25 23:31:05 UTC 2018


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


More information about the svn-src-head mailing list