[file,namedpipe] clean up NamedPipeClientCreateFileA

simplify and improve error handling
This commit is contained in:
akallabeth 2024-10-31 08:31:48 +01:00
parent a58a390726
commit d06a6d78cb
No known key found for this signature in database
GPG Key ID: A49454A3FC909FD5

View File

@ -122,11 +122,8 @@ static HANDLE NamedPipeClientCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAcces
DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes, DWORD dwCreationDisposition, DWORD dwFlagsAndAttributes,
HANDLE hTemplateFile) HANDLE hTemplateFile)
{ {
char* name = NULL;
int status = 0; int status = 0;
HANDLE hNamedPipe = NULL;
struct sockaddr_un s = { 0 }; struct sockaddr_un s = { 0 };
WINPR_NAMED_PIPE* pNamedPipe = NULL;
if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED) if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED)
{ {
@ -141,13 +138,7 @@ static HANDLE NamedPipeClientCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAcces
if (!IsNamedPipeFileNameA(lpFileName)) if (!IsNamedPipeFileNameA(lpFileName))
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
name = GetNamedPipeNameWithoutPrefixA(lpFileName); WINPR_NAMED_PIPE* pNamedPipe = (WINPR_NAMED_PIPE*)calloc(1, sizeof(WINPR_NAMED_PIPE));
if (!name)
return INVALID_HANDLE_VALUE;
free(name);
pNamedPipe = (WINPR_NAMED_PIPE*)calloc(1, sizeof(WINPR_NAMED_PIPE));
if (!pNamedPipe) if (!pNamedPipe)
{ {
@ -155,15 +146,14 @@ static HANDLE NamedPipeClientCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAcces
return INVALID_HANDLE_VALUE; return INVALID_HANDLE_VALUE;
} }
hNamedPipe = (HANDLE)pNamedPipe; HANDLE hNamedPipe = (HANDLE)pNamedPipe;
WINPR_HANDLE_SET_TYPE_AND_MODE(pNamedPipe, HANDLE_TYPE_NAMED_PIPE, WINPR_FD_READ); WINPR_HANDLE_SET_TYPE_AND_MODE(pNamedPipe, HANDLE_TYPE_NAMED_PIPE, WINPR_FD_READ);
pNamedPipe->name = _strdup(lpFileName); pNamedPipe->name = _strdup(lpFileName);
if (!pNamedPipe->name) if (!pNamedPipe->name)
{ {
SetLastError(ERROR_NOT_ENOUGH_MEMORY); SetLastError(ERROR_NOT_ENOUGH_MEMORY);
free(pNamedPipe); goto fail;
return INVALID_HANDLE_VALUE;
} }
pNamedPipe->dwOpenMode = 0; pNamedPipe->dwOpenMode = 0;
@ -176,23 +166,17 @@ static HANDLE NamedPipeClientCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAcces
pNamedPipe->lpFileName = GetNamedPipeNameWithoutPrefixA(lpFileName); pNamedPipe->lpFileName = GetNamedPipeNameWithoutPrefixA(lpFileName);
if (!pNamedPipe->lpFileName) if (!pNamedPipe->lpFileName)
{ goto fail;
free((void*)pNamedPipe->name);
free(pNamedPipe);
return INVALID_HANDLE_VALUE;
}
pNamedPipe->lpFilePath = GetNamedPipeUnixDomainSocketFilePathA(lpFileName); pNamedPipe->lpFilePath = GetNamedPipeUnixDomainSocketFilePathA(lpFileName);
if (!pNamedPipe->lpFilePath) if (!pNamedPipe->lpFilePath)
{ goto fail;
free((void*)pNamedPipe->lpFileName);
free((void*)pNamedPipe->name);
free(pNamedPipe);
return INVALID_HANDLE_VALUE;
}
pNamedPipe->clientfd = socket(PF_LOCAL, SOCK_STREAM, 0); pNamedPipe->clientfd = socket(PF_LOCAL, SOCK_STREAM, 0);
if (pNamedPipe->clientfd < 0)
goto fail;
pNamedPipe->serverfd = -1; pNamedPipe->serverfd = -1;
pNamedPipe->ServerMode = FALSE; pNamedPipe->ServerMode = FALSE;
s.sun_family = AF_UNIX; s.sun_family = AF_UNIX;
@ -201,14 +185,7 @@ static HANDLE NamedPipeClientCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAcces
pNamedPipe->common.ops = &ops; pNamedPipe->common.ops = &ops;
if (status != 0) if (status != 0)
{ goto fail;
close(pNamedPipe->clientfd);
free(pNamedPipe->name);
free(pNamedPipe->lpFileName);
free(pNamedPipe->lpFilePath);
free(pNamedPipe);
return INVALID_HANDLE_VALUE;
}
if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED) if (dwFlagsAndAttributes & FILE_FLAG_OVERLAPPED)
{ {
@ -222,6 +199,18 @@ static HANDLE NamedPipeClientCreateFileA(LPCSTR lpFileName, DWORD dwDesiredAcces
} }
return hNamedPipe; return hNamedPipe;
fail:
if (pNamedPipe)
{
if (pNamedPipe->clientfd >= 0)
close(pNamedPipe->clientfd);
free(pNamedPipe->name);
free(pNamedPipe->lpFileName);
free(pNamedPipe->lpFilePath);
free(pNamedPipe);
}
return INVALID_HANDLE_VALUE;
} }
const HANDLE_CREATOR* GetNamedPipeClientHandleCreator(void) const HANDLE_CREATOR* GetNamedPipeClientHandleCreator(void)