From 09ef2e39277f78b9f6ac445614da1eb34939ddcb Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 19:57:36 +0100 Subject: [PATCH] nfc_device_new(): replace err() by return Not a good idea to call exit() from a library... --- libnfc/drivers/acr122_pcsc.c | 4 ++++ libnfc/drivers/acr122_usb.c | 4 ++++ libnfc/drivers/acr122s.c | 8 ++++++++ libnfc/drivers/arygon.c | 8 ++++++++ libnfc/drivers/pn532_uart.c | 8 ++++++++ libnfc/drivers/pn53x_usb.c | 4 ++++ libnfc/nfc-device.c | 2 +- 7 files changed, 37 insertions(+), 1 deletion(-) diff --git a/libnfc/drivers/acr122_pcsc.c b/libnfc/drivers/acr122_pcsc.c index 1bb9b8e..e24e425 100644 --- a/libnfc/drivers/acr122_pcsc.c +++ b/libnfc/drivers/acr122_pcsc.c @@ -280,6 +280,10 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring) char *pcFirmware; nfc_device *pnd = nfc_device_new(context, fullconnstring); + if (!pnd) { + perror("malloc"); + goto error; + } pnd->driver_data = malloc(sizeof(struct acr122_pcsc_data)); if (!pnd->driver_data) { perror("malloc"); diff --git a/libnfc/drivers/acr122_usb.c b/libnfc/drivers/acr122_usb.c index 91b47c0..c39cac6 100644 --- a/libnfc/drivers/acr122_usb.c +++ b/libnfc/drivers/acr122_usb.c @@ -475,6 +475,10 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring) data.model = acr122_usb_get_device_model(dev->descriptor.idVendor, dev->descriptor.idProduct); // Allocate memory for the device info and specification, fill it and return the info pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + goto error; + } acr122_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name)); pnd->driver_data = malloc(sizeof(struct acr122_usb_data)); diff --git a/libnfc/drivers/acr122s.c b/libnfc/drivers/acr122s.c index 3043268..0b19d32 100644 --- a/libnfc/drivers/acr122s.c +++ b/libnfc/drivers/acr122s.c @@ -470,6 +470,10 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz nfc_connstring connstring; snprintf(connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, ACR122S_DRIVER_NAME, acPort, ACR122S_DEFAULT_SPEED); nfc_device *pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return -1; + } pnd->driver = &acr122s_driver; pnd->driver_data = malloc(sizeof(struct acr122s_data)); @@ -574,6 +578,10 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring) uart_set_speed(sp, ndd.speed); pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return NULL; + } pnd->driver = &acr122s_driver; strcpy(pnd->name, ACR122S_DRIVER_NAME); diff --git a/libnfc/drivers/arygon.c b/libnfc/drivers/arygon.c index ee0f149..a788807 100644 --- a/libnfc/drivers/arygon.c +++ b/libnfc/drivers/arygon.c @@ -114,6 +114,10 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size nfc_connstring connstring; snprintf(connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, ARYGON_DRIVER_NAME, acPort, ARYGON_DEFAULT_SPEED); nfc_device *pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return 0; + } pnd->driver = &arygon_driver; pnd->driver_data = malloc(sizeof(struct arygon_data)); @@ -263,6 +267,10 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring) // We have a connection pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return NULL; + } snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct arygon_data)); diff --git a/libnfc/drivers/pn532_uart.c b/libnfc/drivers/pn532_uart.c index 48ffc4b..4cbb9a5 100644 --- a/libnfc/drivers/pn532_uart.c +++ b/libnfc/drivers/pn532_uart.c @@ -88,6 +88,10 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const nfc_connstring connstring; snprintf(connstring, sizeof(nfc_connstring), "%s:%s:%"PRIu32, PN532_UART_DRIVER_NAME, acPort, PN532_UART_DEFAULT_SPEED); nfc_device *pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return 0; + } pnd->driver = &pn532_uart_driver; pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); if (!pnd->driver_data) { @@ -240,6 +244,10 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring) // We have a connection pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + return NULL; + } snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port); pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); diff --git a/libnfc/drivers/pn53x_usb.c b/libnfc/drivers/pn53x_usb.c index 747c6ec..f440b5a 100644 --- a/libnfc/drivers/pn53x_usb.c +++ b/libnfc/drivers/pn53x_usb.c @@ -360,6 +360,10 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring) data.model = pn53x_usb_get_device_model(dev->descriptor.idVendor, dev->descriptor.idProduct); // Allocate memory for the device info and specification, fill it and return the info pnd = nfc_device_new(context, connstring); + if (!pnd) { + perror("malloc"); + goto error; + } pn53x_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name)); pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); diff --git a/libnfc/nfc-device.c b/libnfc/nfc-device.c index 76c29f1..4e5d883 100644 --- a/libnfc/nfc-device.c +++ b/libnfc/nfc-device.c @@ -38,7 +38,7 @@ nfc_device_new(const nfc_context *context, const nfc_connstring connstring) nfc_device *res = malloc(sizeof(*res)); if (!res) { - err(EXIT_FAILURE, "nfc_device_new: malloc"); + return NULL; } // Store associated context