Skip to main content

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index] [List Home]
Re: [paho-dev] Embedded MQTT C transport API

Hi everyone, hi Ian
I happened to have found a bug in my MQTTPacket.c non-blocking
functions, particularly the one that extracts the message length:
MQTTPacket_decodenb()

On really slow links, if TCP (or whatever underlying transport) fails to
provide the whole length bytes before this function is called 4 times,
next call will exit with an error code. The problem is that the function
is also counting "additional entries" in addition to properly counting
received bytes.
Correction is as follows (patch attached):
´´´
diff --git a/MQTTPacket/src/MQTTPacket.c b/MQTTPacket/src/MQTTPacket.c
index bd5f90a..e4746cd 100644
--- a/MQTTPacket/src/MQTTPacket.c
+++ b/MQTTPacket/src/MQTTPacket.c
@@ -333,14 +333,15 @@ static int MQTTPacket_decodenb(MQTTTransport *trp)
        }
        do {
                int frc;
-               if (++(trp->len) > MAX_NO_OF_REMAINING_LENGTH_BYTES)
+               if (trp->len >= MAX_NO_OF_REMAINING_LENGTH_BYTES)
                        goto exit;
                if ((frc=(*trp->getfn)(trp->sck, &c, 1)) == -1)
                        goto exit;
                if (frc == 0){
                        rc = 0;
                        goto exit;
-               }
+               }
+               ++(trp->len);
                trp->rem_len += (c & 127) * trp->multiplier;
                trp->multiplier *= 128;
        } while ((c & 128) != 0);
´´´

PS: I don't know if I'm still allowed to submit this via git to Gerrit
(nor if I still remember how to do it...), I can reconnect with this
very subject on december, this bug hunting has been done as part of a
project ending nov 30th, which will also provide new samples and a
serial transport to Paho MQTT. See you later guys.

diff --git a/MQTTPacket/src/MQTTPacket.c b/MQTTPacket/src/MQTTPacket.c
index bd5f90a..e4746cd 100644
--- a/MQTTPacket/src/MQTTPacket.c
+++ b/MQTTPacket/src/MQTTPacket.c
@@ -333,14 +333,15 @@ static int MQTTPacket_decodenb(MQTTTransport *trp)
 	}
 	do {
 		int frc;
-		if (++(trp->len) > MAX_NO_OF_REMAINING_LENGTH_BYTES)
+		if (trp->len >= MAX_NO_OF_REMAINING_LENGTH_BYTES)
 			goto exit;
 		if ((frc=(*trp->getfn)(trp->sck, &c, 1)) == -1)
 			goto exit;
 		if (frc == 0){
 			rc = 0;
 			goto exit;
-		}
+		}
+		++(trp->len);
 		trp->rem_len += (c & 127) * trp->multiplier;
 		trp->multiplier *= 128;
 	} while ((c & 128) != 0);

Back to the top