From owner-freebsd-bugs Sat Jun 15 2:50: 8 2002 Delivered-To: freebsd-bugs@hub.freebsd.org Received: from freefall.freebsd.org (freefall.FreeBSD.org [216.136.204.21]) by hub.freebsd.org (Postfix) with ESMTP id 59D6237B415 for ; Sat, 15 Jun 2002 02:50:03 -0700 (PDT) Received: (from gnats@localhost) by freefall.freebsd.org (8.11.6/8.11.6) id g5F9o3K32437; Sat, 15 Jun 2002 02:50:03 -0700 (PDT) (envelope-from gnats) Date: Sat, 15 Jun 2002 02:50:03 -0700 (PDT) Message-Id: <200206150950.g5F9o3K32437@freefall.freebsd.org> To: freebsd-bugs@FreeBSD.org Cc: From: Bruce Evans Subject: Re: misc/39314: Memory leak in setenv Reply-To: Bruce Evans Sender: owner-freebsd-bugs@FreeBSD.ORG Precedence: bulk List-ID: List-Archive: (Web Archive) List-Help: (List Instructions) List-Subscribe: List-Unsubscribe: X-Loop: FreeBSD.org The following reply was made to PR misc/39314; it has been noted by GNATS. From: Bruce Evans To: Nathan Mower Cc: freebsd-gnats-submit@FreeBSD.ORG Subject: Re: misc/39314: Memory leak in setenv Date: Sat, 15 Jun 2002 19:47:17 +1000 (EST) On Fri, 14 Jun 2002, Nathan Mower wrote: > >Description: > There is a memory leak in setenv in the case where a name/value pair already exists and the new value is larger than the old one. Setenv does not free the old memory before it malloc's space for the new string. > >How-To-Repeat: > setenv("HOME", "/usr/home/abc", 1); > setenv("HOME", "/usr/home/abcdefghijklmnopqrstuvwxyz", 1); > >Fix: > free the old environ[offset] before malloc'ing the new one. Easier said than done. Patches to free it in previous PRs about this were not accepted, IIRC because it was considered too dangerous to free memory, since this would turn pointers previously returned by getenv() into garbage. setenv() is now specified in a standard that seems to permit freeing (POSIX.1-2001), so it might be time to revisit these patches. Bruce To Unsubscribe: send mail to majordomo@FreeBSD.org with "unsubscribe freebsd-bugs" in the body of the message