1
0
Fork 0

free memory for copied environ variables properly

This commit is contained in:
Jan Steemann 2012-08-06 17:38:32 +02:00
parent 61ddc95a56
commit 63f4ab1248
1 changed files with 20 additions and 6 deletions

View File

@ -325,19 +325,22 @@ void TRI_SetProcessTitle (char const* title) {
}
if (envLen > 0) {
size = environ[envLen-1] + strlen(environ[envLen-1]) - ARGV[0];
size = environ[envLen - 1] + strlen(environ[envLen - 1]) - ARGV[0];
}
else {
size = ARGV[ARGC-1] + strlen(ARGV[ARGC-1]) - ARGV[0];
size = ARGV[ARGC - 1] + strlen(ARGV[ARGC - 1]) - ARGV[0];
}
if (environ) {
char **newEnviron = malloc(envLen*sizeof(char *));
unsigned int i = -1;
char** newEnviron = TRI_Allocate(TRI_CORE_MEM_ZONE, (envLen + 1) * sizeof(char*), false);
size_t i = 0;
while (environ[++i]) {
newEnviron[i] = strdup(environ[i]);
while (environ[i]) {
newEnviron[i] = TRI_DuplicateStringZ(TRI_CORE_MEM_ZONE, environ[i]);
++i;
}
// pad with a null pointer so we know the end of the array
newEnviron[i] = NULL;
environ = newEnviron;
}
@ -391,6 +394,17 @@ void TRI_InitialiseProcess (int argc, char* argv[]) {
void TRI_ShutdownProcess () {
TRI_FreeString(TRI_CORE_MEM_ZONE, ProcessName);
if (environ) {
// free all arguments copied for environ
size_t i = 0;
while (environ[i]) {
TRI_FreeString(TRI_CORE_MEM_ZONE, environ[i]);
++i;
}
TRI_Free(TRI_CORE_MEM_ZONE, environ);
}
}
////////////////////////////////////////////////////////////////////////////////