From nobody@FreeBSD.org  Sun Oct 16 21:50:16 2005
Return-Path: <nobody@FreeBSD.org>
Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125])
	by hub.freebsd.org (Postfix) with ESMTP id 51A2F16A420
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 16 Oct 2005 21:50:16 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (www.freebsd.org [216.136.204.117])
	by mx1.FreeBSD.org (Postfix) with ESMTP id 84E6443D73
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 16 Oct 2005 21:50:12 +0000 (GMT)
	(envelope-from nobody@FreeBSD.org)
Received: from www.freebsd.org (localhost [127.0.0.1])
	by www.freebsd.org (8.13.1/8.13.1) with ESMTP id j9GLoCtm040328
	for <freebsd-gnats-submit@FreeBSD.org>; Sun, 16 Oct 2005 21:50:12 GMT
	(envelope-from nobody@www.freebsd.org)
Received: (from nobody@localhost)
	by www.freebsd.org (8.13.1/8.13.1/Submit) id j9GLoCAt040327;
	Sun, 16 Oct 2005 21:50:12 GMT
	(envelope-from nobody)
Message-Id: <200510162150.j9GLoCAt040327@www.freebsd.org>
Date: Sun, 16 Oct 2005 21:50:12 GMT
From: David Leppik <dleppik@vocalabs.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: LIST_*, TAILQ_* man pages include memory leak in sample code
X-Send-Pr-Version: www-2.3

>Number:         87548
>Category:       docs
>Synopsis:       LIST_*, TAILQ_* man pages include memory leak in sample code
>Confidential:   no
>Severity:       non-critical
>Priority:       low
>Responsible:    freebsd-doc
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          doc-bug
>Submitter-Id:   current-users
>Arrival-Date:   Sun Oct 16 22:00:27 GMT 2005
>Closed-Date:    Mon Oct 17 20:34:40 GMT 2005
>Last-Modified:  Mon Oct 17 20:34:40 GMT 2005
>Originator:     David Leppik
>Release:        None (old BSD code--see below)
>Organization:
Vocal Laboratories
>Environment:
>Description:
I've seen this on Mac OS X and Linux, so this probably affects FreeBSD as well.

The man page for TAILQ_INSERT, TAILQ_REMOVE, etc. have the following sample code:

while (head.tqh_first != NULL)
             TAILQ_REMOVE(&head, head.tqh_first, entries);

I was so shocked that I checked queue.h and sure enough, TAILQ_REMOVE does not deallocate the queue entry (nor should it.)  The same is true for the other structures mentioned in this man page.
>How-To-Repeat:

>Fix:
I'm just a lowly Java programmer, but I think the fix is:

while (head.tqh_first != NULL) {
             np = head.tqh_first;
             TAILQ_REMOVE(&head, np, entries);
             free(np);
}
>Release-Note:
>Audit-Trail:

From: John Baldwin <jhb@freebsd.org>
To: freebsd-doc@freebsd.org
Cc: David Leppik <dleppik@vocalabs.com>,
 freebsd-gnats-submit@freebsd.org
Subject: Re: docs/87548: LIST_*, TAILQ_* man pages include memory leak in sample code
Date: Mon, 17 Oct 2005 13:53:11 -0400

 On Sunday 16 October 2005 05:50 pm, David Leppik wrote:
 > >Number:         87548
 > >Category:       docs
 > >Synopsis:       LIST_*, TAILQ_* man pages include memory leak in sample
 > > code Confidential:   no
 > >Severity:       non-critical
 > >Priority:       low
 > >Responsible:    freebsd-doc
 > >State:          open
 > >Quarter:
 > >Keywords:
 > >Date-Required:
 > >Class:          doc-bug
 > >Submitter-Id:   current-users
 > >Arrival-Date:   Sun Oct 16 22:00:27 GMT 2005
 > >Closed-Date:
 > >Last-Modified:
 > >Originator:     David Leppik
 > >Release:        None (old BSD code--see below)
 > >Organization:
 >
 > Vocal Laboratories
 >
 > >Environment:
 > >Description:
 >
 > I've seen this on Mac OS X and Linux, so this probably affects FreeBSD as
 > well.
 >
 > The man page for TAILQ_INSERT, TAILQ_REMOVE, etc. have the following sample
 > code:
 >
 > while (head.tqh_first != NULL)
 >              TAILQ_REMOVE(&head, head.tqh_first, entries);
 >
 > I was so shocked that I checked queue.h and sure enough, TAILQ_REMOVE does
 > not deallocate the queue entry (nor should it.)  The same is true for the
 > other structures mentioned in this man page.
 >
 > >How-To-Repeat:
 > >
 > >Fix:
 >
 > I'm just a lowly Java programmer, but I think the fix is:
 >
 > while (head.tqh_first != NULL) {
 >              np = head.tqh_first;
 >              TAILQ_REMOVE(&head, np, entries);
 >              free(np);
 > }
 >
 
 Well, it should be something like:
 
 	while (!TAILQ_EMPTY(&head)) {
 		np = TAILQ_FIRST(&head);
 		TAILQ_REMOVE(&head, np, entries);
 		free(np);
 	}
 
 And actually, on my machine here running current this is what the examples 
 look like:
                                              /* TailQ Deletion. */
      while (!TAILQ_EMPTY(&head)) {
              n1 = TAILQ_FIRST(&head);
              TAILQ_REMOVE(&head, n1, entries);
              free(n1);
      }
                                              /* Faster TailQ Deletion. */
      n1 = TAILQ_FIRST(&head);
      while (n1 != NULL) {
              n2 = TAILQ_NEXT(n1, entries);
              free(n1);
              n1 = n2;
      }
      TAILQ_INIT(&head);
 
 Which version of FreeBSD are you seeing the memory leak in?
 
 -- 
 John Baldwin <jhb@FreeBSD.org>  <><  http://www.FreeBSD.org/~jhb/
 "Power Users Use the Power to Serve"  =  http://www.FreeBSD.org
State-Changed-From-To: open->closed 
State-Changed-By: jhb 
State-Changed-When: Mon Oct 17 20:33:38 GMT 2005 
State-Changed-Why:  
The queue(3) manual page on FreeBSD has had the calls to free() in place 
since revision 1.2 added them back in January of 1996. :) 

http://www.freebsd.org/cgi/query-pr.cgi?pr=87548 
>Unformatted:
