Trampas Stern a écrit : > You also need to send the rest of the code. > > For example it could be that rx_len is where memory overflow is happening > > cascade: > * rx_len = sizeof(sak);* > snprintf(t, 3, "%d", (*tag).level); > > On Thu, May 20, 2021 at 11:07 AM Trampas Stern <tst...@nc.rr.com > <mailto:tst...@nc.rr.com>> wrote: > > Try changing this: > unsigned char t[3]; > > to this > unsigned char t[4]; > > Then see if your problem goes away.
I have tried to replace t[3] by t[8] before my first post, same result. I have tried also to add a delay after each stty_print() and I obtain the same corruption. > So snprintf() is not well documented... is the size parameter > including null terminator or not? snprintf always contains null terminator. > Even worse is when you get to > multiple threads (ISR handlers) is snprintf() reentrant? > > Generally I write my own snprintf() so that I know what it does and > make sure it is reentrant. This is not as optimized but I know what > the results are. > > You also have lots of other things to consider for example the AVR > is Harvard machine so code like this: > stty_print("Anticol ACF\r\n"); > actually will copy string from flash to RAM and thus use more RAM. I have found a very strange bug. uint8_t iso14443a_anticol(uint8_t wup, rfid_tag_t *tag) { ... (*tag).state = ISO14443A_STATE_ANTICOL_RUNNING; (*tag).level = ISO14443A_LEVEL_CL1; snprintf(t, 3, "%02X", (*tag).level); snprintf(t, 3, "%02X", (*tag).level); snprintf(t, 3, "%02X", (*tag).level); snprintf(t, 3, "%02X", (*tag).level); snprintf(t, 3, "%02X", (*tag).level); snprintf(t, 3, "%02X", (*tag).level); snprintf(t, 3, "%02X", (*tag).level); vfd_print(t); vfd_print(" (0)\r\n"); //vfd_print("1"); vfd_print(" (0)\r\n"); snprintf(t, 3, "%02X", (*tag).level); stty_print(t); stty_print(" (0)\r\n"); snprintf(t, 3, "%02X", (*tag).level); stty_print(t); stty_print(" (0.0)\r\n"); cascade: rx_len = sizeof(sak); snprintf(t, 3, "%02X", (*tag).level); stty_print(t); stty_print(" (0.1)\r\n"); ... This code runs as expected (please note vfd_print(t); vfd_print(" (0)\r\n");). On VFD screen, I obtain "01 (0)". If I replace this line by vfd_print("01 (0)\r\n"); I can see my memory corruption again. If I deplace this line after snprintf(t, 3, "%02X", (*tag).level); stty_print(t); stty_print(" (0.0)\r\n"); program runs as expected. If I remove this line, memory corruption again. My avr-gcc is an old release (5.4 given by debian). I will try a recent gcc. JB