Skip to content

Commit

Permalink
Ensure UART file descriptors are valid before using them in uart_clos…
Browse files Browse the repository at this point in the history
…e ().

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'.
  • Loading branch information
smortex committed Jan 15, 2010
1 parent 3f8068b commit e92de4d
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/lib/buses/uart.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down

0 comments on commit e92de4d

Please sign in to comment.