diff --git a/src/PubSubClient.cpp b/src/PubSubClient.cpp index dd00d738..7e6aa0d9 100755 --- a/src/PubSubClient.cpp +++ b/src/PubSubClient.cpp @@ -383,11 +383,11 @@ bool PubSubClient::handlePacket(uint8_t hdrLen, size_t length) { DEBUG_PSC_PRINTF("handlePacket(): Packet too short to contain topic length field\n"); return false; } - uint16_t topicLen = (_buffer[hdrLen + 1] << 8) + _buffer[hdrLen + 2]; // topic length in bytes - char* topic = (char*)(_buffer + hdrLen + 3 - 1); // set the topic in the LSB of the topic lenght, as we move it there - uint16_t payloadOffset = hdrLen + 3 + topicLen; // payload starts after header and topic (if there is no packet identifier) - size_t payloadLen = length - payloadOffset; // this might change by 2 if we have a QoS 1 or 2 message - uint8_t* payload = _buffer + payloadOffset; + const uint16_t topicLen = (_buffer[hdrLen + 1] << 8) + _buffer[hdrLen + 2]; // topic length in bytes + char* const topic = (char*)(_buffer + hdrLen + 3 - 1); // set the topic in the LSB of the topic lenght, as we move it there + const uint16_t payloadOffset = hdrLen + 3 + topicLen; // payload starts after header and topic (if there is no packet identifier) + const size_t payloadLen = length - payloadOffset; + uint8_t* const payload = _buffer + payloadOffset; // Guard 2: ensure topic fits in buffer if (length < payloadOffset) { @@ -406,16 +406,16 @@ bool PubSubClient::handlePacket(uint8_t hdrLen, size_t length) { DEBUG_PSC_PRINTF("handlePacket(): Missing msgId in QoS 1/2 message\n"); return false; } - uint8_t publishQos = MQTT_HDR_GET_QOS(_buffer[0]); // save QoS before _buffer[0] is overwritten - uint16_t msgId = (_buffer[payloadOffset] << 8) + _buffer[payloadOffset + 1]; + const uint8_t publishQos = MQTT_HDR_GET_QOS(_buffer[0]); // save QoS before _buffer[0] is overwritten + const uint16_t msgId = (_buffer[payloadOffset] << 8) + _buffer[payloadOffset + 1]; callback(topic, payload + 2, payloadLen - 2); // remove the msgId from the callback payload // QoS 1: respond with PUBACK // QoS 2: respond with PUBREC (first step of the QoS 2 subscriber handshake) _buffer[0] = (publishQos == MQTT_QOS1) ? MQTTPUBACK : MQTTPUBREC; _buffer[1] = 2; - _buffer[2] = (msgId >> 8); - _buffer[3] = (msgId & 0xFF); + _buffer[2] = (uint8_t)(msgId >> 8); + _buffer[3] = (uint8_t)(msgId & 0xFF); if (_client->write(_buffer, 4) == 4) { _lastOutActivity = millis(); }