From a630510baf8ca56d7937a847ab6e51fe82a505ee Mon Sep 17 00:00:00 2001 From: James Juran <jamesjuran@alumni.psu.edu> Date: Tue, 20 Nov 2001 20:26:54 +0000 Subject: [PATCH] GetUserName should include the terminating null character when returning the size of the buffer returned. Correctly handle the ERROR_MORE_DATA case. --- dlls/advapi32/advapi.c | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/dlls/advapi32/advapi.c b/dlls/advapi32/advapi.c index 26f9a1ce9a8..7f9e99dd3ca 100644 --- a/dlls/advapi32/advapi.c +++ b/dlls/advapi32/advapi.c @@ -4,6 +4,7 @@ * Copyright 1995 Sven Verdoolaege */ +#include <errno.h> #include <stdio.h> #include <unistd.h> #include <string.h> @@ -16,9 +17,13 @@ #include "debugtools.h" +DEFAULT_DEBUG_CHANNEL(advapi); /****************************************************************************** * GetUserNameA [ADVAPI32.@] + * + * NOTE: lpSize returns the total length of the username, including the + * terminating null character. */ BOOL WINAPI GetUserNameA( LPSTR lpszName, LPDWORD lpSize ) @@ -27,14 +32,24 @@ GetUserNameA( LPSTR lpszName, LPDWORD lpSize ) char *name; struct passwd *pwd = getpwuid( getuid() ); - if (!pwd) return 0; + if (!pwd) + { + ERR("Username lookup failed: %s\n", strerror(errno)); + return 0; + } + name = pwd->pw_name; - len = name ? strlen(name) : 0; - if (!len || !lpSize || len > *lpSize) { - if (lpszName) *lpszName = 0; + + /* 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; + + *lpSize = len; strcpy(lpszName, name); return 1; } -- GitLab