Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 22 Feb 2005 22:01:12 -0600 (CST)
From:      =?ISO-8859-1?Q?Se=E1n_C=2E_Farley?= <sean-freebsd@farley.org>
To:        freebsd-hackers@freebsd.org
Subject:   setenv/unsetenv's known memory leak
Message-ID:  <20050222173013.B26342@thor.farley.org>

next in thread | raw e-mail | index | archive | help
  This message is in MIME format.  The first part should be readable text,
  while the remaining parts are likely unreadable without MIME-aware tools.

--0-1396765616-1109117000=:26342
Content-Type: TEXT/PLAIN; CHARSET=ISO-8859-1; format=flowed
Content-Transfer-Encoding: QUOTED-PRINTABLE
Content-ID: <20050222180603.T26686@thor.farley.org>

While playing around with setenv(), I noticed that it can leak memory
when a program overwrites a variable with a larger value.  unsetenv()
will just leak memory.  All of this is documented in their man pages.

The latest PR on this (two PR's mentioned in it are closed):
http://www.freebsd.org/cgi/query-pr.cgi?pr=3Dmisc/19406

I could find no apparent reason for continuing to allow for the memory
leak.  The only reason given to allow it was to permit programs to
continue to use the environment variable retrieved by setenv() after the
program had reset or deleted it.

If a program can be assumed to have a persistent pointer, it should be
considered memory that should be freed by the program later.  Yes?
Obviously, this is not the case with the setenv()/unsetenv() API.  Also,
if the pointer is used by a program after it has been overwritten, the
program will be using the wrong value if the value was expanded in size.

After updating an environment variable, the value will be wrong (freed
memory or incorrect value) regardless if the leak is plugged or not.  It
stands to reason that fixing the leak should not be harmful.

In checking a different system, I verified that Linux does not exhibit
this leak.

Here is a test program along with a patch to stop the leak:
http://www.farley.org/freebsd/tmp/setenv.tar.bz2

Se=E1n
--=20
sean-freebsd@farley.org
--0-1396765616-1109117000=:26342--



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20050222173013.B26342>