Fixed memory leak

This commit is contained in:
Armin Novak 2019-04-05 13:17:24 +02:00
parent 9bee5e80ee
commit e42b15eb05
2 changed files with 69 additions and 67 deletions

View File

@ -6,133 +6,130 @@
int TestEnvironmentGetSetEB(int argc, char* argv[]) int TestEnvironmentGetSetEB(int argc, char* argv[])
{ {
int rc = 0;
#ifndef _WIN32 #ifndef _WIN32
char test[1024]; char test[1024];
TCHAR* p; TCHAR* p = NULL;
DWORD length; DWORD length;
LPTCH lpszEnvironmentBlock = "SHELL=123\0test=1\0test1=2\0DISPLAY=WINPR_TEST_VALUE\0\0"; LPTCH lpszEnvironmentBlock = "SHELL=123\0test=1\0test1=2\0DISPLAY=WINPR_TEST_VALUE\0\0";
LPTCH lpszEnvironmentBlockNew = NULL; LPTCH lpszEnvironmentBlockNew = NULL;
rc = -1;
/* Get length of an variable */ /* Get length of an variable */
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"DISPLAY", NULL, 0); length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", NULL, 0);
if (0 == length) if (0 == length)
return -1; return -1;
/* Get the variable itself */ /* Get the variable itself */
p = (LPSTR) malloc(length); p = (LPSTR) malloc(length);
if (!p) if (!p)
return -1; goto fail;
if (GetEnvironmentVariableEBA(lpszEnvironmentBlock,"DISPLAY", p, length) != length - 1) if (GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", p, length) != length - 1)
return -1; goto fail;
printf("GetEnvironmentVariableA(WINPR_TEST_VARIABLE) = %s\n" , p); printf("GetEnvironmentVariableA(WINPR_TEST_VARIABLE) = %s\n", p);
if (strcmp(p, "WINPR_TEST_VALUE") != 0) if (strcmp(p, "WINPR_TEST_VALUE") != 0)
{ goto fail;
free(p);
return -1;
}
free(p);
/* Get length of an non-existing variable */ /* Get length of an non-existing variable */
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"BLA", NULL, 0); length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "BLA", NULL, 0);
if (0 != length) if (0 != length)
{ {
printf("Unset variable returned\n"); printf("Unset variable returned\n");
return -1; goto fail;
} }
/* Get length of an similar called variables */ /* Get length of an similar called variables */
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"XDISPLAY", NULL, 0); length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "XDISPLAY", NULL, 0);
if (0 != length) if (0 != length)
{ {
printf("Similar named variable returned (XDISPLAY, length %d)\n", length); printf("Similar named variable returned (XDISPLAY, length %d)\n", length);
return -1; goto fail;
} }
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"DISPLAYX", NULL, 0);
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAYX", NULL, 0);
if (0 != length) if (0 != length)
{ {
printf("Similar named variable returned (DISPLAYX, length %d)\n", length); printf("Similar named variable returned (DISPLAYX, length %d)\n", length);
return -1; goto fail;
} }
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"DISPLA", NULL, 0);
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLA", NULL, 0);
if (0 != length) if (0 != length)
{ {
printf("Similar named variable returned (DISPLA, length %d)\n", length); printf("Similar named variable returned (DISPLA, length %d)\n", length);
return -1; goto fail;
} }
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"ISPLAY", NULL, 0);
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "ISPLAY", NULL, 0);
if (0 != length) if (0 != length)
{ {
printf("Similar named variable returned (ISPLAY, length %d)\n", length); printf("Similar named variable returned (ISPLAY, length %d)\n", length);
return -1; goto fail;
} }
/* Set variable in empty environment block */ /* Set variable in empty environment block */
if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5")) if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5"))
{ {
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew,"test", test, 1023)) if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
{ {
if (strcmp(test,"5") != 0) if (strcmp(test, "5") != 0)
{ goto fail;
return -1;
}
} }
else else
{ goto fail;
return -1;
}
} }
/* Clear variable */ /* Clear variable */
if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", NULL)) if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", NULL))
{ {
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew,"test", test, 1023)) if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
{ goto fail;
free(lpszEnvironmentBlockNew);
return -1;
}
else else
{ {
// not found .. this is expected // not found .. this is expected
} }
} }
free(lpszEnvironmentBlockNew); free(lpszEnvironmentBlockNew);
lpszEnvironmentBlockNew = (LPTCH) calloc(1024, sizeof(TCHAR)); lpszEnvironmentBlockNew = (LPTCH) calloc(1024, sizeof(TCHAR));
if (!lpszEnvironmentBlockNew)
return -1;
memcpy(lpszEnvironmentBlockNew,lpszEnvironmentBlock,length); if (!lpszEnvironmentBlockNew)
goto fail;
memcpy(lpszEnvironmentBlockNew, lpszEnvironmentBlock, length);
/* Set variable in empty environment block */ /* Set variable in empty environment block */
if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5")) if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5"))
{ {
if (0 != GetEnvironmentVariableEBA(lpszEnvironmentBlockNew,"testr", test, 1023)) if (0 != GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "testr", test, 1023))
{ {
printf("GetEnvironmentVariableEBA returned unset variable\n"); printf("GetEnvironmentVariableEBA returned unset variable\n");
free(lpszEnvironmentBlockNew); goto fail;
return -1;
} }
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew,"test", test, 1023))
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
{ {
if (strcmp(test,"5") != 0) if (strcmp(test, "5") != 0)
{ goto fail;
free(lpszEnvironmentBlockNew);
return -1;
}
} }
else else
{ goto fail;
free(lpszEnvironmentBlockNew);
return -1;
}
} }
rc = 0;
fail:
free(p);
free(lpszEnvironmentBlockNew); free(lpszEnvironmentBlockNew);
#endif #endif
return rc;
return 0;
} }

