Skip to content

Commit

Permalink
Reduce stack usage in loop(). No need to duplicate
Browse files Browse the repository at this point in the history
topic string onto stack before giving it to callback()
Just move it one byte in buffer to add space for 'C' string end \0x00
  • Loading branch information
edwin-oetelaar committed Jun 26, 2016
1 parent 35ead34 commit d724864
Showing 1 changed file with 4 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/PubSubClient.cpp
Expand Up @@ -306,12 +306,10 @@ boolean PubSubClient::loop() {
uint8_t type = buffer[0]&0xF0;
if (type == MQTTPUBLISH) {
if (callback) {
uint16_t tl = (buffer[llen+1]<<8)+buffer[llen+2];
char topic[tl+1];
for (uint16_t i=0;i<tl;i++) {
topic[i] = buffer[llen+3+i];
}
topic[tl] = 0;
uint16_t tl = (buffer[llen+1]<<8)+buffer[llen+2]; /* topic length in bytes */
memmove(buffer+llen+2,buffer+llen+3,tl); /* move topic inside buffer 1 byte to front */
buffer[llen+2+tl] = 0; /* end the topic as a 'C' string with \x00 */
char *topic = (char*) buffer+llen+2;
// msgId only present for QOS>0
if ((buffer[0]&0x06) == MQTTQOS1) {
msgId = (buffer[llen+3+tl]<<8)+buffer[llen+3+tl+1];
Expand Down

0 comments on commit d724864

Please sign in to comment.