Fixes issue with client erroneously reading a byte from 0 length packets

This commit is contained in:
Lucas Jandrew 2020-02-24 01:10:09 -05:00
parent 0b8488d9bf
commit 44199a17ee
2 changed files with 21 additions and 6 deletions

View File

@ -229,9 +229,21 @@ uint16_t Adafruit_MQTT::processPacketsUntil(uint8_t *buffer, uint8_t waitforpack
if (packetType == waitforpackettype) {
return len;
} else {
// Serial.print("Packet Types: ");
// Serial.print(packetType);
// Serial.print(" ");
// Serial.println(waitforpackettype);
if (packetType == MQTT_CTRL_PUBLISH) {
handleSubscriptionPacket(len);
} else {
Serial.print("Packet dropped: ");
Serial.print(len);
Serial.print(" ");
for (int i = 0; i < MAXBUFFERSIZE; i++) {
Serial.print(this->buffer[i]);
Serial.print(" ");
}
Serial.println();
ERROR_PRINTLN(F("Dropped a packet"));
}
}
@ -247,7 +259,9 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16
// read the packet type:
rlen = readPacket(pbuff, 1, timeout);
if (rlen != 1) return 0;
if (rlen != 1) {
return 0;
}
DEBUG_PRINT(F("Packet Type:\t"));
DEBUG_PRINTBUFFER(pbuff, rlen);
@ -259,7 +273,9 @@ uint16_t Adafruit_MQTT::readFullPacket(uint8_t *buffer, uint16_t maxsize, uint16
do {
rlen = readPacket(pbuff, 1, timeout);
if (rlen != 1) return 0;
if (rlen != 1) {
return 0;
}
encodedByte = pbuff[0]; // save the last read val
pbuff++; // get ready for reading the next byte
uint32_t intermediate = encodedByte & 0x7F;

View File

@ -53,6 +53,9 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
uint16_t len = 0;
int16_t t = timeout;
if (maxlen == 0) { // handle zero-length packets
return 0;
}
while (client->connected() && (timeout >= 0)) {
//DEBUG_PRINT('.');
@ -64,10 +67,6 @@ uint16_t Adafruit_MQTT_Client::readPacket(uint8_t *buffer, uint16_t maxlen,
//DEBUG_PRINTLN((uint8_t)c, HEX);
len++;
if (maxlen == 0) { // handle zero-length packets
return 0;
}
if (len == maxlen) { // we read all we want, bail
DEBUG_PRINT(F("Read data:\t"));
DEBUG_PRINTBUFFER(buffer, len);