Writing some code to serve a TCP and creating a character pointer to hold the received data. I then malloc the buffer, receive the data, decrypt it and try and free it. When I try to free I get the below error. As soon as I comment the free, the program works, but I'm pretty sure that with enough client requests, the esp will crash anyway. Any ideas what I'm doing wrong? I've tried various things, including avoiding malloc altogether, however, the netbuf_data instruction doesnt seem to work with a statically allocated array. This is the function that causes the esp to abort.
Code: Select all
static void networkTCPServe(struct netconn *conn)
{
err_t err;
struct netbuf *inbuf;
u16_t buflen = 16;
unsigned char plaintext[17];
/* Read the data from the port, blocking if nothing yet there.
We assume the request (the part we care about) is in one netbuf */
err = netconn_recv(conn, &inbuf);
if (err == ERR_OK)
{
char* buf = malloc((size_t)buflen);
netbuf_data(inbuf, (void**)&buf, &buflen);//16 byte for 128bit block size used in AES
networkAES256CBCDecrypt(buf, buflen, plaintext);
free(buf); //PROGRAM CRASHING
serialEnqueue(plaintext, buflen);
}
else
{
ESP_LOGE(TAG, "networkReceive: recv: %d %s", err, strerror(errno));
}
/* Close the connection (server closes in TCP) */
netconn_close(conn);
/* Delete the buffer (netconn_recv gives us ownership,
so we have to make sure to deallocate the buffer) */
netbuf_delete(inbuf);
}
Code: Select all
C:/SysGCC/esp32/esp32-bsp/esp-idf/components/freertos/heap_regions.c:368 (vPortF
reeTagged)- assert failed!
Guru Meditation Error: Core 0 panic'ed (Unhandled debug exception)
Register dump:
PC : 0x40009203 PS : 0x00060136 A0 : 0x800d2be9 A1 : 0x3f
fd9dd0
A2 : 0x0000006b A3 : 0x0000006b A4 : 0x00000010 A5 : 0xff
ffffff
A6 : 0x00000000 A7 : 0xfffffff0 A8 : 0x00000000 A9 : 0x3f
fd9d40
A10 : 0x00000000 A11 : 0x3f4048bf A12 : 0x3ffd9d0f A13 : 0x00
000038
A14 : 0x00000000 A15 : 0x3ffd9d14 SAR : 0x00000004 EXCCAUSE: 0x00
000001
EXCVADDR: 0x00000000 LBEG : 0x4000c2e0 LEND : 0x4000c2f6 LCOUNT : 0x00
000000
Backtrace: 0x40009203:0x3ffd9dd0 0x400d2be9:0x3ffd9df0 0x400d35b9:0x3ffd9e10 0x
4008202c:0x3ffd9e30 0x4000beca:0x3ffd9e50 0x400eb16b:0x3ffd9e70 0x400eb0f9:0x3ff
d9ed0