GetUserName should include the terminating null character when
returning the size of the buffer returned. Correctly handle the ERROR_MORE_DATA case.
This commit is contained in:
parent
55a14edd17
commit
a630510baf
|
@ -4,6 +4,7 @@
|
||||||
* Copyright 1995 Sven Verdoolaege
|
* Copyright 1995 Sven Verdoolaege
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
#include <errno.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
@ -16,9 +17,13 @@
|
||||||
|
|
||||||
#include "debugtools.h"
|
#include "debugtools.h"
|
||||||
|
|
||||||
|
DEFAULT_DEBUG_CHANNEL(advapi);
|
||||||
|
|
||||||
/******************************************************************************
|
/******************************************************************************
|
||||||
* GetUserNameA [ADVAPI32.@]
|
* GetUserNameA [ADVAPI32.@]
|
||||||
|
*
|
||||||
|
* NOTE: lpSize returns the total length of the username, including the
|
||||||
|
* terminating null character.
|
||||||
*/
|
*/
|
||||||
BOOL WINAPI
|
BOOL WINAPI
|
||||||
GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
|
GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
|
||||||
|
@ -27,14 +32,24 @@ GetUserNameA( LPSTR lpszName, LPDWORD lpSize )
|
||||||
char *name;
|
char *name;
|
||||||
|
|
||||||
struct passwd *pwd = getpwuid( getuid() );
|
struct passwd *pwd = getpwuid( getuid() );
|
||||||
if (!pwd) return 0;
|
if (!pwd)
|
||||||
name = pwd->pw_name;
|
{
|
||||||
len = name ? strlen(name) : 0;
|
ERR("Username lookup failed: %s\n", strerror(errno));
|
||||||
if (!len || !lpSize || len > *lpSize) {
|
|
||||||
if (lpszName) *lpszName = 0;
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
*lpSize=len;
|
|
||||||
|
name = pwd->pw_name;
|
||||||
|
|
||||||
|
/* We need to include the null character when determining the size of the buffer. */
|
||||||
|
len = strlen(name) + 1;
|
||||||
|
if (len > *lpSize)
|
||||||
|
{
|
||||||
|
SetLastError(ERROR_MORE_DATA);
|
||||||
|
*lpSize = len;
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
*lpSize = len;
|
||||||
strcpy(lpszName, name);
|
strcpy(lpszName, name);
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue