| Summary: | Re: Memory leak in putenv | ||
|---|---|---|---|
| Product: | Base System | Reporter: | Abdul Khan <khana> |
| Component: | kern | Assignee: | 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 | ||
State Changed From-To: open->closed Followup to bin/18515. |
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