Error conditions in utils & examples: fix leaks, unify style (see details)

* in main():
** errx()/err()/return -> exit()
** return values -> EXIT_SUCCESS & EXIT_FAILURE

* out of main:
** err()/errx()/exit() -> return
** change retval from size_t to int to allow returning errors
** don't use EXIT_SUCCESS / EXIT_FAILURE as retvals

* add nfc_close() & nfc_exit() to exit() on errors
* add missing fclose() on errors
* add missing test if (pnd == NULL)
* unify style if (pnd == / != NULL)
* remove goto's
* few related fixes
* remove if(pnd!=NULL) test on nfc_close() calls
This commit is contained in:
Philippe Teuwen
2013-03-05 19:44:59 +01:00
parent 232930c3d5
commit bece73faaf
21 changed files with 433 additions and 298 deletions

View File

@@ -134,6 +134,7 @@ main(int argc, char *argv[])
if (pnd == NULL) {
printf("Unable to open NFC device\n");
nfc_exit(context);
exit(EXIT_FAILURE);
}
@@ -162,13 +163,17 @@ main(int argc, char *argv[])
if ((szRecvBits = nfc_target_init(pnd, &nt, abtRecv, sizeof(abtRecv), 0)) < 0) {
nfc_perror(pnd, "nfc_target_init");
ERR("Could not come out of auto-emulation, no command was received");
goto error;
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
printf("[+] Received initiator command: ");
print_hex_bits(abtRecv, (size_t) szRecvBits);
printf("[+] Configuring communication\n");
if ((nfc_device_set_property_bool(pnd, NP_HANDLE_CRC, false) < 0) || (nfc_device_set_property_bool(pnd, NP_HANDLE_PARITY, true) < 0)) {
nfc_perror(pnd, "nfc_device_set_property_bool");
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
printf("[+] Done, the emulated tag is initialized with UID: %02X%02X%02X%02X\n\n", abtUidBcc[0], abtUidBcc[1],
@@ -211,7 +216,9 @@ main(int argc, char *argv[])
// Send and print the command to the screen
if (nfc_target_send_bits(pnd, pbtTx, szTxBits, NULL) < 0) {
nfc_perror(pnd, "nfc_target_send_bits");
goto error;
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}
if (!quiet_output) {
printf("T: ");
@@ -223,9 +230,4 @@ main(int argc, char *argv[])
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_SUCCESS);
error:
nfc_close(pnd);
nfc_exit(context);
exit(EXIT_FAILURE);
}