View File

@ -9,13 +9,13 @@
#define TEST_VALUE "WINPR_TEST_VALUE" #define TEST_VALUE "WINPR_TEST_VALUE"
int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[]) int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
{ {
int rc = -1;
DWORD nSize; DWORD nSize;
LPSTR lpBuffer; LPSTR lpBuffer = NULL;
DWORD error = 0; DWORD error = 0;
SetEnvironmentVariableA(TEST_NAME, TEST_VALUE); SetEnvironmentVariableA(TEST_NAME, TEST_VALUE);
nSize = GetEnvironmentVariableA(TEST_NAME, NULL, 0); nSize = GetEnvironmentVariableA(TEST_NAME, NULL, 0);
/* check if value returned is len + 1 ) */ /* check if value returned is len + 1 ) */
if (nSize != strlen(TEST_VALUE) + 1) if (nSize != strlen(TEST_VALUE) + 1)
{ {
@ -24,6 +24,7 @@ int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
} }
lpBuffer = (LPSTR) malloc(nSize); lpBuffer = (LPSTR) malloc(nSize);
if (!lpBuffer) if (!lpBuffer)
return -1; return -1;
@ -32,33 +33,37 @@ int TestEnvironmentSetEnvironmentVariable(int argc, char* argv[])
if (nSize != strlen(TEST_VALUE)) if (nSize != strlen(TEST_VALUE))
{ {
printf("GetEnvironmentVariableA wrong size returned\n"); printf("GetEnvironmentVariableA wrong size returned\n");
return -1; goto fail;
} }
if (strcmp(lpBuffer, TEST_VALUE) != 0) if (strcmp(lpBuffer, TEST_VALUE) != 0)
{ {
printf("GetEnvironmentVariableA returned value doesn't match\n"); printf("GetEnvironmentVariableA returned value doesn't match\n");
return -1; goto fail;
} }
nSize = GetEnvironmentVariableA("__xx__notset_",lpBuffer, nSize); nSize = GetEnvironmentVariableA("__xx__notset_", lpBuffer, nSize);
error = GetLastError(); error = GetLastError();
if (0 != nSize || ERROR_ENVVAR_NOT_FOUND != error) if (0 != nSize || ERROR_ENVVAR_NOT_FOUND != error)
{ {
printf("GetEnvironmentVariableA not found error\n"); printf("GetEnvironmentVariableA not found error\n");
return -1; goto fail;
} }
free(lpBuffer);
/* clear variable */ /* clear variable */
SetEnvironmentVariableA(TEST_NAME, NULL); SetEnvironmentVariableA(TEST_NAME, NULL);
nSize = GetEnvironmentVariableA(TEST_VALUE, NULL, 0); nSize = GetEnvironmentVariableA(TEST_VALUE, NULL, 0);
if ( 0 != nSize)
if (0 != nSize)
{ {
printf("SetEnvironmentVariableA failed to clear variable\n"); printf("SetEnvironmentVariableA failed to clear variable\n");
return -1; goto fail;
} }
return 0;
rc = 0;
fail:
free(lpBuffer);
return rc;
} }