Fixed length checks for GetComputerNameA

This commit is contained in:
Armin Novak 2019-10-29 11:29:47 +01:00
parent 66ad508b45
commit 68f519ed6c

View File

@ -391,7 +391,7 @@ BOOL GetVersionExW(LPOSVERSIONINFOW lpVersionInformation)
BOOL GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize) BOOL GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
{ {
char* dot; char* dot;
int length; size_t length;
char hostname[256]; char hostname[256];
if (!lpnSize) if (!lpnSize)
@ -403,22 +403,22 @@ BOOL GetComputerNameA(LPSTR lpBuffer, LPDWORD lpnSize)
if (gethostname(hostname, sizeof(hostname)) == -1) if (gethostname(hostname, sizeof(hostname)) == -1)
return FALSE; return FALSE;
length = (int) strlen(hostname); length = strnlen(hostname, sizeof(hostname));
dot = strchr(hostname, '.'); dot = strchr(hostname, '.');
if (dot) if (dot)
length = (int)(dot - hostname); length = (dot - hostname);
if ((*lpnSize <= (DWORD) length) || !lpBuffer) if ((*lpnSize <= (DWORD) length) || !lpBuffer)
{ {
SetLastError(ERROR_BUFFER_OVERFLOW); SetLastError(ERROR_BUFFER_OVERFLOW);
*lpnSize = length + 1; *lpnSize = (DWORD)(length + 1);
return FALSE; return FALSE;
} }
CopyMemory(lpBuffer, hostname, length); CopyMemory(lpBuffer, hostname, length);
lpBuffer[length] = '\0'; lpBuffer[length] = '\0';
*lpnSize = length; *lpnSize = (DWORD)length;
return TRUE; return TRUE;
} }