ESP32 panic immediately if running strcpy .

char NTP_IP[16];
char* ntpServer = “pool.ntp.org”;

Serial.println(“Output 01”);
strcpy(NTP_IP, “209.58.185.100”);
Serial.println(“Output 02”);

strcpy(ntpServer, NTP_IP); <----- Cause panic
Serial.println(“Output 04”);

I found that the below coding will cause the ESP32 panic with below error. However, it works properly if using ESP8266.

ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8424
ho 0 tail 12 room 4
load:0x40080400,len:5868
entry 0x4008069c
Connecting to CSP-1 … CONNECTED
Output 01
Output 02
Guru Meditation Error: Core 1 panic’ed (LoadStoreError). Exception was unhandled.
Core 1 register dump:
PC : 0x40001432 PS : 0x00060430 A0 : 0x800d1934 A1 : 0x3ffb1f70
A2 : 0x3f4010fe A3 : 0x3ffc0cf1 A4 : 0x000000ff A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000032 A9 : 0x3ffb1f40
A10 : 0x3f4010fe A11 : 0x3f4010ea A12 : 0x00000003 A13 : 0x3ffbdaec
A14 : 0x3ffb8058 A15 : 0xff000000 SAR : 0x00000018 EXCCAUSE: 0x00000003
EXCVADDR: 0x3f4010fe LBEG : 0x4000142d LEND : 0x4000143a LCOUNT : 0xffffffff

Backtrace: 0x40001432:0x3ffb1f70 0x400d1931:0x3ffb1f80 0x400d320f:0x3ffb1fb0 0x4008877d:0x3ffb1fd0

Rebooting…

I’m using IDE v1.8.9 with ESP32 Espressif System v1.0.2 library. Is it a known bug? How can I fix it?

Thanks in advance.

This overwrites a string constant pointed to by ntpServer.

strcpy(ntpServer, NTP_IP);           <----- Cause panic

Instead you could change the pointer to the string in RAM.

ntpServer = NTP_IP;

gbafamily:
Instead you could change the pointer to the string in RAM.

ntpServer = NTP_IP;

Yes, it fixed the problem. Thanks.

I have another panic for the toCharArray() if ESP32.

char* ntpServer = “pool.ntp.org”;
String Data01 = “209.58.185.100”;

Serial.println(“Output 01”);
Serial.print("Data01 length : ");
Serial.println(Data01.length() + 1);
Serial.println(“Output 02”);
Data01.toCharArray(ntpServer, (Data01.length() + 1)); <— Cause panic
Serial.println(“Output 03”);

ets Jun 8 2016 00:22:57

rst:0xc (SW_CPU_RESET),boot:0x13 (SPI_FAST_FLASH_BOOT)
configsip: 0, SPIWP:0xee
clk_drv:0x00,q_drv:0x00,d_drv:0x00,cs0_drv:0x00,hd_drv:0x00,wp_drv:0x00
mode:DIO, clock div:1
load:0x3fff0018,len:4
load:0x3fff001c,len:928
ho 0 tail 12 room 4
load:0x40078000,len:8424
ho 0 tail 12 room 4
load:0x40080400,len:5868
entry 0x4008069c
Connecting to CSP-1 … CONNECTED
Output 01
Data01 length : 15
Output 02
Guru Meditation Error: Core 1 panic’ed (LoadStoreError). Exception was unhandled.
Core 1 register dump:
PC : 0x4000169e PS : 0x00060430 A0 : 0x800d2951 A1 : 0x3ffb1f40
A2 : 0x3f40110f A3 : 0x3ffb6361 A4 : 0x0000000e A5 : 0x0000ff00
A6 : 0x00ff0000 A7 : 0xff000000 A8 : 0x00000032 A9 : 0x3ffb1f10
A10 : 0x3f40110f A11 : 0x000000ff A12 : 0x00000009 A13 : 0x0000ff00
A14 : 0x00ff0000 A15 : 0xff000000 SAR : 0x00000018 EXCCAUSE: 0x00000003
EXCVADDR: 0x3f40110f LBEG : 0x40001699 LEND : 0x400016aa LCOUNT : 0xffffffff

Backtrace: 0x4000169e:0x3ffb1f40 0x400d294e:0x3ffb1f50 0x400d1985:0x3ffb1f70 0x400d33cf:0x3ffb1fb0 0x4008877d:0x3ffb1fd0

How can I fix or avoid it?

Thanks in advance.

Data01.toCharArray(ntpServer, (Data01.length() + 1));      <--- Cause panic

This copies data from Data01 to the string constant pointed to by ntpServer. Declare a char array instead and pass the size of the char array.