diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 791fca4..e4f8542 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -429,6 +429,7 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz nfc_device *pnd = nfc_device_new(context, connstring); if (!pnd) { perror("malloc"); + uart_close(sp); return -1; } @@ -436,6 +437,7 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz pnd->driver_data = malloc(sizeof(struct acr122s_data)); if (!pnd->driver_data) { perror("malloc"); + uart_close(sp); return -1; } DRIVER_DATA(pnd)->port = sp; @@ -443,6 +445,7 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz #ifndef WIN32 if (pipe(DRIVER_DATA(pnd)->abort_fds) < 0) { + uart_close(DRIVER_DATA(pnd)->port); return 0; } #else @@ -459,9 +462,9 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz ret = -1; } + uart_close(DRIVER_DATA(pnd)->port); pn53x_data_free(pnd); nfc_device_free(pnd); - uart_close(sp); if (ret != 0) continue; @@ -550,7 +553,7 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) if (!pnd) { perror("malloc"); free(ndd.port); - acr122s_close(pnd); + uart_close(sp); return NULL; } pnd->driver = &acr122s_driver; @@ -560,7 +563,8 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) pnd->driver_data = malloc(sizeof(struct acr122s_data)); if (!pnd->driver_data) { perror("malloc"); - acr122s_close(pnd); + uart_close(sp); + nfc_device_free(pnd); return NULL; } @@ -569,7 +573,8 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) #ifndef WIN32 if (pipe(DRIVER_DATA(pnd)->abort_fds) < 0) { - acr122s_close(pnd); + uart_close(DRIVER_DATA(pnd)->port); + nfc_device_free(pnd); return NULL; } #else diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index dc0bb8f..35f4dfd 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -120,6 +120,7 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size nfc_device *pnd = nfc_device_new(context, connstring); if (!pnd) { perror("malloc"); + uart_close(sp); return 0; } @@ -127,6 +128,7 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size pnd->driver_data = malloc(sizeof(struct arygon_data)); if (!pnd->driver_data) { perror("malloc"); + uart_close(sp); return 0; } DRIVER_DATA(pnd)->port = sp; @@ -137,6 +139,7 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size #ifndef WIN32 // pipe-based abort mecanism if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) { + uart_close(DRIVER_DATA(pnd)->port); return 0; } #else @@ -144,9 +147,9 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size #endif int res = arygon_reset_tama(pnd); + uart_close(DRIVER_DATA(pnd)->port); pn53x_data_free(pnd); nfc_device_free(pnd); - uart_close(sp); if (res < 0) { continue; } @@ -237,6 +240,7 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring) if (!pnd) { perror("malloc"); free(ndd.port); + uart_close(sp); return NULL; } snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); @@ -245,6 +249,7 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring) pnd->driver_data = malloc(sizeof(struct arygon_data)); if (!pnd->driver_data) { perror("malloc"); + uart_close(sp); return NULL; } DRIVER_DATA(pnd)->port = sp; @@ -262,6 +267,7 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring) #ifndef WIN32 // pipe-based abort mecanism if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) { + uart_close(DRIVER_DATA(pnd)->port); return NULL; } #else diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 68b35dd..d82555d 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -90,12 +90,14 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const nfc_device *pnd = nfc_device_new(context, connstring); if (!pnd) { perror("malloc"); + uart_close(sp); return 0; } pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); if (!pnd->driver_data) { perror("malloc"); + uart_close(sp); return 0; } DRIVER_DATA(pnd)->port = sp; @@ -110,6 +112,7 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const #ifndef WIN32 // pipe-based abort mecanism if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) { + uart_close(DRIVER_DATA(pnd)->port); return 0; } #else @@ -118,9 +121,9 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const // Check communication using "Diagnose" command, with "Communication test" (0x00) int res = pn53x_check_communication(pnd); + uart_close(DRIVER_DATA(pnd)->port); pn53x_data_free(pnd); nfc_device_free(pnd); - uart_close(sp); if (res < 0) { continue; } @@ -209,6 +212,7 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) if (!pnd) { perror("malloc"); free(ndd.port); + uart_close(sp); return NULL; } snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); @@ -217,6 +221,7 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); if (!pnd->driver_data) { perror("malloc"); + uart_close(sp); return NULL; } DRIVER_DATA(pnd)->port = sp; @@ -235,6 +240,7 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) #ifndef WIN32 // pipe-based abort mecanism if (pipe(DRIVER_DATA(pnd)->iAbortFds) < 0) { + uart_close(DRIVER_DATA(pnd)->port); return NULL; } #else