Fix some NFC/PN532 crashes (#4678)

* Add + use some constants, fix some crashes

* Fix PN532 crashes
This commit is contained in:
Keith Burzinski 2023-04-11 21:29:06 -05:00 committed by GitHub
parent 3d7d689040
commit 5a4840f641
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 31 additions and 9 deletions

View File

@ -44,6 +44,11 @@ NdefMessage::NdefMessage(std::vector<uint8_t> &data) {
index += id_length;
}
if ((data.begin() + index > data.end()) || (data.begin() + index + payload_length > data.end())) {
ESP_LOGE(TAG, "Corrupt record encountered; NdefMessage constructor aborting");
break;
}
std::vector<uint8_t> payload_data(data.begin() + index, data.begin() + index + payload_length);
std::unique_ptr<NdefRecord> record;

View File

@ -42,8 +42,8 @@ class NdefRecord {
virtual const std::string &get_payload() const { return this->payload_; };
virtual std::vector<uint8_t> get_encoded_payload() {
std::vector<uint8_t> empty_payload;
return empty_payload;
std::vector<uint8_t> payload(this->payload_.begin(), this->payload_.end());
return payload;
};
protected:

View File

@ -89,18 +89,18 @@ uint32_t get_mifare_classic_buffer_size(uint32_t message_length) {
}
bool mifare_classic_is_first_block(uint8_t block_num) {
if (block_num < 128) {
return (block_num % 4 == 0);
if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
} else {
return (block_num % 16 == 0);
return (block_num % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
}
}
bool mifare_classic_is_trailer_block(uint8_t block_num) {
if (block_num < 128) {
return ((block_num + 1) % 4 == 0);
if (block_num < MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW * MIFARE_CLASSIC_16BLOCK_SECT_START) {
return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW == 0);
} else {
return ((block_num + 1) % 16 == 0);
return ((block_num + 1) % MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH == 0);
}
}

View File

@ -14,6 +14,9 @@ namespace nfc {
static const uint8_t MIFARE_CLASSIC_BLOCK_SIZE = 16;
static const uint8_t MIFARE_CLASSIC_LONG_TLV_SIZE = 4;
static const uint8_t MIFARE_CLASSIC_SHORT_TLV_SIZE = 2;
static const uint8_t MIFARE_CLASSIC_BLOCKS_PER_SECT_LOW = 4;
static const uint8_t MIFARE_CLASSIC_BLOCKS_PER_SECT_HIGH = 16;
static const uint8_t MIFARE_CLASSIC_16BLOCK_SECT_START = 32;
static const uint8_t MIFARE_ULTRALIGHT_PAGE_SIZE = 4;
static const uint8_t MIFARE_ULTRALIGHT_READ_SIZE = 4;
@ -30,10 +33,18 @@ static const uint8_t TAG_TYPE_UNKNOWN = 99;
// Mifare Commands
static const uint8_t MIFARE_CMD_AUTH_A = 0x60;
static const uint8_t MIFARE_CMD_AUTH_B = 0x61;
static const uint8_t MIFARE_CMD_HALT = 0x50;
static const uint8_t MIFARE_CMD_READ = 0x30;
static const uint8_t MIFARE_CMD_WRITE = 0xA0;
static const uint8_t MIFARE_CMD_WRITE_ULTRALIGHT = 0xA2;
// Mifare Ack/Nak
static const uint8_t MIFARE_CMD_ACK = 0x0A;
static const uint8_t MIFARE_CMD_NAK_INVALID_XFER_BUFF_VALID = 0x00;
static const uint8_t MIFARE_CMD_NAK_CRC_ERROR_XFER_BUFF_VALID = 0x01;
static const uint8_t MIFARE_CMD_NAK_INVALID_XFER_BUFF_INVALID = 0x04;
static const uint8_t MIFARE_CMD_NAK_CRC_ERROR_XFER_BUFF_INVALID = 0x05;
static const char *const MIFARE_CLASSIC = "Mifare Classic";
static const char *const NFC_FORUM_TYPE_2 = "NFC Forum Type 2";
static const char *const ERROR = "Error";

View File

@ -52,7 +52,13 @@ std::unique_ptr<nfc::NfcTag> PN532::read_mifare_classic_tag_(std::vector<uint8_t
current_block++;
}
}
buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
if (buffer.begin() + message_start_index < buffer.end()) {
buffer.erase(buffer.begin(), buffer.begin() + message_start_index);
} else {
return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC);
}
return make_unique<nfc::NfcTag>(uid, nfc::MIFARE_CLASSIC, buffer);
}