Skip to content

Commit 194ea94

Browse files
Use localtime_r for multicore safety (#3169)
The libraries should use multicore safe localtime_r, and update the examples to use it too, since we have 2 cores which could possibly call the unsafe one at the same time.
1 parent 6c7a0b0 commit 194ea94

File tree

6 files changed

+30
-23
lines changed

6 files changed

+30
-23
lines changed

libraries/FatFS/src/FatFS.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -294,17 +294,18 @@ DWORD get_fattime() {
294294
} else {
295295
now = time(nullptr);
296296
}
297-
struct tm *stm = localtime(&now);
298-
if (stm->tm_year < 80) {
297+
struct tm stm;
298+
localtime_r(&now, &stm);
299+
if (stm.tm_year < 80) {
299300
// FAT can't report years before 1980
300-
stm->tm_year = 80;
301+
stm.tm_year = 80;
301302
}
302-
return (DWORD)(stm->tm_year - 80) << 25 |
303-
(DWORD)(stm->tm_mon + 1) << 21 |
304-
(DWORD)stm->tm_mday << 16 |
305-
(DWORD)stm->tm_hour << 11 |
306-
(DWORD)stm->tm_min << 5 |
307-
(DWORD)stm->tm_sec >> 1;
303+
return (DWORD)(stm.tm_year - 80) << 25 |
304+
(DWORD)(stm.tm_mon + 1) << 21 |
305+
(DWORD)stm.tm_mday << 16 |
306+
(DWORD)stm.tm_hour << 11 |
307+
(DWORD)stm.tm_min << 5 |
308+
(DWORD)stm.tm_sec >> 1;
308309
}
309310

310311
}

libraries/FatFSUSB/examples/Listfiles-USB/Listfiles-USB.ino

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,8 +90,9 @@ void printDirectory(String dirName, int numTabs) {
9090
Serial.print("\t\t");
9191
Serial.print(dir.fileSize(), DEC);
9292
time_t cr = dir.fileCreationTime();
93-
struct tm* tmstruct = localtime(&cr);
94-
Serial.printf("\t%d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
93+
struct tm tmstruct;
94+
localtime_r(&cr, &tmstruct);
95+
Serial.printf("\t%d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
9596
}
9697
}
9798
}

libraries/SD/examples/CardInfo/CardInfo.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -180,10 +180,11 @@ void printDirectory(File dir, int numTabs) {
180180
Serial.print(entry.size(), DEC);
181181
time_t cr = entry.getCreationTime();
182182
time_t lw = entry.getLastWrite();
183-
struct tm* tmstruct = localtime(&cr);
184-
Serial.printf("\tCREATION: %d-%02d-%02d %02d:%02d:%02d", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
185-
tmstruct = localtime(&lw);
186-
Serial.printf("\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
183+
struct tm tmstruct;
184+
localtime_r(&cr, &tmstruct);
185+
Serial.printf("\tCREATION: %d-%02d-%02d %02d:%02d:%02d", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
186+
localtime_r(&lw, &tmstruct);
187+
Serial.printf("\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
187188
}
188189
entry.close();
189190
}

libraries/SD/examples/listfiles/listfiles.ino

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,11 @@ void printDirectory(File dir, int numTabs) {
123123
Serial.print(entry.size(), DEC);
124124
time_t cr = entry.getCreationTime();
125125
time_t lw = entry.getLastWrite();
126-
struct tm* tmstruct = localtime(&cr);
127-
Serial.printf("\tCREATION: %d-%02d-%02d %02d:%02d:%02d", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
128-
tmstruct = localtime(&lw);
129-
Serial.printf("\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct->tm_year) + 1900, (tmstruct->tm_mon) + 1, tmstruct->tm_mday, tmstruct->tm_hour, tmstruct->tm_min, tmstruct->tm_sec);
126+
struct tm tmstruct;
127+
localtime_r(&cr, &tmstruct);
128+
Serial.printf("\tCREATION: %d-%02d-%02d %02d:%02d:%02d", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
129+
localtime_r(&lw, &tmstruct);
130+
Serial.printf("\tLAST WRITE: %d-%02d-%02d %02d:%02d:%02d\n", (tmstruct.tm_year) + 1900, (tmstruct.tm_mon) + 1, tmstruct.tm_mday, tmstruct.tm_hour, tmstruct.tm_min, tmstruct.tm_sec);
130131
}
131132
entry.close();
132133
}

libraries/SDFS/src/SDFS.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,9 +242,10 @@ class SDFSImpl : public FSImpl {
242242
} else {
243243
now = time(nullptr);
244244
}
245-
struct tm *tiempo = localtime(&now);
246-
*dosYear = ((tiempo->tm_year - 80) << 9) | ((tiempo->tm_mon + 1) << 5) | tiempo->tm_mday;
247-
*dosTime = (tiempo->tm_hour << 11) | (tiempo->tm_min << 5) | tiempo->tm_sec;
245+
struct tm tiempo;
246+
localtime_r(&now, &tiempo);
247+
*dosYear = ((tiempo.tm_year - 80) << 9) | ((tiempo.tm_mon + 1) << 5) | tiempo.tm_mday;
248+
*dosTime = (tiempo.tm_hour << 11) | (tiempo.tm_min << 5) | tiempo.tm_sec;
248249
}
249250

250251
protected:

libraries/rp2040/examples/Time/Time.ino

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ void loop() {
2121
char buff[80];
2222

2323
time(&now);
24-
strftime(buff, sizeof(buff), "%c", localtime(&now));
24+
struct tm tmstruct;
25+
localtime_r(&now, &tmstruct);
26+
strftime(buff, sizeof(buff), "%c", &tmstruct);
2527
Serial.println(buff);
2628
delay(1000);
2729
}

0 commit comments

Comments
 (0)