From 3e00c7899aff137ed267507e864832a6897c4386 Mon Sep 17 00:00:00 2001
From: Romuald Conty <romuald@libnfc.org>
Date: Fri, 8 Jan 2010 21:00:57 +0000
Subject: [PATCH] mifare_classic_get_tags() now return NULL when on failure and
 a one-entry array set to NULL when no tag is available (tags[0] == NULL).

---
 libfreefare/mifare_classic.c | 20 +++++++++-----------
 1 file changed, 9 insertions(+), 11 deletions(-)

diff --git a/libfreefare/mifare_classic.c b/libfreefare/mifare_classic.c
index bdda4e6..99164e4 100644
--- a/libfreefare/mifare_classic.c
+++ b/libfreefare/mifare_classic.c
@@ -185,6 +185,10 @@ mifare_classic_get_tags (nfc_device_t *device)
     // Poll for a ISO14443A (MIFARE) tag
     nfc_target_info_t target_info;
 
+    tags = malloc(sizeof (void *));
+    if(!tags) return NULL;
+    tags[0] = NULL;
+
     while (nfc_initiator_select_tag(device,NM_ISO14443A_106,NULL,0,&target_info)) {
 
 	// Ensure the target is a MIFARE classic tag.
@@ -202,17 +206,11 @@ mifare_classic_get_tags (nfc_device_t *device)
 	tag_count++;
 
 	/* (Re)Allocate memory for the found MIFARE classic array */
-	if (!tags) {
-	    if (!(tags = malloc ((tag_count) * sizeof (MifareClassicTag) + sizeof (void *)))) {
-	    	return NULL;
-	    }
-	} else {
-	    MifareClassicTag *p = realloc (tags, (tag_count) * sizeof (MifareClassicTag) + sizeof (void *));
-	    if (p)
-		tags = p;
-	    else
-		return p; // FAIL! Return what has been found so far.
-	}
+	MifareClassicTag *p = realloc (tags, (tag_count) * sizeof (MifareClassicTag) + sizeof (void *));
+	if (p)
+	    tags = p;
+	else
+	    return tags; // FAIL! Return what has been found so far.
 
 	/* Allocate memory for the found MIFARE classic tag */
 	if (!(tags[tag_count-1] = malloc (sizeof (struct mifare_classic_tag)))) {