diff --git a/libnfc/chips/pn53x.c b/libnfc/chips/pn53x.c index 988b89f..e10ca91 100644 --- a/libnfc/chips/pn53x.c +++ b/libnfc/chips/pn53x.c @@ -3122,11 +3122,13 @@ pn53x_current_target_is(const struct nfc_device *pnd, const nfc_target *pnt) return true; } -void +void * pn53x_data_new(struct nfc_device *pnd, const struct pn53x_io *io) { pnd->chip_data = malloc(sizeof(struct pn53x_data)); - + if (!pnd->chip_data) { + return NULL; + } // Keep I/O functions CHIP_DATA(pnd)->io = io; @@ -3165,6 +3167,8 @@ pn53x_data_new(struct nfc_device *pnd, const struct pn53x_io *io) CHIP_DATA(pnd)->supported_modulation_as_initiator = NULL; CHIP_DATA(pnd)->supported_modulation_as_target = NULL; + + return pnd->chip_data; } void diff --git a/libnfc/chips/pn53x.h b/libnfc/chips/pn53x.h index 15dae18..51d5dbf 100644 --- a/libnfc/chips/pn53x.h +++ b/libnfc/chips/pn53x.h @@ -394,7 +394,7 @@ int pn53x_get_supported_modulation(nfc_device *pnd, const nfc_mode mode, cons int pn53x_get_supported_baud_rate(nfc_device *pnd, const nfc_modulation_type nmt, const nfc_baud_rate **const supported_br); int pn53x_get_information_about(nfc_device *pnd, char **pbuf); -void pn53x_data_new(struct nfc_device *pnd, const struct pn53x_io *io); +void *pn53x_data_new(struct nfc_device *pnd, const struct pn53x_io *io); void pn53x_data_free(struct nfc_device *pnd); #endif // __NFC_CHIPS_PN53X_H__ diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index 578c21f..3a0f889 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -264,7 +264,10 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) } // Alloc and init chip's data - pn53x_data_new(pnd, &acr122_pcsc_io); + if (pn53x_data_new(pnd, &acr122_pcsc_io) == NULL) { + perror("malloc"); + goto error; + } SCARDCONTEXT *pscc; diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index b5bc387..95b7a49 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -452,7 +452,10 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) *DRIVER_DATA(pnd) = data; // Alloc and init chip's data - pn53x_data_new(pnd, &acr122_usb_io); + if (pn53x_data_new(pnd, &acr122_usb_io) == NULL) { + perror("malloc"); + goto error; + } memcpy(&(DRIVER_DATA(pnd)->tama_frame), acr122_usb_frame_template, sizeof(acr122_usb_frame_template)); memcpy(&(DRIVER_DATA(pnd)->apdu_frame), acr122_usb_frame_template, sizeof(acr122_usb_frame_template)); diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 8cc9c6f..55a57c3 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -454,7 +454,12 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz DRIVER_DATA(pnd)->abort_flag = false; #endif - pn53x_data_new(pnd, &acr122s_io); + if (pn53x_data_new(pnd, &acr122s_io) == NULL) { + perror("malloc"); + uart_close(DRIVER_DATA(pnd)->port); + nfc_device_free(pnd); + return 0; + } CHIP_DATA(pnd)->type = PN532; CHIP_DATA(pnd)->power_mode = NORMAL; @@ -583,7 +588,12 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) DRIVER_DATA(pnd)->abort_flag = false; #endif - pn53x_data_new(pnd, &acr122s_io); + if (pn53x_data_new(pnd, &acr122s_io) == NULL) { + perror("malloc"); + uart_close(DRIVER_DATA(pnd)->port); + nfc_device_free(pnd); + return NULL; + } CHIP_DATA(pnd)->type = PN532; #if 1 diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index 1201529..59263e9 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -135,7 +135,12 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data - pn53x_data_new(pnd, &arygon_tama_io); + if (pn53x_data_new(pnd, &arygon_tama_io) == NULL) { + perror("malloc"); + uart_close(DRIVER_DATA(pnd)->port); + nfc_device_free(pnd); + return 0; + } #ifndef WIN32 // pipe-based abort mecanism @@ -259,7 +264,12 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring) DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data - pn53x_data_new(pnd, &arygon_tama_io); + if (pn53x_data_new(pnd, &arygon_tama_io) == NULL) { + perror("malloc"); + uart_close(DRIVER_DATA(pnd)->port); + nfc_device_free(pnd); + return NULL; + } // The PN53x chip opened to ARYGON MCU doesn't seems to be in LowVBat mode CHIP_DATA(pnd)->power_mode = NORMAL; diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index afa07d0..0a700b4 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -104,7 +104,12 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data - pn53x_data_new(pnd, &pn532_uart_io); + if (pn53x_data_new(pnd, &pn532_uart_io) == NULL) { + perror("malloc"); + uart_close(DRIVER_DATA(pnd)->port); + nfc_device_free(pnd); + return 0; + } // SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532 CHIP_DATA(pnd)->type = PN532; // This device starts in LowVBat power mode @@ -231,7 +236,12 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) DRIVER_DATA(pnd)->port = sp; // Alloc and init chip's data - pn53x_data_new(pnd, &pn532_uart_io); + if (pn53x_data_new(pnd, &pn532_uart_io) == NULL) { + perror("malloc"); + uart_close(DRIVER_DATA(pnd)->port); + nfc_device_free(pnd); + return NULL; + } // SAMConfiguration command if needed to wakeup the chip and pn53x_SAMConfiguration check if the chip is a PN532 CHIP_DATA(pnd)->type = PN532; // This device starts in LowVBat mode diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index ed08c76..26edd82 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -337,7 +337,10 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) *DRIVER_DATA(pnd) = data; // Alloc and init chip's data - pn53x_data_new(pnd, &pn53x_usb_io); + if (pn53x_data_new(pnd, &pn53x_usb_io) == NULL) { + perror("malloc"); + goto error; + } switch (DRIVER_DATA(pnd)->model) { // empirical tuning