From f139519061bdb81dfc6d41e9cd704fd455f1647a Mon Sep 17 00:00:00 2001 From: ufocia <49005154+ufocia@users.noreply.github.com> Date: Wed, 27 Mar 2019 19:27:20 -0500 Subject: [PATCH] Correct out of bounds condition and remove an unnecessary cast (#5924) C arrays are 0 indexed, accordingly writing to array[255] actually writes to the 256th position, which is naturally out of bounds of a 255 item array. increasing the size of the array to 256 corrects that error. Also, 0 is an int and requires a cast into a char, '\0' is just a cleaner (a more syntactically pedantic) way to achieve the same end. --- doc/esp8266wifi/udp-examples.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/doc/esp8266wifi/udp-examples.rst b/doc/esp8266wifi/udp-examples.rst index ffd8375af..9047fde18 100644 --- a/doc/esp8266wifi/udp-examples.rst +++ b/doc/esp8266wifi/udp-examples.rst @@ -35,7 +35,7 @@ Once we have libraries in place we need to create a ``WiFiUDP`` object. Then we WiFiUDP Udp; unsigned int localUdpPort = 4210; - char incomingPacket[255]; + char incomingPacket[256]; char replyPacket[] = "Hi there! Got the message :-)"; Wi-Fi Connection @@ -68,7 +68,7 @@ Waiting for incoming UDP packed is done by the following code: int len = Udp.read(incomingPacket, 255); if (len > 0) { - incomingPacket[len] = 0; + incomingPacket[len] = '\0'; } Serial.printf("UDP packet contents: %s\n", incomingPacket);