From 61fd783ad16009239095014b1e3bc7c361fbaf1a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Sun, 12 Jul 2026 18:41:15 +0200 Subject: [PATCH 1/2] added castings, added const to locals that are assigned only once --- src/PubSubClient.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/PubSubClient.cpp b/src/PubSubClient.cpp index dd00d738..e173d9c2 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; // this might change by 2 if we have a QoS 1 or 2 message + 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(); } From 6832ee3cd02b3c628fdef4364a9ee82e6f232987 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Holger=20Mu=CC=88ller?= Date: Sun, 12 Jul 2026 22:01:21 +0200 Subject: [PATCH 2/2] removed misleading comment --- src/PubSubClient.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/PubSubClient.cpp b/src/PubSubClient.cpp index e173d9c2..7e6aa0d9 100755 --- a/src/PubSubClient.cpp +++ b/src/PubSubClient.cpp @@ -386,7 +386,7 @@ bool PubSubClient::handlePacket(uint8_t hdrLen, size_t length) { 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; // this might change by 2 if we have a QoS 1 or 2 message + const size_t payloadLen = length - payloadOffset; uint8_t* const payload = _buffer + payloadOffset; // Guard 2: ensure topic fits in buffer