From 28821d7a29e4babcdc508f254c1ebc69e5916baf Mon Sep 17 00:00:00 2001 From: James Muehlner Date: Wed, 2 Aug 2023 21:48:00 +0000 Subject: [PATCH] GUACAMOLE-1841: Always close event handles. --- src/guacd/connection.c | 8 +++++++- src/libguac/handle-helpers.c | 25 ++++++++++++++++++++----- src/libguac/wait-handle.c | 19 ++++++++++++++----- 3 files changed, 41 insertions(+), 11 deletions(-) diff --git a/src/guacd/connection.c b/src/guacd/connection.c index f0ecf9772..1e8baacc1 100644 --- a/src/guacd/connection.c +++ b/src/guacd/connection.c @@ -373,8 +373,14 @@ static int guacd_add_user(guacd_proc* proc, guac_parser* parser, guac_socket* so return 1; } - /* Wait for the other end of the pipe to connect before attempting IO */ + /* Create an event to monitor for pipe connection */ HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL); + if (event == NULL) { + guacd_log(GUAC_LOG_ERROR, "Event creation failed."); + return 1; + } + + /* Wait for the other end of the pipe to connect before attempting IO */ OVERLAPPED overlapped = { 0 }; overlapped.hEvent = event; ConnectNamedPipe(pipe_handle, &overlapped); diff --git a/src/libguac/handle-helpers.c b/src/libguac/handle-helpers.c index 6c878336f..3c48dc795 100755 --- a/src/libguac/handle-helpers.c +++ b/src/libguac/handle-helpers.c @@ -39,6 +39,8 @@ int guac_read_from_handle( */ OVERLAPPED overlapped = { 0 }; overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + if (overlapped.hEvent == NULL) + return GetLastError(); /* Attempt to start the async read operation */ if (!ReadFile(handle, buffer, count, NULL, &overlapped)) { @@ -50,6 +52,7 @@ int guac_read_from_handle( * return it as the error code immediately. */ if (error != ERROR_IO_PENDING) { + CloseHandle(overlapped.hEvent); return error; } @@ -59,10 +62,14 @@ int guac_read_from_handle( * Wait on the result of the read. If any error occurs when waiting, * return the error. */ - if (!GetOverlappedResult(handle, &overlapped, num_bytes_read, TRUE)) - return GetLastError(); + if (!GetOverlappedResult(handle, &overlapped, num_bytes_read, TRUE)) { + DWORD error = GetLastError(); + CloseHandle(overlapped.hEvent); + return error; + } /* No errors occured, so the read was successful */ + CloseHandle(overlapped.hEvent); return 0; } @@ -75,6 +82,8 @@ int guac_write_to_handle( */ OVERLAPPED overlapped = { 0 }; overlapped.hEvent = CreateEvent(NULL, FALSE, FALSE, NULL); + if (overlapped.hEvent == NULL) + return GetLastError(); /* Attempt to start the async write operation */ if (!WriteFile(handle, buffer, count, NULL, &overlapped)) { @@ -85,8 +94,10 @@ int guac_write_to_handle( * If an error other than the expected ERROR_IO_PENDING happens, * return it as the error code immediately. */ - if (error != ERROR_IO_PENDING) + if (error != ERROR_IO_PENDING) { + CloseHandle(overlapped.hEvent); return error; + } } @@ -94,10 +105,14 @@ int guac_write_to_handle( * Wait on the result of the write. If any error occurs when waiting, * return the error. */ - if (!GetOverlappedResult(handle, &overlapped, num_bytes_written, TRUE)) - return GetLastError(); + if (!GetOverlappedResult(handle, &overlapped, num_bytes_written, TRUE)) { + DWORD error = GetLastError(); + CloseHandle(overlapped.hEvent); + return error; + } /* No errors occured, so the write was successful */ + CloseHandle(overlapped.hEvent); return 0; } \ No newline at end of file diff --git a/src/libguac/wait-handle.c b/src/libguac/wait-handle.c index 0e3133868..bfba875b2 100644 --- a/src/libguac/wait-handle.c +++ b/src/libguac/wait-handle.c @@ -28,10 +28,12 @@ int guac_wait_for_handle(HANDLE handle, int usec_timeout) { + /* Create an event to be used to signal comm events */ HANDLE event = CreateEvent(NULL, FALSE, FALSE, NULL); + if (event == NULL) + return GetLastError(); + OVERLAPPED overlapped = { 0 }; - - /* Set the event to be used to signal comm events */ overlapped.hEvent = event; /* Request to wait for new data to be available */ @@ -41,8 +43,10 @@ int guac_wait_for_handle(HANDLE handle, int usec_timeout) { DWORD error = GetLastError(); /* ERROR_IO_PENDING is expected in overlapped mode */ - if (error != ERROR_IO_PENDING) + if (error != ERROR_IO_PENDING) { + CloseHandle(event); return error; + } } @@ -51,17 +55,22 @@ int guac_wait_for_handle(HANDLE handle, int usec_timeout) { DWORD result = WaitForSingleObject(event, millis); /* The wait attempt failed */ - if (result == WAIT_FAILED) + if (result == WAIT_FAILED) { + CloseHandle(event); return GetLastError(); + } /* The event was signalled, which should indicate data is ready */ - else if (result == WAIT_OBJECT_0) + else if (result == WAIT_OBJECT_0) { + CloseHandle(event); return 0; + } /* * If the event didn't trigger and the wait didn't fail, data just isn't * ready yet. */ + CloseHandle(event); return -1; }