Commit c0b7345e authored by Andrew Nguyen's avatar Andrew Nguyen Committed by Alexandre Julliard

advapi32: Fix last error value of GetUserNameA/W and output buffer handling in GetUserNameA.

parent c8ea11c0
...@@ -55,24 +55,20 @@ GetUserNameA( LPSTR lpszName, LPDWORD lpSize ) ...@@ -55,24 +55,20 @@ GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
{ {
WCHAR *buffer; WCHAR *buffer;
BOOL ret; BOOL ret;
DWORD sizeW = *lpSize * 2; DWORD sizeW = *lpSize;
if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeW * sizeof(WCHAR) ))) if (!(buffer = HeapAlloc( GetProcessHeap(), 0, sizeW * sizeof(WCHAR) )))
{ {
SetLastError( ERROR_NOT_ENOUGH_MEMORY ); SetLastError( ERROR_NOT_ENOUGH_MEMORY );
return FALSE; return FALSE;
} }
ret = GetUserNameW( buffer, &sizeW ); ret = GetUserNameW( buffer, &sizeW );
if (ret) if (ret)
{ WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpszName, *lpSize, NULL, NULL );
if (!(*lpSize = WideCharToMultiByte( CP_ACP, 0, buffer, -1, lpszName, *lpSize, NULL, NULL ))) else
{ *lpSize = sizeW;
*lpSize = WideCharToMultiByte( CP_ACP, 0, buffer, -1, NULL, 0, NULL, NULL );
SetLastError( ERROR_MORE_DATA );
ret = FALSE;
}
}
else *lpSize = sizeW * 2;
HeapFree( GetProcessHeap(), 0, buffer ); HeapFree( GetProcessHeap(), 0, buffer );
return ret; return ret;
} }
...@@ -91,7 +87,7 @@ GetUserNameW( LPWSTR lpszName, LPDWORD lpSize ) ...@@ -91,7 +87,7 @@ GetUserNameW( LPWSTR lpszName, LPDWORD lpSize )
if (len > *lpSize) if (len > *lpSize)
{ {
SetLastError(ERROR_MORE_DATA); SetLastError( ERROR_INSUFFICIENT_BUFFER );
*lpSize = len; *lpSize = len;
return FALSE; return FALSE;
} }
......
...@@ -3741,7 +3741,6 @@ static void test_GetUserNameA(void) ...@@ -3741,7 +3741,6 @@ static void test_GetUserNameA(void)
ret = GetUserNameA(NULL, &required_len); ret = GetUserNameA(NULL, &required_len);
ok(ret == FALSE, "GetUserNameA returned %d\n", ret); ok(ret == FALSE, "GetUserNameA returned %d\n", ret);
ok(required_len != 0, "Outputted buffer length was %u\n", required_len); ok(required_len != 0, "Outputted buffer length was %u\n", required_len);
todo_wine
ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError()); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
SetLastError(0xdeadbeef); SetLastError(0xdeadbeef);
...@@ -3749,7 +3748,6 @@ static void test_GetUserNameA(void) ...@@ -3749,7 +3748,6 @@ static void test_GetUserNameA(void)
ret = GetUserNameA(NULL, &required_len); ret = GetUserNameA(NULL, &required_len);
ok(ret == FALSE, "GetUserNameA returned %d\n", ret); ok(ret == FALSE, "GetUserNameA returned %d\n", ret);
ok(required_len != 0 && required_len != 1, "Outputted buffer length was %u\n", required_len); ok(required_len != 0 && required_len != 1, "Outputted buffer length was %u\n", required_len);
todo_wine
ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError()); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
/* Tests crashes on Windows. */ /* Tests crashes on Windows. */
...@@ -3774,7 +3772,6 @@ static void test_GetUserNameA(void) ...@@ -3774,7 +3772,6 @@ static void test_GetUserNameA(void)
ok(ret == FALSE, "GetUserNameA returned %d\n", ret); ok(ret == FALSE, "GetUserNameA returned %d\n", ret);
ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was altered\n"); ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was altered\n");
ok(required_len != 0, "Outputted buffer length was %u\n", required_len); ok(required_len != 0, "Outputted buffer length was %u\n", required_len);
todo_wine
ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError()); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
SetLastError(0xdeadbeef); SetLastError(0xdeadbeef);
...@@ -3783,7 +3780,6 @@ static void test_GetUserNameA(void) ...@@ -3783,7 +3780,6 @@ static void test_GetUserNameA(void)
ret = GetUserNameA(buffer, &buffer_len); ret = GetUserNameA(buffer, &buffer_len);
ok(ret == TRUE, "GetUserNameA returned %d, last error %u\n", ret, GetLastError()); ok(ret == TRUE, "GetUserNameA returned %d, last error %u\n", ret, GetLastError());
ok(memcmp(buffer, filler, sizeof(filler)) != 0, "Output buffer was untouched\n"); ok(memcmp(buffer, filler, sizeof(filler)) != 0, "Output buffer was untouched\n");
todo_wine
ok(buffer_len == required_len || ok(buffer_len == required_len ||
broken(buffer_len == required_len / sizeof(WCHAR)), /* XP+ */ broken(buffer_len == required_len / sizeof(WCHAR)), /* XP+ */
"Outputted buffer length was %u\n", buffer_len); "Outputted buffer length was %u\n", buffer_len);
...@@ -3795,11 +3791,9 @@ static void test_GetUserNameA(void) ...@@ -3795,11 +3791,9 @@ static void test_GetUserNameA(void)
buffer_len--; buffer_len--;
ret = GetUserNameA(buffer, &buffer_len); ret = GetUserNameA(buffer, &buffer_len);
ok(ret == FALSE, "GetUserNameA returned %d\n", ret); ok(ret == FALSE, "GetUserNameA returned %d\n", ret);
todo_wine {
ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was untouched\n"); ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was untouched\n");
ok(buffer_len == required_len, "Outputted buffer length was %u\n", buffer_len); ok(buffer_len == required_len, "Outputted buffer length was %u\n", buffer_len);
ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError()); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
}
} }
static void test_GetUserNameW(void) static void test_GetUserNameW(void)
...@@ -3820,7 +3814,6 @@ static void test_GetUserNameW(void) ...@@ -3820,7 +3814,6 @@ static void test_GetUserNameW(void)
ret = GetUserNameW(NULL, &required_len); ret = GetUserNameW(NULL, &required_len);
ok(ret == FALSE, "GetUserNameW returned %d\n", ret); ok(ret == FALSE, "GetUserNameW returned %d\n", ret);
ok(required_len != 0, "Outputted buffer length was %u\n", required_len); ok(required_len != 0, "Outputted buffer length was %u\n", required_len);
todo_wine
ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError()); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
SetLastError(0xdeadbeef); SetLastError(0xdeadbeef);
...@@ -3828,7 +3821,6 @@ static void test_GetUserNameW(void) ...@@ -3828,7 +3821,6 @@ static void test_GetUserNameW(void)
ret = GetUserNameW(NULL, &required_len); ret = GetUserNameW(NULL, &required_len);
ok(ret == FALSE, "GetUserNameW returned %d\n", ret); ok(ret == FALSE, "GetUserNameW returned %d\n", ret);
ok(required_len != 0 && required_len != 1, "Outputted buffer length was %u\n", required_len); ok(required_len != 0 && required_len != 1, "Outputted buffer length was %u\n", required_len);
todo_wine
ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError()); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
/* Tests crash on Windows. */ /* Tests crash on Windows. */
...@@ -3851,7 +3843,6 @@ static void test_GetUserNameW(void) ...@@ -3851,7 +3843,6 @@ static void test_GetUserNameW(void)
ok(ret == FALSE, "GetUserNameW returned %d\n", ret); ok(ret == FALSE, "GetUserNameW returned %d\n", ret);
ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was altered\n"); ok(!memcmp(buffer, filler, sizeof(filler)), "Output buffer was altered\n");
ok(required_len != 0, "Outputted buffer length was %u\n", required_len); ok(required_len != 0, "Outputted buffer length was %u\n", required_len);
todo_wine
ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError()); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
SetLastError(0xdeadbeef); SetLastError(0xdeadbeef);
...@@ -3872,7 +3863,6 @@ static void test_GetUserNameW(void) ...@@ -3872,7 +3863,6 @@ static void test_GetUserNameW(void)
broken(memcmp(buffer, filler, sizeof(filler)) != 0), /* XP+ */ broken(memcmp(buffer, filler, sizeof(filler)) != 0), /* XP+ */
"Output buffer was altered\n"); "Output buffer was altered\n");
ok(buffer_len == required_len, "Outputted buffer length was %u\n", buffer_len); ok(buffer_len == required_len, "Outputted buffer length was %u\n", buffer_len);
todo_wine
ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError()); ok(GetLastError() == ERROR_INSUFFICIENT_BUFFER, "Last error was %u\n", GetLastError());
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment