malloc/free: some more cleaning & checking malloc errors

This commit is contained in:
Philippe Teuwen
2013-03-02 02:50:33 +01:00
parent 0708279215
commit a6c405a5d5
4 changed files with 46 additions and 0 deletions

View File

@@ -79,16 +79,19 @@ uart_open(const char *pcPortName)
sp->fd = open(pcPortName, O_RDWR | O_NOCTTY | O_NONBLOCK);
if (sp->fd == -1) {
uart_close_ext(sp, false);
free(sp);
return INVALID_SERIAL_PORT;
}
if (tcgetattr(sp->fd, &sp->termios_backup) == -1) {
uart_close_ext(sp, false);
free(sp);
return INVALID_SERIAL_PORT;
}
// Make sure the port is not claimed already
if (sp->termios_backup.c_iflag & CCLAIMED) {
uart_close_ext(sp, false);
free(sp);
return CLAIMED_SERIAL_PORT;
}
// Copy the old terminal info struct
@@ -104,6 +107,7 @@ uart_open(const char *pcPortName)
if (tcsetattr(sp->fd, TCSANOW, &sp->termios_new) == -1) {
uart_close_ext(sp, true);
free(sp);
return INVALID_SERIAL_PORT;
}
return sp;

View File

@@ -45,6 +45,9 @@ uart_open(const char *pcPortName)
char acPortName[255];
struct serial_port_windows *sp = malloc(sizeof(struct serial_port_windows));
if (sp == 0)
return INVALID_SERIAL_PORT;
// Copy the input "com?" to "\\.\COM?" format
sprintf(acPortName, "\\\\.\\%s", pcPortName);
_strupr(acPortName);
@@ -53,6 +56,7 @@ uart_open(const char *pcPortName)
sp->hPort = CreateFileA(acPortName, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL);
if (sp->hPort == INVALID_HANDLE_VALUE) {
uart_close(sp);
free(sp);
return INVALID_SERIAL_PORT;
}
// Prepare the device control
@@ -60,11 +64,13 @@ uart_open(const char *pcPortName)
sp->dcb.DCBlength = sizeof(DCB);
if (!BuildCommDCBA("baud=9600 data=8 parity=N stop=1", &sp->dcb)) {
uart_close(sp);
free(sp);
return INVALID_SERIAL_PORT;
}
// Update the active serial port
if (!SetCommState(sp->hPort, &sp->dcb)) {
uart_close(sp);
free(sp);
return INVALID_SERIAL_PORT;
}
@@ -76,6 +82,7 @@ uart_open(const char *pcPortName)
if (!SetCommTimeouts(sp->hPort, &sp->ct)) {
uart_close(sp);
free(sp);
return INVALID_SERIAL_PORT;
}