sprintf crashing esp8266

In this receive code the sprintf() is causing a wdt crash.

. connected
Now listening at IP 192.168.4.100, UDP port 4220
Received 12 bytes from 192.168.4.1, port 4210
UDP packet contents: NODECMCMNODE
Received 12 bytes from 192.168.4.1, port 4210

Exception (9):
epc1=0x402025b5 epc2=0x00000000 epc3=0x00000000 excvaddr=0x00454493 depc=0x00000000

>>>stack>>>

ctx: cont
sp: 3ffffdc0 end: 3fffffc0 offset: 01a0
3fffff60:  00000000 00000000 00000000 402041c0  
3fffff70:  0104a8c0 3ffe84dc 3ffee528 40202530  
3fffff80:  3ffef3c4 00000000 00000000 402041c0  
3fffff90:  00000000 00000000 00000001 3ffee5a8  
3fffffa0:  3fffdad0 00000000 3ffee578 40203d58  
3fffffb0:  feefeffe feefeffe 3ffe8514 40100869  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,6)


 ets Jan  8 2013,rst cause:4, boot mode:(1,6)

wdt reset

This is the receiving code

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

IPAddress Server(192, 168, 4, 1);
WiFiUDP Udp;

const char* ssid = "udpserver";
const char* password = "12345678";
char verifyStart[7];
char verifyEnd[7];
unsigned int localUdpPort = 4220;  // local port to listen on
char incomingPacket[255];  // buffer for incoming packets
int ServerPort = 4210;

unsigned long now = 0;
unsigned long now2 = 0;
unsigned long now3 = 0;
struct dataStruct {
  int CounterUP = 0;
  int CounterDN = 0;
  unsigned long duration = 5050;
};

dataStruct localData;
void setup()
{
  pinMode(5, OUTPUT);
  digitalWrite(5, LOW);
  pinMode(4, OUTPUT);
  digitalWrite(4, LOW);
  Serial.begin(115200);
  Serial.println();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED)
  {
    delay(500);
    Serial.print(".");
  }
  Serial.println(" connected");

  Udp.begin(localUdpPort);
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.localIP().toString().c_str(), localUdpPort);
}

void loop()
{
  int packetSize = Udp.parsePacket();
  if (packetSize) {
    Serial.printf("Received %d bytes from %s, port %d\n", packetSize, Udp.remoteIP().toString().c_str(), Udp.remotePort());
    int len = Udp.read(incomingPacket, 800);
    sprintf (verifyStart, (char*)incomingPacket, 7 );
    //sprintf (verifyEnd, (char *)incomingPacket + len - 6 , 7 );
    Serial.printf("UDP packet contents: %s\n", incomingPacket);
    digitalWrite(5, HIGH);
    now3 = millis();
  }
}

here is the sending code,

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

IPAddress local_IP(192, 168, 4, 1);
IPAddress gateway(192, 168, 4, 1);
IPAddress subnet(255, 255, 255, 0);

IPAddress Client(192, 168, 4, 100);
const char* ssid = "udpserver";
const char* password = "12345678";
unsigned long now = 0;
unsigned long now2 = 0;
WiFiUDP Udp;
unsigned int localUdpPort = 4210;  // local port to listen on
unsigned int clientPort = 4220;
char incomingPacket[800];  // buffer for incoming packets
char  replyPacket[] = "Hi there! Got the message :-)";  // a reply string to send back
char verifyStart[7];
char verifyEnd[7];

