Fixed GetEnvironmentStrings test

Some os return an invalid/wrong character count if a string with
unknown symbols is printed to stdout.
Instead use the sprintf family and print to a buffer / determine
the required buffer size.
This commit is contained in:
Armin Novak
2019-11-21 10:33:45 +01:00
parent 67dc2e0bdb
commit 53f7d023b1
2 changed files with 10 additions and 2 deletions

View File

@@ -37,6 +37,7 @@ typedef CHAR TCHAR;
#ifdef UNICODE
#define _tprintf wprintf
#define _sntprintf snwprintf
#define _tcslen _wcslen
#define _tcsdup _wcsdup
#define _tcscmp wcscmp
@@ -50,6 +51,7 @@ typedef CHAR TCHAR;
#define _tcsnccmp wcsncmp
#else
#define _tprintf printf
#define _sntprintf snprintf
#define _tcslen strlen
#define _tcsdup _strdup
#define _tcscmp strcmp

View File

@@ -16,16 +16,22 @@ int TestEnvironmentGetEnvironmentStrings(int argc, char* argv[])
lpszEnvironmentBlock = GetEnvironmentStrings();
p = (TCHAR*)lpszEnvironmentBlock;
p = lpszEnvironmentBlock;
while (p[0] && p[1])
{
const int rc = _tprintf(_T("%s\n"), p);
const int rc = _sntprintf(NULL, 0, _T("%s\n"), p);
if (rc < 1)
{
_tprintf(_T("test failed: return %d\n"), rc);
goto fail;
}
length = _tcslen(p);
if (length != (size_t)(rc - 1))
{
_tprintf(_T("test failed: length %") _T(PRIuz) _T(" != %d [%s]\n"), length, rc - 1, p);
goto fail;
}
p += (length + 1);
}