From 73b5c9d0af39128d41b544fe6d8617ea944d5dc7 Mon Sep 17 00:00:00 2001 From: Philippe Teuwen Date: Tue, 5 Mar 2013 22:24:59 +0100 Subject: [PATCH] nfc_init() return rather than exit on malloc error, examples fixed accordingly --- examples/doc/quick_start_example1.c | 4 ++++ examples/doc/quick_start_example2.c | 4 ++++ examples/nfc-anticol.c | 6 +++++- examples/nfc-dep-initiator.c | 6 +++++- examples/nfc-dep-target.c | 4 ++++ examples/nfc-emulate-forum-tag2.c | 4 ++++ examples/nfc-emulate-tag.c | 4 ++++ examples/nfc-emulate-uid.c | 6 +++++- examples/nfc-mfsetuid.c | 6 +++++- examples/nfc-poll.c | 4 ++++ examples/nfc-relay.c | 6 +++++- examples/pn53x-diagnose.c | 4 ++++ examples/pn53x-sam.c | 4 ++++ examples/pn53x-tamashell.c | 4 ++++ libnfc/nfc.c | 3 +-- utils/nfc-emulate-forum-tag4.c | 4 ++++ utils/nfc-list.c | 4 ++++ utils/nfc-mfclassic.c | 6 +++++- utils/nfc-mfultralight.c | 6 +++++- utils/nfc-read-forum-tag3.c | 4 ++++ utils/nfc-relay-picc.c | 4 ++++ utils/nfc-scan-device.c | 4 ++++ 22 files changed, 92 insertions(+), 9 deletions(-) diff --git a/examples/doc/quick_start_example1.c b/examples/doc/quick_start_example1.c index 7d26a3d..59cf06d 100644 --- a/examples/doc/quick_start_example1.c +++ b/examples/doc/quick_start_example1.c @@ -36,6 +36,10 @@ main(int argc, const char *argv[]) // Initialize libnfc and set the nfc_context nfc_init(&context); + if (context == NULL) { + warnx("Unable to init libnfc (malloc)\n"); + exit(EXIT_FAILURE); + } // Display libnfc version const char *acLibnfcVersion = nfc_version(); diff --git a/examples/doc/quick_start_example2.c b/examples/doc/quick_start_example2.c index 0532ca9..dd7bdcc 100644 --- a/examples/doc/quick_start_example2.c +++ b/examples/doc/quick_start_example2.c @@ -28,6 +28,10 @@ main(int argc, const char *argv[]) // Initialize libnfc and set the nfc_context nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version const char *acLibnfcVersion = nfc_version(); diff --git a/examples/nfc-anticol.c b/examples/nfc-anticol.c index 217b267..bbbdb1b 100644 --- a/examples/nfc-anticol.c +++ b/examples/nfc-anticol.c @@ -151,12 +151,16 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Error opening NFC reader\n"); + ERR("Error opening NFC reader"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-dep-initiator.c b/examples/nfc-dep-initiator.c index 5d5da9e..acf6e31 100644 --- a/examples/nfc-dep-initiator.c +++ b/examples/nfc-dep-initiator.c @@ -73,10 +73,14 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Unable to open NFC device.\n"); + ERR("Unable to open NFC device."); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-dep-target.c b/examples/nfc-dep-target.c index 0853809..d238a27 100644 --- a/examples/nfc-dep-target.c +++ b/examples/nfc-dep-target.c @@ -72,6 +72,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } #define MAX_DEVICE_COUNT 2 nfc_connstring connstrings[MAX_DEVICE_COUNT]; size_t szDeviceFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); diff --git a/examples/nfc-emulate-forum-tag2.c b/examples/nfc-emulate-forum-tag2.c index e569d78..f76ca44 100644 --- a/examples/nfc-emulate-forum-tag2.c +++ b/examples/nfc-emulate-forum-tag2.c @@ -187,6 +187,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } pnd = nfc_open(context, NULL); if (pnd == NULL) { diff --git a/examples/nfc-emulate-tag.c b/examples/nfc-emulate-tag.c index aa73e7e..68e8766 100644 --- a/examples/nfc-emulate-tag.c +++ b/examples/nfc-emulate-tag.c @@ -181,6 +181,10 @@ main(int argc, char *argv[]) #endif nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version acLibnfcVersion = nfc_version(); diff --git a/examples/nfc-emulate-uid.c b/examples/nfc-emulate-uid.c index 11d861f..969848f 100644 --- a/examples/nfc-emulate-uid.c +++ b/examples/nfc-emulate-uid.c @@ -128,12 +128,16 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC device pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Unable to open NFC device\n"); + ERR("Unable to open NFC device"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-mfsetuid.c b/examples/nfc-mfsetuid.c index 9311873..b785a65 100644 --- a/examples/nfc-mfsetuid.c +++ b/examples/nfc-mfsetuid.c @@ -179,12 +179,16 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Error opening NFC reader\n"); + ERR("Error opening NFC reader"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/nfc-poll.c b/examples/nfc-poll.c index 0bb1e8f..7c2d079 100644 --- a/examples/nfc-poll.c +++ b/examples/nfc-poll.c @@ -105,6 +105,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } pnd = nfc_open(context, NULL); diff --git a/examples/nfc-relay.c b/examples/nfc-relay.c index 7b5048c..8dbdcd9 100644 --- a/examples/nfc-relay.c +++ b/examples/nfc-relay.c @@ -110,6 +110,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices size_t szFound = nfc_list_devices(context, connstrings, MAX_DEVICE_COUNT); @@ -123,7 +127,7 @@ main(int argc, char *argv[]) // Try to open the NFC emulator device pndTag = nfc_open(context, connstrings[0]); if (pndTag == NULL) { - printf("Error opening NFC emulator device\n"); + ERR("Error opening NFC emulator device"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/examples/pn53x-diagnose.c b/examples/pn53x-diagnose.c index 97f0848..f747b39 100644 --- a/examples/pn53x-diagnose.c +++ b/examples/pn53x-diagnose.c @@ -69,6 +69,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version acLibnfcVersion = nfc_version(); diff --git a/examples/pn53x-sam.c b/examples/pn53x-sam.c index 61e3924..f4baabe 100644 --- a/examples/pn53x-sam.c +++ b/examples/pn53x-sam.c @@ -77,6 +77,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version const char *acLibnfcVersion = nfc_version(); diff --git a/examples/pn53x-tamashell.c b/examples/pn53x-tamashell.c index bc59177..eac9993 100644 --- a/examples/pn53x-tamashell.c +++ b/examples/pn53x-tamashell.c @@ -85,6 +85,10 @@ int main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); diff --git a/libnfc/nfc.c b/libnfc/nfc.c index e9a8a9c..50d6272 100644 --- a/libnfc/nfc.c +++ b/libnfc/nfc.c @@ -174,8 +174,7 @@ nfc_init(nfc_context **context) *context = nfc_context_new(); if (!context) { perror("malloc"); - // TODO: not a good idea to call exit() from a library, we should change API and return error - exit(EXIT_FAILURE); + return; } if (!nfc_drivers) nfc_drivers_init(); diff --git a/utils/nfc-emulate-forum-tag4.c b/utils/nfc-emulate-forum-tag4.c index 7ef5e10..cfd94e7 100644 --- a/utils/nfc-emulate-forum-tag4.c +++ b/utils/nfc-emulate-forum-tag4.c @@ -380,6 +380,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)\n"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); diff --git a/utils/nfc-list.c b/utils/nfc-list.c index d50b469..47d485c 100644 --- a/utils/nfc-list.c +++ b/utils/nfc-list.c @@ -71,6 +71,10 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Display libnfc version acLibnfcVersion = nfc_version(); diff --git a/utils/nfc-mfclassic.c b/utils/nfc-mfclassic.c index 83138f2..081be3b 100644 --- a/utils/nfc-mfclassic.c +++ b/utils/nfc-mfclassic.c @@ -503,11 +503,15 @@ main(int argc, const char *argv[]) // printf("Successfully opened required files\n"); nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC reader pnd = nfc_open(context, NULL); if (pnd == NULL) { - printf("Error opening NFC reader\n"); + ERR("Error opening NFC reader"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/utils/nfc-mfultralight.c b/utils/nfc-mfultralight.c index 178e60b..b1e3e79 100644 --- a/utils/nfc-mfultralight.c +++ b/utils/nfc-mfultralight.c @@ -206,11 +206,15 @@ main(int argc, const char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } // Try to open the NFC device pnd = nfc_open(context, NULL); if (pnd == NULL) { - ERR("Error opening NFC device\n"); + ERR("Error opening NFC device"); nfc_exit(context); exit(EXIT_FAILURE); } diff --git a/utils/nfc-read-forum-tag3.c b/utils/nfc-read-forum-tag3.c index a075627..27ce150 100644 --- a/utils/nfc-read-forum-tag3.c +++ b/utils/nfc-read-forum-tag3.c @@ -198,6 +198,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)\n"); + exit(EXIT_FAILURE); + } pnd = nfc_open(context, NULL); diff --git a/utils/nfc-relay-picc.c b/utils/nfc-relay-picc.c index 9dcafa2..90221c8 100644 --- a/utils/nfc-relay-picc.c +++ b/utils/nfc-relay-picc.c @@ -197,6 +197,10 @@ main(int argc, char *argv[]) nfc_context *context; nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)"); + exit(EXIT_FAILURE); + } nfc_connstring connstrings[MAX_DEVICE_COUNT]; // List available devices diff --git a/utils/nfc-scan-device.c b/utils/nfc-scan-device.c index 72a28bc..9bfebee 100644 --- a/utils/nfc-scan-device.c +++ b/utils/nfc-scan-device.c @@ -90,6 +90,10 @@ main(int argc, const char *argv[]) } nfc_init(&context); + if (context == NULL) { + ERR("Unable to init libnfc (malloc)\n"); + exit(EXIT_FAILURE); + } // Display libnfc version acLibnfcVersion = nfc_version();