void setup()
{
  Serial.begin(115200);
  Serial.println();
  pinMode(5, OUTPUT);
  pinMode(2, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(0, OUTPUT);
  pinMode(14, OUTPUT);
  digitalWrite(14, LOW);
  digitalWrite(4, LOW);
  digitalWrite(0, LOW);
  digitalWrite(2, LOW);
  digitalWrite(5, LOW);
  //Serial.printf("Connecting to %s ", ssid);
  //WiFi.begin(ssid, password);

  while (!WiFi.softAP(ssid, password, 6, false, 15)) {
    delay(500);
    Serial.print(".");
  }
  Serial.println(WiFi.softAPConfig(local_IP, gateway, subnet) ? "Ready" : "Failed!");
  Serial.println(" connected");

  Udp.begin(localUdpPort);
  Serial.printf("Now listening at IP %s, UDP port %d\n", WiFi.softAPIP().toString().c_str(), localUdpPort);
}


void loop()
{
  if (millis() - now >= 200) {
    digitalWrite(5, LOW);
    digitalWrite(2, LOW);
    digitalWrite(0, LOW);
    digitalWrite(4, LOW);
    digitalWrite(14, LOW);
    now = millis();
  }
    if (millis() - now2 >= 1000) {
     const char delimiter[] = "NODECM";
    const char delimiter2[] = "CMNODE";
    Udp.write((const uint8_t *)delimiter, sizeof(delimiter) - 1);
    //Udp.write((const uint8_t *)&localData, sizeof(localData));
    Udp.write((const uint8_t *)delimiter2, sizeof(delimiter2) - 1);
    Udp.beginPacket(Client, clientPort);
    //Udp.write((char*)&buffer, sizeof(buffer));
    Udp.endPacket();
    now2 = millis();
    digitalWrite(5, HIGH);
  }
}

the first sprintf() crashes the esp the second one does not?

    sprintf (verifyStart, (char*)incomingPacket, 7 ); //crashes program
    sprintf (verifyEnd, (char *)incomingPacket + len - 6 , 7 );//does not crash program

EDIT: I just noticed the UDP.Writes are in the wrong place. ill fixt hat and see if it helps

EDIT2: Did not help..

I don't think sprintf does what you think it does, have a look at the documentation.

https://en.cppreference.com/w/cpp/io/c/fprintf
http://www.cplusplus.com/reference/cstdio/sprintf/

Prefer snprintf over sprintf to prevent buffer overflows.

Also, when you get a stack trace, decode it so you know where it went wrong.

Pieter

PieterP:
I don't think sprintf does what you think it does, have a look at the documentation.

std::printf, std::fprintf, std::sprintf, std::snprintf - cppreference.com
http://www.cplusplus.com/reference/cstdio/sprintf/

Prefer snprintf over sprintf to prevent buffer overflows.

Also, when you get a stack trace, decode it so you know where it went wrong.

GitHub - me-no-dev/EspExceptionDecoder: Exception Stack Trace Decoder for ESP8266 and ESP32

Pieter

Please enlighten me, i thought i was printing the first 7 bytes of incoming packet to verifyStart so i can compare the string later??

No, that's not what it does. Did you read the documentation?

You need strncpy. (And don't forget to explicitly null terminate it, either in initialization, or after strncpy'ing.)

The stack trace says,,

Decoding stack results
0x40202530: setup() at C:\Users\User\Documents\Arduino\esp_udp_clent/esp_udp_clent.ino line 43
0x40203d58: __yield() at C:\Users\User\AppData\Local\Arduino15\packages\esp8266\hardware\esp8266\2.5.0\cores\esp8266\core_esp8266_main.cpp line 97

And as for sprintf()

sprintf stands for “String print”. Instead of printing on console, it store output on char buffer which are specified in sprintf

is this not what i want. do i not want to store the string into the char buffer. what do i not get?

your method works perfectly,. i still don't understand why the first method did not work

The second argument to sprintf is a format string, and the remaining arguments are variadic, containing the values to replace the format specifiers with.

Please read the documentation and examples I linked to.

Thanks for you help. Ill read everything i can about them

notsolowki:
. i still don't understand why the first method did not work

I mentioned back in yesterday's thread that you were using sprintf incorrectly and provided a reference link.

So if anything i should have used snprintf() ? to get the string into the buffer without the format params. I still don't understand what it means by format exactly.

notsolowki:
So if anything i should have used snprintf() ? to get the string into the buffer without the format params. I still don't understand what it means by format exactly.

Check out the documentstion for the printf function. sprintf and snprintf are extensions of printf with added / different functionality.

EDIT: If you only want to copy a string from one buffer to another buffer, then you can use strcpy or strncpy.

notsolowki:
So if anything i should have used snprintf() ? to get the string into the buffer without the format params.

All members of the printf() function family require the formatting string. Did you read the references provided?

gfvalvo:
All members of the printf() function family require the formatting string. Did you read the references provided?

Sorry i didn't read printf() yet. Iv'e been trying to implement some type of ACK in my esp udp server. It actually worked out good so far. It takes 3ms from the time i press the button to send a message. then about 3ms for the ack to be received. even in slightly congested condition according to my oscilloscope no more than 5ms