1

Topic: Bug in nfc-mfclassic.c

Hi there,

the current version of nfc-mfclassic.c (revision 178) contains a small bug:
get_trailer_block assumes that we are at the first block, when calculating the trailer block number,
but when authenticating in read_card(), that's not always true.
(BTW: Why does the block reading start at the last block, not the first?)

I fixed it like this:

===================================================================
--- examples/nfc-mfclassic.c    (revision 178)
+++ examples/nfc-mfclassic.c    (working copy)
@@ -69,8 +69,17 @@
 
 uint32_t get_trailer_block(uint32_t uiFirstBlock)
 {
+   uint32_t trailer_block = 0;
+
+   if (uiFirstBlock < 128) {
+      trailer_block = uiFirstBlock + (3 - (uiFirstBlock % 4));
+   } else {
+      trailer_block = uiFirstBlock + (15 - (uiFirstBlock % 16));
+   }
+   return trailer_block;
+
   // Test if we are in the small or big sectors
-  if (uiFirstBlock<128) return uiFirstBlock+3; else return uiFirstBlock+15;
+  // if (uiFirstBlock<128) return uiFirstBlock+3; else return uiFirstBlock+15;
 }
 

2

Re: Bug in nfc-mfclassic.c

One more thing:

If writing of a trailer block fails, the next authentication will also fail. The fix is to set bFailure to true in this case:

       // Try to write the trailer
-      nfc_initiator_mifare_cmd(pdi,MC_WRITE,uiBlock,&mp);
+      if (nfc_initiator_mifare_cmd(pdi,MC_WRITE,uiBlock,&mp) == false) {
+          printf("failed to write trailer block %d \n", uiBlock);
+          bFailure = true;
+      }

Re: Bug in nfc-mfclassic.c

Hello,

Thanks for reports and patches, ud !

Theses 2 issues have been fixed in r179, thanks a lot !

Feel free to use the issue tracker at http://code.google.com/p/libnfc/issues to report bugs and you can also send .diff or .patch attached with your issue.

Romuald Conty