Commit 07082792 authored by Philippe Teuwen's avatar Philippe Teuwen

malloc/free: some cleaning & checking malloc errors

parent 35b08dc5
......@@ -261,9 +261,15 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring)
if (sscanf(ndd.pcsc_device_name, "%lu", &index) != 1)
return NULL;
nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1));
if (!ncs) {
perror("malloc");
return NULL;
}
size_t szDeviceFound = acr122_pcsc_scan(context, ncs, index + 1);
if (szDeviceFound < index + 1)
if (szDeviceFound < index + 1) {
free(ncs);
return NULL;
}
strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring));
free(ncs);
connstring_decode_level = acr122_pcsc_connstring_decode(fullconnstring, &ndd);
......@@ -275,6 +281,10 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring)
char *pcFirmware;
nfc_device *pnd = nfc_device_new(context, fullconnstring);
pnd->driver_data = malloc(sizeof(struct acr122_pcsc_data));
if (!pnd->driver_data) {
perror("malloc");
goto error;
}
// Alloc and init chip's data
pn53x_data_new(pnd, &acr122_pcsc_io);
......
......@@ -351,8 +351,23 @@ acr122_usb_connstring_decode(const nfc_connstring connstring, struct acr122_usb_
{
int n = strlen(connstring) + 1;
char *driver_name = malloc(n);
if (!driver_name) {
perror("malloc");
return 0;
}
char *dirname = malloc(n);
if (!dirname) {
perror("malloc");
free(driver_name);
return 0;
}
char *filename = malloc(n);
if (!filename) {
perror("malloc");
free(driver_name);
free(dirname);
return 0;
}
driver_name[0] = '\0';
......@@ -462,6 +477,10 @@ acr122_usb_open(const nfc_context *context, const nfc_connstring connstring)
acr122_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name));
pnd->driver_data = malloc(sizeof(struct acr122_usb_data));
if (!pnd->driver_data) {
perror("malloc");
goto error;
}
*DRIVER_DATA(pnd) = data;
// Alloc and init chip's data
......
......@@ -473,6 +473,10 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz
pnd->driver = &acr122s_driver;
pnd->driver_data = malloc(sizeof(struct acr122s_data));
if (!pnd->driver_data) {
perror("malloc");
return -1;
}
DRIVER_DATA(pnd)->port = sp;
DRIVER_DATA(pnd)->seq = 0;
......@@ -574,6 +578,12 @@ acr122s_open(const nfc_context *context, const nfc_connstring connstring)
strcpy(pnd->name, ACR122S_DRIVER_NAME);
pnd->driver_data = malloc(sizeof(struct acr122s_data));
if (!pnd->driver_data) {
perror("malloc");
acr122s_close(pnd);
return NULL;
}
DRIVER_DATA(pnd)->port = sp;
DRIVER_DATA(pnd)->seq = 0;
......
......@@ -117,6 +117,10 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size
pnd->driver = &arygon_driver;
pnd->driver_data = malloc(sizeof(struct arygon_data));
if (!pnd->driver_data) {
perror("malloc");
return 0;
}
DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data
......@@ -262,6 +266,10 @@ arygon_open(const nfc_context *context, const nfc_connstring connstring)
snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port);
pnd->driver_data = malloc(sizeof(struct arygon_data));
if (!pnd->driver_data) {
perror("malloc");
return NULL;
}
DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data
......
......@@ -90,6 +90,10 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const
nfc_device *pnd = nfc_device_new(context, connstring);
pnd->driver = &pn532_uart_driver;
pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
if (!pnd->driver_data) {
perror("malloc");
return 0;
}
DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data
......@@ -239,6 +243,10 @@ pn532_uart_open(const nfc_context *context, const nfc_connstring connstring)
snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port);
pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
if (!pnd->driver_data) {
perror("malloc");
return NULL;
}
DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data
......
......@@ -234,8 +234,23 @@ pn53x_usb_connstring_decode(const nfc_connstring connstring, struct pn53x_usb_de
{
int n = strlen(connstring) + 1;
char *driver_name = malloc(n);
if (!driver_name) {
perror("malloc");
return 0;
}
char *dirname = malloc(n);
if (!dirname) {
perror("malloc");
free(driver_name);
return 0;
}
char *filename = malloc(n);
if (!filename) {
perror("malloc");
free(driver_name);
free(dirname);
return 0;
}
driver_name[0] = '\0';
......@@ -345,6 +360,10 @@ pn53x_usb_open(const nfc_context *context, const nfc_connstring connstring)
pn53x_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name));
pnd->driver_data = malloc(sizeof(struct pn53x_usb_data));
if (!pnd->driver_data) {
perror("malloc");
goto error;
}
*DRIVER_DATA(pnd) = data;
// Alloc and init chip's data
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment