From khana@gaffer.adm.intelsat.int  Fri May 12 09:43:19 2000
Return-Path: <khana@gaffer.adm.intelsat.int>
Received: from iserv.intelsat.int (iserv.intelsat.int [164.86.102.11])
	by hub.freebsd.org (Postfix) with ESMTP id 285E837B814
	for <FreeBSD-gnats-submit@FreeBSD.org>; Fri, 12 May 2000 09:43:18 -0700 (PDT)
	(envelope-from khana@gaffer.adm.intelsat.int)
Received: from pc2.adm.intelsat.int ([164.86.36.13]) by iserv.intelsat.int
          (Post.Office MTA v3.1.2 release (PO203-101c)
          ID# 0-49113U1000L2S100) with ESMTP id AAA1204;
          Fri, 12 May 2000 12:43:07 -0400
Received: (from smap@localhost) by pc2.adm.intelsat.int (8.8.6 (PHNE_14041)/8.6.10) id MAA29449; Fri, 12 May 2000 12:43:14 -0400 (EDT)
Received: from gaffer.adm.intelsat.int(164.86.39.103) by pc2 via smap (V2.1)
	id xma029391; Fri, 12 May 00 16:42:30 GMT
Received: (from khana@localhost) by gaffer.adm.intelsat.int (8.8.6 (PHNE_17135)/8.7.1) id RAA15827; Fri, 12 May 2000 17:42:24 +0100 (BST)
Message-Id: <200005121642.RAA15827@gaffer.adm.intelsat.int>
Date: Fri, 12 May 2000 17:42:24 +0100 (BST)
From: Abdul Khan <khana@kcm.adm.intelsat.int>
Sender: khana@gaffer.adm.intelsat.int
To: FreeBSD-gnats-submit@FreeBSD.org, jeremyp@gsmx07.alcatel.com.au
Subject: Memory leak in putenv

>Number:         18515
>Category:       bin
>Synopsis:       Memory leak in putenv
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri May 12 09:50:01 PDT 2000
>Closed-Date:    Sun May 28 07:47:03 PDT 2000
>Last-Modified:  Sun May 28 07:47:44 PDT 2000
>Originator:     
>Release:        
>Organization:
>Environment:
>Description:
 I think I may have come up with a work around:
 
 #include <iostream.h>
 #include <stdlib.h>
 
 extern char** environ;
 
 class Exception
 {
 	public:
 		virtual char* getErrorMessage() = 0;
 };
 
 class NullPointerException : public Exception
 {
 	public:
 		virtual char* getErrorMessage() 
 		{
 			return "Null Pointer Exception";
 		};	
 };
 
 class EnviornmentVariable 
 {
 	public:
 
 		EnvironmentVariable(char* variable_name) 
 		{ 
 			if (!variable_name) throw NullPointerException();
 			else name = strdup(variable_name); 
 		};
 
 		void setEnv(char* value)
 		{
 			int i = 0, j = 0;
 			while (environ[i])
 			{
 				while (name[j] && environ[i][j] &&
 							name[j] == environ[i][j]) j++;
 				if (environ[i][j] == '=' && !name[j]) 
 				{
 					environ[i] = (char*) realloc (environ[i],
 											strlen (value) + strlen (name) + 1 + 1);
 					sprintf(environ[i], "%s=%s", name, value);
 					break;
 				}
 				i++;
 			}
 		}
 
 		void setEnvWithLeak(char* value)
 		{
 			char* env = (char *) malloc (strlen (value) + strlen (name) + 1 + 1);
 			sprintf(env, "%s=%s", name, value);
 			putenv(env);
 		}
 
 		virtual ~EnvironmentVariable() { free(name);};
 	private:
 		char* name;
 	};
 
 
 int main(int argc, char** argv)
 {
 char * str = 0;
 try 
 {
 	if (argc < 4) 
 	{
 		cout << "Usage: env_mem_leak <environment variable name> ";
 		cout << "<iterations> <bifurcations>" << endl;
 		cout << "Where iterations is the number of times to set the variable ";
 		cout << "and bifrucations is the number of changes in the length of "; 
 		cout << "the string that is put into the environment variable" << endl;
 	}
 	
 	EnvironmentVariable myenv(argv[1]);
 	int iterations = atoi(argv[2]);
 	int bifurcations = atoi(argv[3]);
 	str = (char*) malloc(sizeof(char)*(bifurcations+1));
 
 	for (int i=0; i < iterations; i++)
 	{
 		memset(str, 'A', i % bifurcations);
 		str[(i % bifurcations) + 1]='\0';
 
 		myenv.setEnv(str);
 			////switch these to see the memory leak problem in purify!!!!!!!!!!
 		//myenv.setEnvWithLeak(str);
 
 	}
 
 } 
 catch(Exception& e) 
 {
 	cout << endl << e.getErrorMessage() << endl;
 };
 if (str) free(str);
 return 0;
 }
 
>How-To-Repeat:
>Fix:
>Release-Note:
>Audit-Trail:
Responsible-Changed-From-To: gnats-admin->freebsd-bugs 
Responsible-Changed-By: asmodai 
Responsible-Changed-When: Sat May 13 03:57:23 PDT 2000 
Responsible-Changed-Why:  
Misfiled/botched PR.  Severity and priority left at default value. 
Memory leaks are IMHO serious. 
State-Changed-From-To: open->closed 
State-Changed-By: steve 
State-Changed-When: Sun May 28 07:47:03 PDT 2000 
State-Changed-Why:  
See also bin/18519. 

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