Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 30 Jan 1998 14:26:52 -0800 (PST)
From:      Archie Cobbs <archie@whistle.com>
To:        freebsd-hackers@FreeBSD.ORG
Subject:   setenv() bug.. how to fix?
Message-ID:  <199801302226.OAA17656@bubba.whistle.com>

next in thread | raw e-mail | index | archive | help

I just submitted a bug (bin/5604) regarding setenv(). The main
problem is that if you overwrite a variable with a value that is
longer than the previous value, the memory malloc()'d for the
previous value is never freed. Also, unsetenv() has the same problem
-- it doesn't free the old value.

I submitted a patch to fix this, but now I realize that the patch
may not be entirely correct.. because the original contents of the
environ[] array are not actually malloc()'d, rather they are set
up by the loader (right??)  So it's inappropriate to free() an
"original" value.. but later values that get overwritten (or
unsetenv()'d) DO need to be freed...

What's the best way to properly fix this? The only way I can think
of is to keep a bit array associated with the environ[] array,
which specifies which entries are "original" and don't need to be
free'd.

Thanks,
-Archie

PS. This program demonstrates the memory leak bug:

    #include <stdlib.h>
    #define BSIZE 1024
    char buf[BSIZE + 1];
    int main(int ac, char *av[])
    {
      int	x;
      memset(buf, 'b', BSIZE);
      buf[BSIZE] = 0;
      for (x = 0; 1; x++)
      {
	buf[x % BSIZE] = 0;
	setenv("foo", buf, 1);
	buf[x % BSIZE] = 'b';
      }
      return(0);
    }


___________________________________________________________________________
Archie Cobbs   *   Whistle Communications, Inc.  *   http://www.whistle.com



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