Updated ESP8266 Boards to V3.0.2 crash with sprintf (%10lu)

Hi
I have recently reinstalled my Arduino IDE 1.8.16 (Mac)
It fetched ESP8266 Boards Version3.0.2
My sketch compiled error-free, but crashed on a Wemos D1 mini.

I narrowed down the error to:

getEpoch(); // writes the Epoch (Numbers of seconds till 1.1.1970...
getTimeData(); // breaks down the Epoch into discrete values.
sprintf(charbuff, "Now is %02d:%02d:%02d. The Epoch is: %10lu\r\nDate is %s, %02d %s %04d", Hour, Minute, Second, Epoch, DayName, Day, MonthName, Year);
Console3.println(charbuff);

Without printing the Epoch with %10lu it worked.
I can far remember having read something with lu beeing non-standard ?

Is it a bug or not?
if not, what is the replacement for lu?

Thank you.

We can't see your datatypes.

%10lu is acceptable

%lu is the correct format for unsigned long If that’s what you have
10 would be the field’s width for padding if needed

Feels more like a memory issue.
Do you have enough space in the buffer? Use snprintf() to not overflow

Charbuff is an array of 80 bytes,
Epoch inherits from time_t,
Hour, Minute, Second, Day, Year integers,
Dayname and Monthname arrays of 12 bytes.

The routine runs upon initializing, just after Wifi connection and NTP sync.

My sketch uses 486129 bytes (46%) of program storage space. Maximum is 1044464 bytes.
Global variables use 40304 bytes (49%) of dynamic memory, leaving 41616 bytes for local variables. Maximum is 81920 bytes.
So ii won't run out of memory.

So, if lu is acceptable, it's a bug.
It worked with the ESP8266 lib V2.7.4.
Without this lu part of the sprintf, it works also.
The case should be clear enough?

The comment "We can't see your datatypes" was meant as a wider hint.

Never mind.

Good luck.

Your source code really was the ask . It might crash elsewhere.

Possibly 79 bytes not enough for this

Now is %02d:%02d:%02d. The Epoch is: %10lu\r\nDate is %s, %02d %s %04d

Use snprintf() and see if you still have the crash.

The fact that it crashes depends on what’s in memory after the string buffer which can very depending on how it’s compiled so can work with luck or have silent damage

This compiles and runs fine for me with 3.0.2



void setup() {
 char charbuff[80];
 memset (charbuff, '\0', 80);

 Serial.begin(115200);
 int Hour, Minute, Second,Day, Year;//32 bit integers on esp8266
 long int Epoch; //64 bit for extended epoch
 Hour = 12;
 Minute = 30;
 Second = 45;
 Epoch = 1234567890;
 Day =1;
 Year = 2022;
 char DayName[] = "Sunday";
 char MonthName[] = "May";
 
 delay(5000);
 sprintf(charbuff, "Now is %02d:%02d:%02d. The Epoch is: %10lu\r\nDate is %s, %02d %s %04d", Hour, Minute, Second, Epoch, DayName, Day, MonthName, Year);
 Serial.println(strlen(charbuff));
 Serial.println(charbuff);
}
void loop() {
  // put your main code here, to run repeatedly:

}
11:06:56.772 -> 70
11:06:56.772 -> Now is 12:30:45. The Epoch is: 1234567890
11:06:56.817 -> Date is Sunday, 01 May 2022

A long int is also only 32 bits on an ESP32. (Time is better managed with unsigned long)

I am currently updating Github and will provide the whole code soon (quite a lot...)

79 bytes is more than enough, it worked previously.
The output is:
Now is 20:10:03.
[ Epoch = 10 chars for Epoch ]
Date is Sunday, 01 May 2022
so ~just 60 chars.

OK if you say so (I count 26 just for the epoch message)
did not know exactly your text representation (you said 12 bytes for month and days so could be padded there too) but you are the one with the code :wink:

The snippet and the reduced example code works for me with V3.0.2.

I don't think the core version is the root cause of your issue.

Let me update Github first, then I will test with
static char charbuff[120]; instead of
static char charbuff[80];

One problem with C/C++ is that issues can crop up quite a way away from where the problem appears to be, or where you might think it is.

It's a common noob misunderstanding

I defined Epoch from time_t so not dependent on the guts of the ESP

time_t     Epoch;

and defined charbuf that way:

static char charbuff[80]; 

It might be indeed good enough for English

Hour = 12;
 Minute = 30;
 Second = 45;
 Epoch = 0xFFFFFFFF;
 Day =1;
 Year = 2022;
 char DayName[] = "Wednesday";
 char MonthName[] = "September";
 

Would give 79 characters if I counted correctly - so you can add the trailing null.

Bug might be somewhere else then

time_t Epoch;

The sprintf specifier of %10lu, is not happy with the time_t type.

warning: format '%lu' expects argument of type 'long unsigned int', but argument 6 has type 'time_t' {aka 'long long int'} [-Wformat=]

That could be it.
I don't get that warning.
What would the correct printf format be?
Does %llu exist?

Does %llu exist?

Yes. Indeed it accepts the time_t declaration of Epoch with no warnings.

%10llu

1 Like

Four minutes says "knee jerk" rather than "tried it"

Here is the full code:
Booster for sound pressure level meters

it is intended to be max legible for noobs and has a lot of parametrization abilities