Skip to content

Commit

Permalink
[MSAFD] WSPSendTo: Fix leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
TAN-Gaming committed Jan 17, 2024
1 parent b49b992 commit 1812a7d
Showing 1 changed file with 11 additions and 15 deletions.
26 changes: 11 additions & 15 deletions dll/win32/msafd/misc/sndrcv.c
Original file line number Diff line number Diff line change
Expand Up @@ -789,8 +789,6 @@ WSPSendTo(SOCKET Handle,
PIO_APC_ROUTINE APCFunction;
HANDLE Event = NULL;
PTRANSPORT_ADDRESS RemoteAddress;
PSOCKADDR BindAddress = NULL;
INT BindAddressLength;
HANDLE SockEvent;
PSOCKET_INFORMATION Socket;

Expand Down Expand Up @@ -828,6 +826,10 @@ WSPSendTo(SOCKET Handle,
/* Bind us First */
if (Socket->SharedData->State == SocketOpen)
{
INT BindAddressLength;
PSOCKADDR BindAddress;
INT BindError;

/* Get the Wildcard Address */
BindAddressLength = Socket->HelperData->MaxWSAddressLength;
BindAddress = HeapAlloc(GlobalHeap, 0, BindAddressLength);
Expand All @@ -841,17 +843,15 @@ WSPSendTo(SOCKET Handle,
BindAddress,
&BindAddressLength);
/* Bind it */
if (WSPBind(Handle, BindAddress, BindAddressLength, lpErrno) == SOCKET_ERROR)
BindError = WSPBind(Handle, BindAddress, BindAddressLength, lpErrno);
HeapFree(GlobalHeap, 0, BindAddress);
if (BindError == SOCKET_ERROR)
return SOCKET_ERROR;
}

RemoteAddress = HeapAlloc(GlobalHeap, 0, 0x6 + SocketAddressLength);
if (!RemoteAddress)
{
if (BindAddress != NULL)
{
HeapFree(GlobalHeap, 0, BindAddress);
}
return MsafdReturnWithErrno(STATUS_INSUFFICIENT_RESOURCES, lpErrno, 0, NULL);
}

Expand All @@ -862,10 +862,6 @@ WSPSendTo(SOCKET Handle,
if (!NT_SUCCESS(Status))
{
HeapFree(GlobalHeap, 0, RemoteAddress);
if (BindAddress != NULL)
{
HeapFree(GlobalHeap, 0, BindAddress);
}
return SOCKET_ERROR;
}

Expand Down Expand Up @@ -896,6 +892,8 @@ WSPSendTo(SOCKET Handle,
if ((Socket->SharedData->CreateFlags & SO_SYNCHRONOUS_NONALERT) != 0)
{
TRACE("Opened without flag WSA_FLAG_OVERLAPPED. Do nothing.\n");
NtClose(SockEvent);
HeapFree(GlobalHeap, 0, RemoteAddress);
return MsafdReturnWithErrno(STATUS_SUCCESS, lpErrno, 0, lpNumberOfBytesSent);
}
if (lpCompletionRoutine == NULL)
Expand All @@ -913,6 +911,8 @@ WSPSendTo(SOCKET Handle,
if (!APCContext)
{
ERR("Not enough memory for APC Context\n");
NtClose(SockEvent);
HeapFree(GlobalHeap, 0, RemoteAddress);
return MsafdReturnWithErrno(STATUS_INSUFFICIENT_RESOURCES, lpErrno, 0, lpNumberOfBytesSent);
}
APCContext->lpCompletionRoutine = lpCompletionRoutine;
Expand Down Expand Up @@ -950,10 +950,6 @@ WSPSendTo(SOCKET Handle,

NtClose(SockEvent);
HeapFree(GlobalHeap, 0, RemoteAddress);
if (BindAddress != NULL)
{
HeapFree(GlobalHeap, 0, BindAddress);
}

if (Status == STATUS_PENDING)
{
Expand Down

0 comments on commit 1812a7d

Please sign in to comment.