Pārlūkot izejas kodu

Fix buffer overrun bug

Christopher Mullins 8 gadi atpakaļ
vecāks
revīzija
0b2fbe3f91

+ 3 - 3
lib/MiLight/MiLightClient.cpp

@@ -124,11 +124,11 @@ void MiLightClient::write(uint8_t packet[]) {
   }
 
 #ifdef DEBUG_PRINTF
-  printf("Sending packet (%d repeats): ", this->currentResendCount);
+  Serial.printf("Sending packet (%d repeats): \n", this->currentResendCount);
   for (int i = 0; i < currentRemote->packetFormatter->getPacketLength(); i++) {
-    printf("%02X", packet[i]);
+    Serial.printf("%02X ", packet[i]);
   }
-  printf("\n");
+  Serial.println();
   int iStart = millis();
 #endif
 

+ 6 - 0
lib/MiLight/PacketFormatter.cpp

@@ -116,6 +116,12 @@ void PacketFormatter::pushPacket() {
     finalizePacket(currentPacket);
   }
 
+  // Make sure there's enough buffer to add another packet.
+  if ((currentPacket + packetLength) > PACKET_BUFFER + PACKET_FORMATTER_BUFFER_SIZE) {
+    Serial.println(F("ERROR: packet buffer full!  Cannot buffer a new packet.  THIS IS A BUG!"));
+    return;
+  }
+
   currentPacket = PACKET_BUFFER + (numPackets * packetLength);
   numPackets++;
   initializePacket(currentPacket);

+ 4 - 1
lib/MiLight/PacketFormatter.h

@@ -9,7 +9,10 @@
 #ifndef _PACKET_FORMATTER_H
 #define _PACKET_FORMATTER_H
 
-#define PACKET_FORMATTER_BUFFER_SIZE 48
+// Most packets sent is for CCT bulbs, which always includes 10 down commands
+// and can include up to 10 up commands.  CCT packets are 7 bytes.
+//   (10 * 7) + (10 * 7) = 140
+#define PACKET_FORMATTER_BUFFER_SIZE 140
 
 struct PacketStream {
   PacketStream();

+ 1 - 1
platformio.ini

@@ -25,7 +25,7 @@ lib_deps_external =
   CircularBuffer
 extra_scripts =
   pre:.build_web.py
-build_flags = !python .get_version.py -DMQTT_MAX_PACKET_SIZE=200 -Idist -Ilib/DataStructures 
+build_flags = !python .get_version.py -DMQTT_MAX_PACKET_SIZE=200 -Idist -Ilib/DataStructures
 # -D DEBUG_PRINTF
 # -D MQTT_DEBUG
 # -D MILIGHT_UDP_DEBUG