Bug 18567

Summary: Re: Memory leak in putenv
Product: Base System Reporter: Abdul Khan <khana>
Component: kernAssignee: GNATS administrator <gnats-admin>
Status: Closed FIXED    
Severity: Affects Only Me CC: FreeBSD-gnats-submit
Priority: Normal    
Version: 1.0-RELEASE   
Hardware: Any   
OS: Any   

Description Abdul Khan 2000-05-15 15:30:01 UTC
 actually i had to move that class to C.. all it really is is the setENV function:
 
 
 static bool setEnvironmentVariableOneTime = false;
 
 void
 setEnvironmentVariable(char* name, 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])
     {
       if (setEnvironmentVariableOneTime)
       {
         environ[i] = (char*) realloc (environ[i], sizeof(char)*
                                      (strlen (value) + strlen (name) + 1 + 1));
         sprintf(environ[i], "%s=%s", name, value);
       }
       else  //the first time we have to create our own environ mem
       {
         int k = i;
         while (environ[k]) k++;
         char** new_environ = (char**) malloc(sizeof(char*)*(k+1));
         for(int n=0;n<k;n++)
         {
           new_environ[n] = (char*)
           malloc(sizeof(char)*(strlen(environ[n])+1));
           strcpy(new_environ[n], environ[n]);
         }
         new_environ[n]=NULL;
         new_environ[i] = (char*) realloc (new_environ[i], sizeof(char)*
                                      (strlen (value) + strlen (name) + 1 + 1));
         sprintf(new_environ[i], "%s=%s", name, value);
         clearenv();
         environ=new_environ;
       }
       break;
     }
     i++;
   }
   if (!environ[i])
   {
     char* new_env = (char*) malloc(sizeof(char)*
                         (strlen (value) + strlen (name) + 1 + 1));
     sprintf(new_env, "%s=%s", name, value);
     putenv(new_env);
   }
   else
   {
     setEnvironmentVariableOneTime = true;
   }
 }
 
 
 
 
 // the code basically copies the environment onto the heap if needed and
 // manipulates it there from then on out.
 
 abdul
Comment 1 Steve Price freebsd_committer freebsd_triage 2000-05-28 15:46:00 UTC
State Changed
From-To: open->closed

Followup to bin/18515.