kernel32: Fix GetComputerNameA to not count trailing NULL, with test.

This commit is contained in:
Alasdair Sinclair 2007-03-03 03:03:53 +00:00 committed by Alexandre Julliard
parent f279252189
commit 81c922398e
2 changed files with 16 additions and 6 deletions

View File

@ -371,7 +371,7 @@ out:
BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size) BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
{ {
WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ]; WCHAR nameW[ MAX_COMPUTERNAME_LENGTH + 1 ];
DWORD sizeW = MAX_COMPUTERNAME_LENGTH; DWORD sizeW = MAX_COMPUTERNAME_LENGTH + 1;
unsigned int len; unsigned int len;
BOOL ret; BOOL ret;
@ -381,17 +381,16 @@ BOOL WINAPI GetComputerNameA(LPSTR name, LPDWORD size)
/* for compatibility with Win9x */ /* for compatibility with Win9x */
__TRY __TRY
{ {
if ( *size < len + 1 ) if ( *size < len )
{ {
*size = len + 1; *size = len;
SetLastError( ERROR_MORE_DATA ); SetLastError( ERROR_MORE_DATA );
ret = FALSE; ret = FALSE;
} }
else else
{ {
WideCharToMultiByte ( CP_ACP, 0, nameW, -1, name, len, NULL, 0 ); WideCharToMultiByte ( CP_ACP, 0, nameW, -1, name, len, NULL, 0 );
name[len] = 0; *size = len - 1;
*size = len;
ret = TRUE; ret = TRUE;
} }
} }

View File

@ -239,6 +239,7 @@ static void test_GetComputerName(void)
LPSTR name; LPSTR name;
LPWSTR nameW; LPWSTR nameW;
DWORD error; DWORD error;
int name_len;
size = 0; size = 0;
ret = GetComputerNameA((LPSTR)0xdeadbeef, &size); ret = GetComputerNameA((LPSTR)0xdeadbeef, &size);
@ -250,6 +251,16 @@ static void test_GetComputerName(void)
ok(ret, "GetComputerNameA failed with error %d\n", GetLastError()); ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
HeapFree(GetProcessHeap(), 0, name); HeapFree(GetProcessHeap(), 0, name);
size = MAX_COMPUTERNAME_LENGTH + 1;
name = HeapAlloc(GetProcessHeap(), 0, size * sizeof(name[0]));
ok(name != NULL, "HeapAlloc failed with error %d\n", GetLastError());
ret = GetComputerNameA(name, &size);
ok(ret, "GetComputerNameA failed with error %d\n", GetLastError());
trace("computer name is \"%s\"\n", name);
name_len = strlen(name);
ok(size == name_len, "size should be same as length, name_len=%d, size=%d\n", name_len, size);
HeapFree(GetProcessHeap(), 0, name);
size = 0; size = 0;
ret = GetComputerNameW((LPWSTR)0xdeadbeef, &size); ret = GetComputerNameW((LPWSTR)0xdeadbeef, &size);
error = GetLastError(); error = GetLastError();