From nobody@FreeBSD.org  Mon May 21 09:29:48 2001
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21])
	by hub.freebsd.org (Postfix) with ESMTP id CF96C37B422
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 21 May 2001 09:29:47 -0700 (PDT)
	(envelope-from nobody@FreeBSD.org)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.11.1/8.11.1) id f4LGTlV51311;
	Mon, 21 May 2001 09:29:47 -0700 (PDT)
	(envelope-from nobody)
Message-Id: <200105211629.f4LGTlV51311@freefall.freebsd.org>
Date: Mon, 21 May 2001 09:29:47 -0700 (PDT)
From: thierry@herbelot.com
To: freebsd-gnats-submit@FreeBSD.org
Subject: memory leak in libfetch
X-Send-Pr-Version: www-1.0

>Number:         27506
>Category:       bin
>Synopsis:       memory leak in libfetch
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    des
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon May 21 09:30:01 PDT 2001
>Closed-Date:    Sun May 27 06:10:47 PDT 2001
>Last-Modified:  Sun May 27 06:11:09 PDT 2001
>Originator:     thierry herbelot
>Release:        4.3-Release
>Organization:
>Environment:
FreeBSD pc-bsd60.XXXX 4.3-RELEASE FreeBSD 4.3-RELEASE #11: 
Tue Apr 24 19:14:23 CEST 2001
herbelot@pc-bsd60.XXXXX:/usr/obj/usr/src/sys/station  i386
>Description:
I'm using libfetch for automated file transfers from a program (via ftp).

The program is running for long periods of time, and seems to be 
leaking memory (at least it's SIZE in top(1) just grows and grows - 
the swap is also increasingly used).

As there is no dynamically allocated memory in my program, one suspect 
could be libfetch(3).

Indeed, there are 5 calls to malloc(3) in the source code of the lib :
- in common.c, line 263, which should not cause a leak as the pointer 
is stored in buf, which is later saved,
- in common.c, line 362, in a function which is not used in my app 
(the file name is known)
- in fetch.c, line 377, in a function which is used only for http/https
transfers, thus not in my app,
- in http.c, line 517, in a function which is used only for http/https 
transfers, thus not in my app,
- finally in ftp.c, line 434, in a suspicious manner : the "io" 
variable is located on the stack, thus visible only from this function,
gets a pointer to a newly allocated ftpio struct and disappears after 
_ftp_setup() returns.

The comparison between usr.bin/compress/zopen.c:zclose() and 
lib/libfetch/ftp.c:_ftp_close() shows a missing free(cookie) at the 
end of the function.

>How-To-Repeat:
just having a constantly running program fetching repeated files
using libfetch (the fetch(1) program is a bad example, as it runs for
a very short period of time - my program runs for extended periods of
time, like one week, and fetches many files each second)
>Fix:
the following patch seems to cure the memory leak :
-----------------------------------------
--- ftp.c.ori	Sat Apr  7 19:30:48 2001
+++ ftp.c	Mon May 21 15:26:42 2001
@@ -422,7 +422,9 @@
 	io->err = 0;
     close(io->csd);
     io->csd = -1;
-    return io->err ? -1 : 0;
+    r = io->err ? -1 : 0;
+    free(io);
+    return (r);
 }
 
 static FILE *
-----------------------------------------


>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: freebsd-bugs->des 
Responsible-Changed-By: roam 
Responsible-Changed-When: Mon May 21 09:33:23 PDT 2001 
Responsible-Changed-Why:  
Over to the libfetch author/maintainer. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=27506 

From: Dag-Erling Smorgrav <des@ofug.org>
To: freebsd-gnats-submit@FreeBSD.org
Cc:  
Subject: Re: bin/27506: memory leak in libfetch
Date: 21 May 2001 19:59:39 +0200

 Try this patch instead of the one in the PR:
 
 Index: ftp.c
 ===================================================================
 RCS file: /home/ncvs/src/lib/libfetch/ftp.c,v
 retrieving revision 1.63
 diff -u -r1.63 ftp.c
 --- ftp.c       2001/04/24 00:06:20     1.63
 +++ ftp.c       2001/05/21 17:58:31
 @@ -418,13 +418,10 @@
      io->dir = -1;
      io->dsd = -1;
      DEBUG(fprintf(stderr, "Waiting for final status\n"));
 -    if ((r = _ftp_chkerr(io->csd)) != FTP_TRANSFER_COMPLETE)
 -       io->err = r;
 -    else
 -       io->err = 0;
 +    r = _ftp_chkerr(io->csd);
      close(io->csd);
 -    io->csd = -1;
 -    return io->err ? -1 : 0;
 +    free(io);
 +    return (r == FTP_TRANSFER_COMPLETE) ? 0 : -1;
  }
 
  static FILE *
 
 DES
 -- 
 Dag-Erling Smorgrav - des@ofug.org
State-Changed-From-To: open->feedback 
State-Changed-By: des 
State-Changed-When: Sun May 27 06:10:30 PDT 2001 
State-Changed-Why:  


http://www.FreeBSD.org/cgi/query-pr.cgi?pr=27506 
State-Changed-From-To: feedback->closed 
State-Changed-By: des 
State-Changed-When: Sun May 27 06:10:47 PDT 2001 
State-Changed-Why:  
Fixed in -CURRENT and -STABLE. 

http://www.FreeBSD.org/cgi/query-pr.cgi?pr=27506 
>Unformatted:
