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 rc = 0;
#ifndef _WIN32
char test[1024];
TCHAR* p;
TCHAR* p = NULL;
DWORD length;
LPTCH lpszEnvironmentBlock = "SHELL=123\0test=1\0test1=2\0DISPLAY=WINPR_TEST_VALUE\0\0";
LPTCH lpszEnvironmentBlockNew = NULL;
rc = -1;
/* Get length of an variable */
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"DISPLAY", NULL, 0);
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", NULL, 0);
if (0 == length)
return -1;
/* Get the variable itself */
p = (LPSTR) malloc(length);
if (!p)
return -1;
goto fail;
if (GetEnvironmentVariableEBA(lpszEnvironmentBlock,"DISPLAY", p, length) != length - 1)
return -1;
if (GetEnvironmentVariableEBA(lpszEnvironmentBlock, "DISPLAY", p, length) != length - 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)
{
free(p);
return -1;
}
free(p);
goto fail;
/* Get length of an non-existing variable */
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"BLA", NULL, 0);
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "BLA", NULL, 0);
if (0 != length)
{
printf("Unset variable returned\n");
return -1;
goto fail;
}
/* Get length of an similar called variables */
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock,"XDISPLAY", NULL, 0);
length = GetEnvironmentVariableEBA(lpszEnvironmentBlock, "XDISPLAY", NULL, 0);
if (0 != 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)
{
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)
{
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)
{
printf("Similar named variable returned (ISPLAY, length %d)\n", length);
return -1;
goto fail;
}
/* Set variable in empty environment block */
if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", "5"))
{
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew,"test", test, 1023))
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
{
if (strcmp(test,"5") != 0)
{
return -1;
}
if (strcmp(test, "5") != 0)
goto fail;
}
else
{
return -1;
}
goto fail;
}
/* Clear variable */
if (SetEnvironmentVariableEBA(&lpszEnvironmentBlockNew, "test", NULL))
{
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew,"test", test, 1023))
{
free(lpszEnvironmentBlockNew);
return -1;
}
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
goto fail;
else
{
// not found .. this is expected
}
}
free(lpszEnvironmentBlockNew);
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 */
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");
free(lpszEnvironmentBlockNew);
return -1;
goto fail;
}
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew,"test", test, 1023))
if (GetEnvironmentVariableEBA(lpszEnvironmentBlockNew, "test", test, 1023))
{
if (strcmp(test,"5") != 0)
{
free(lpszEnvironmentBlockNew);
return -1;
}
if (strcmp(test, "5") != 0)
goto fail;
}
else
{
free(lpszEnvironmentBlockNew);
return -1;
}
goto fail;
}
rc = 0;
fail:
free(p);
free(lpszEnvironmentBlockNew);
#endif
return 0;
return rc;
}

View File

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