malloc/free: some more cleaning & checking malloc errors

This commit is contained in:
Philippe Teuwen 2013-03-02 02:50:33 +01:00
parent 0708279215
commit a6c405a5d5
4 changed files with 46 additions and 0 deletions

View file

@ -78,6 +78,8 @@ pn53x_init(struct nfc_device *pnd)
if (!CHIP_DATA(pnd)->supported_modulation_as_initiator) {
CHIP_DATA(pnd)->supported_modulation_as_initiator = malloc(sizeof(nfc_modulation) * 9);
if (! CHIP_DATA(pnd)->supported_modulation_as_initiator)
return NFC_ESOFT;
int nbSupportedModulation = 0;
if ((pnd->btSupportByte & SUPPORT_ISO14443A)) {
CHIP_DATA(pnd)->supported_modulation_as_initiator[nbSupportedModulation] = NMT_ISO14443A;
@ -1649,6 +1651,8 @@ pn53x_initiator_transceive_bytes_timed(struct nfc_device *pnd, const uint8_t *pb
// We've to compute CRC ourselves to know last byte actually sent
uint8_t *pbtTxRaw;
pbtTxRaw = (uint8_t *) malloc(szTx + 2);
if (!pbtTxRaw)
return NFC_ESOFT;
memcpy(pbtTxRaw, pbtTx, szTx);
iso14443a_crc_append(pbtTxRaw, szTx);
*cycles = __pn53x_get_timer(pnd, pbtTxRaw[szTx + 1]);
@ -2929,124 +2933,153 @@ pn53x_get_information_about(nfc_device *pnd, char **pbuf)
{
size_t buflen = 2048;
*pbuf = malloc(buflen);
if (! *pbuf) {
return NFC_ESOFT;
}
char *buf = *pbuf;
int res;
if ((res = snprintf(buf, buflen, "chip: %s\n", CHIP_DATA(pnd)->firmware_text)) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
if ((res = snprintf(buf, buflen, "initator mode modulations: ")) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
const nfc_modulation_type *nmt;
if ((res = nfc_device_get_supported_modulation(pnd, N_INITIATOR, &nmt)) < 0) {
free(*pbuf);
return res;
}
for (int i = 0; nmt[i]; i++) {
if ((res = snprintf(buf, buflen, "%s%s (", (i == 0) ? "" : ", ", str_nfc_modulation_type(nmt[i]))) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
const nfc_baud_rate *nbr;
if ((res = nfc_device_get_supported_baud_rate(pnd, nmt[i], &nbr)) < 0) {
free(*pbuf);
return res;
}
for (int j = 0; nbr[j]; j++) {
if ((res = snprintf(buf, buflen, "%s%s", (j == 0) ? "" : ", ", str_nfc_baud_rate(nbr[j]))) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
}
if ((res = snprintf(buf, buflen, ")")) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
}
if ((res = snprintf(buf, buflen, "\n")) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
if ((res = snprintf(buf, buflen, "target mode modulations: ")) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
if ((res = nfc_device_get_supported_modulation(pnd, N_TARGET, &nmt)) < 0) {
free(*pbuf);
return res;
}
for (int i = 0; nmt[i]; i++) {
if ((res = snprintf(buf, buflen, "%s%s (", (i == 0) ? "" : ", ", str_nfc_modulation_type(nmt[i]))) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
const nfc_baud_rate *nbr;
if ((res = nfc_device_get_supported_baud_rate(pnd, nmt[i], &nbr)) < 0) {
free(*pbuf);
return res;
}
for (int j = 0; nbr[j]; j++) {
if ((res = snprintf(buf, buflen, "%s%s", (j == 0) ? "" : ", ", str_nfc_baud_rate(nbr[j]))) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
}
if ((res = snprintf(buf, buflen, ")")) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;
}
if ((res = snprintf(buf, buflen, "\n")) < 0) {
free(*pbuf);
return NFC_ESOFT;
}
buf += res;
if (buflen <= (size_t)res) {
free(*pbuf);
return NFC_EOVFLOW;
}
buflen -= res;