atitan / MFRC522_Ruby

MFRC522 RFID Reader Library for Raspberry Pi
MIT License
17 stars 2 forks source link

Potential un-handled error when reading card (nil bytes) #1

Closed lctseng closed 7 years ago

lctseng commented 7 years ago

Sometimes I got following error when reading a card

mfrc522-1.0.4/lib/mfrc522.rb:259:in `^': nil can't be coerced into Fixnum (TypeError)

Maybe there should be a error handler to handle this issue?

atitan commented 7 years ago

Sure, the buffer is not verified for valid content before doing XOR.

atitan commented 7 years ago

I've made a patch fbc0d273f82d1b264dc57d5cb966191361d873ba to handle this situation. Take a look and tell me what you think.

lctseng commented 7 years ago

I have no devices at hand now. Thus I cannot test whether the code is correct or not. I am wandering that the code in the new patch is trying to fetch all data before it exits the loop. Will that operation hang the execution? Maybe you need to handle the timeout. Namely, report an error when the card is no longer readable.

atitan commented 7 years ago

The patch didn't change the loop behavior. It only verifies the buffer when UID is thought to be completed, and clears the buffer if something went wrong.

For each cascade level, loops are hardcoded to be up to 32 according to ISO spec. Clearing the buffer during a loop does not reset the counter, so the timeout mechanism will not be bypassed.