token-vesting icon indicating copy to clipboard operation
token-vesting copied to clipboard

sprintf problems with serial.print/corrupted memory

Open vslinuxdotnet opened this issue 3 years ago • 6 comments

I have some problems with the sprintf() with serial.print to print the correct variable, seams that some kind of memory corrupted. When I change to snprintf(), all problems was solved! The device used is ATtiny1614.

Don't print 99:

    char s[3];
    sprintf(s,"%d",99);
    Serial.println(s);

Print 99:

    char s[3];
    snprintf(s,2,"%d",99);
    Serial.println(s);

Regards,

VS

vslinuxdotnet avatar Apr 19 '22 08:04 vslinuxdotnet

I'm wondering if this is considered correct behavior. I've fallen out of my chair when i saw some things that were "undefined behavior" in C.

Basically, does this represent a regression from the behavior of the classic core which is inconsistent with standard implementations, or is all of arduino like this?

SpenceKonde avatar Apr 19 '22 09:04 SpenceKonde

I don't think that is the correct behavior, because in the atmega328p it works fine. So may the sprintf() have the snprintf() in the main function ? I tested in arduino IDE in Linux v1.8.18 and v1.8.15.

vslinuxdotnet avatar Apr 19 '22 09:04 vslinuxdotnet

Hmm, yea, I just reproduced on the 1614.

Does it work on megacorex? That's where I still most of the prinf stujff

To be clear, I never use these functions myself - I have trouble keeping them from failing catastrophically.

SpenceKonde avatar Apr 19 '22 10:04 SpenceKonde

almost certainly impacts dxcore too. Begging anyopne who knows how this works for help

SpenceKonde avatar May 01 '22 00:05 SpenceKonde

Adding blocked tag because I have not the faintest idea what's going on under the hood :-(

SpenceKonde avatar May 02 '22 13:05 SpenceKonde

tested right now with attiny1614 and core 2.5.10 and 2.5.11. I was not able to reproduce the error. in fact, snprintf only printed one 9 (the second char being \0 and removed in the print function). Also tested on AVR128DA48 on latest DxCore. Not reproduced either. I'm using an oscilloscope to check the output. Also tested on 9600 baud and 115200 baud.

testCode:

void setup() {
  Serial.begin(9600);
  char s[3];
   sprintf(s,"%d",99);
   //snprintf(s,2,"%d",99);
   Serial.println(s);

   delay(500);
   CPU_CCP = 0xD8;
   RSTCTRL.SWRR = 1;

}

void loop() {}

MX682X avatar Jun 22 '22 18:06 MX682X

I gave this a try and my results agree with @MX682X's - everything works as expected and snprintf generates 9. I tested a 3226 and a 1617, and on the 1617, I tried all three compile time printf options.

If anyone wants to share complete test code that fails for them, I'd be happy to give it a try.

The code I used:

void setup() {
  Serial.begin(115200); 
  delay(2000);
  Serial.println("-- begin --");
}

void loop() {
  char s[3];
  sprintf(s, "%d", 99);
  Serial.println(s);        // prints "99"

  char t[3];
  snprintf(t, 2, "%d", 99);
  Serial.println(t);        // prints "9"

  delay(500);
}

jvasileff avatar Sep 06 '22 17:09 jvasileff

Thanks. Sounds like there isn't actually a problem here.

SpenceKonde avatar Sep 08 '22 10:09 SpenceKonde