From e92de4d49ed05425ddd20dacba79262b6690e910 Mon Sep 17 00:00:00 2001 From: Romain Tartiere Date: Fri, 15 Jan 2010 10:38:50 +0000 Subject: [PATCH] Ensure UART file descriptors are valid before using them in uart_close (). The uart_open() function may call uart_close() with a 'serial_port' structure with an invalid member 'fd' if the port was not opened successfully. The call to uart_close() was kept for consistency with the rest of the function because uart_close() is also in charge of freeing 'serial_port'. --- src/lib/buses/uart.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/lib/buses/uart.c b/src/lib/buses/uart.c index b664c59..16f0466 100644 --- a/src/lib/buses/uart.c +++ b/src/lib/buses/uart.c @@ -170,8 +170,10 @@ uint32_t uart_get_speed(const serial_port sp) void uart_close(const serial_port sp) { - tcsetattr(((serial_port_unix*)sp)->fd,TCSANOW,&((serial_port_unix*)sp)->tiOld); - close(((serial_port_unix*)sp)->fd); + if (((serial_port_unix*)sp)->fd >= 0) { + tcsetattr(((serial_port_unix*)sp)->fd,TCSANOW,&((serial_port_unix*)sp)->tiOld); + close(((serial_port_unix*)sp)->fd); + } free(sp); } @@ -333,7 +335,9 @@ serial_port uart_open(const char* pcPortName) void uart_close(const serial_port sp) { - CloseHandle(((serial_port_windows*)sp)->hPort); + if (((serial_port_windows*)sp)->hPort != INVALID_HANDLE_VALUE) { + CloseHandle(((serial_port_windows*)sp)->hPort); + } free(sp); }