malloc/free: some cleaning & checking malloc errors

This commit is contained in:
Philippe Teuwen 2013-03-02 02:25:13 +01:00
parent 35b08dc5cc
commit 0708279215
6 changed files with 76 additions and 2 deletions

View file

@ -261,9 +261,15 @@ acr122_pcsc_open(const nfc_context *context, const nfc_connstring connstring)
if (sscanf(ndd.pcsc_device_name, "%lu", &index) != 1) if (sscanf(ndd.pcsc_device_name, "%lu", &index) != 1)
return NULL; return NULL;
nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1)); nfc_connstring *ncs = malloc(sizeof(nfc_connstring) * (index + 1));
size_t szDeviceFound = acr122_pcsc_scan(context, ncs, index + 1); if (!ncs) {
if (szDeviceFound < index + 1) perror("malloc");
return NULL; return NULL;
}
size_t szDeviceFound = acr122_pcsc_scan(context, ncs, index + 1);
if (szDeviceFound < index + 1) {
free(ncs);
return NULL;
}
strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring)); strncpy(fullconnstring, ncs[index], sizeof(nfc_connstring));
free(ncs); free(ncs);
connstring_decode_level = acr122_pcsc_connstring_decode(fullconnstring, &ndd); 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; char *pcFirmware;
nfc_device *pnd = nfc_device_new(context, fullconnstring); nfc_device *pnd = nfc_device_new(context, fullconnstring);
pnd->driver_data = malloc(sizeof(struct acr122_pcsc_data)); pnd->driver_data = malloc(sizeof(struct acr122_pcsc_data));
if (!pnd->driver_data) {
perror("malloc");
goto error;
}
// Alloc and init chip's data // Alloc and init chip's data
pn53x_data_new(pnd, &acr122_pcsc_io); pn53x_data_new(pnd, &acr122_pcsc_io);

View file

@ -351,8 +351,23 @@ acr122_usb_connstring_decode(const nfc_connstring connstring, struct acr122_usb_
{ {
int n = strlen(connstring) + 1; int n = strlen(connstring) + 1;
char *driver_name = malloc(n); char *driver_name = malloc(n);
if (!driver_name) {
perror("malloc");
return 0;
}
char *dirname = malloc(n); char *dirname = malloc(n);
if (!dirname) {
perror("malloc");
free(driver_name);
return 0;
}
char *filename = malloc(n); char *filename = malloc(n);
if (!filename) {
perror("malloc");
free(driver_name);
free(dirname);
return 0;
}
driver_name[0] = '\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)); acr122_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name));
pnd->driver_data = malloc(sizeof(struct acr122_usb_data)); pnd->driver_data = malloc(sizeof(struct acr122_usb_data));
if (!pnd->driver_data) {
perror("malloc");
goto error;
}
*DRIVER_DATA(pnd) = data; *DRIVER_DATA(pnd) = data;
// Alloc and init chip's data // Alloc and init chip's data

View file

@ -473,6 +473,10 @@ acr122s_scan(const nfc_context *context, nfc_connstring connstrings[], const siz
pnd->driver = &acr122s_driver; pnd->driver = &acr122s_driver;
pnd->driver_data = malloc(sizeof(struct acr122s_data)); pnd->driver_data = malloc(sizeof(struct acr122s_data));
if (!pnd->driver_data) {
perror("malloc");
return -1;
}
DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->port = sp;
DRIVER_DATA(pnd)->seq = 0; 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); strcpy(pnd->name, ACR122S_DRIVER_NAME);
pnd->driver_data = malloc(sizeof(struct acr122s_data)); 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)->port = sp;
DRIVER_DATA(pnd)->seq = 0; DRIVER_DATA(pnd)->seq = 0;

View file

@ -117,6 +117,10 @@ arygon_scan(const nfc_context *context, nfc_connstring connstrings[], const size
pnd->driver = &arygon_driver; pnd->driver = &arygon_driver;
pnd->driver_data = malloc(sizeof(struct arygon_data)); pnd->driver_data = malloc(sizeof(struct arygon_data));
if (!pnd->driver_data) {
perror("malloc");
return 0;
}
DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data // 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); snprintf(pnd->name, sizeof(pnd->name), "%s:%s", ARYGON_DRIVER_NAME, ndd.port);
pnd->driver_data = malloc(sizeof(struct arygon_data)); pnd->driver_data = malloc(sizeof(struct arygon_data));
if (!pnd->driver_data) {
perror("malloc");
return NULL;
}
DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data // Alloc and init chip's data

View file

@ -90,6 +90,10 @@ pn532_uart_scan(const nfc_context *context, nfc_connstring connstrings[], const
nfc_device *pnd = nfc_device_new(context, connstring); nfc_device *pnd = nfc_device_new(context, connstring);
pnd->driver = &pn532_uart_driver; pnd->driver = &pn532_uart_driver;
pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
if (!pnd->driver_data) {
perror("malloc");
return 0;
}
DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data // 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); snprintf(pnd->name, sizeof(pnd->name), "%s:%s", PN532_UART_DRIVER_NAME, ndd.port);
pnd->driver_data = malloc(sizeof(struct pn532_uart_data)); pnd->driver_data = malloc(sizeof(struct pn532_uart_data));
if (!pnd->driver_data) {
perror("malloc");
return NULL;
}
DRIVER_DATA(pnd)->port = sp; DRIVER_DATA(pnd)->port = sp;
// Alloc and init chip's data // Alloc and init chip's data

View file

@ -234,8 +234,23 @@ pn53x_usb_connstring_decode(const nfc_connstring connstring, struct pn53x_usb_de
{ {
int n = strlen(connstring) + 1; int n = strlen(connstring) + 1;
char *driver_name = malloc(n); char *driver_name = malloc(n);
if (!driver_name) {
perror("malloc");
return 0;
}
char *dirname = malloc(n); char *dirname = malloc(n);
if (!dirname) {
perror("malloc");
free(driver_name);
return 0;
}
char *filename = malloc(n); char *filename = malloc(n);
if (!filename) {
perror("malloc");
free(driver_name);
free(dirname);
return 0;
}
driver_name[0] = '\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)); pn53x_usb_get_usb_device_name(dev, data.pudh, pnd->name, sizeof(pnd->name));
pnd->driver_data = malloc(sizeof(struct pn53x_usb_data)); pnd->driver_data = malloc(sizeof(struct pn53x_usb_data));
if (!pnd->driver_data) {
perror("malloc");
goto error;
}
*DRIVER_DATA(pnd) = data; *DRIVER_DATA(pnd) = data;
// Alloc and init chip's data // Alloc and init chip's data