Skip to content

Commit b44e6f8

Browse files
committed
Assert that file IO is not used in ISRs
Trigger an assert if a file is read from or written to from an interrupt handler or critical section. This includes using printf from an interrupt handler or critical section. This makes failures due to use in incorrect context deterministic and easier to locate. This feature is enabled by defining MBED_TRAP_ERRORS_ENABLED to 1 or by using the debug profile.
1 parent 5ab3de0 commit b44e6f8

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

platform/mbed_retarget.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
#include "platform/PlatformMutex.h"
2424
#include "platform/mbed_error.h"
2525
#include "platform/mbed_stats.h"
26+
#include "platform/mbed_critical.h"
2627
#if MBED_CONF_FILESYSTEM_PRESENT
2728
#include "filesystem/FileSystem.h"
2829
#include "filesystem/File.h"
@@ -309,6 +310,12 @@ extern "C" int PREFIX(_write)(FILEHANDLE fh, const unsigned char *buffer, unsign
309310
#endif
310311
int n; // n is the number of bytes written
311312

313+
#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED
314+
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
315+
error("Error - writing to a file in an ISR or critical section\r\n");
316+
}
317+
#endif
318+
312319
if (fh < 3) {
313320
#if DEVICE_SERIAL
314321
if (!stdio_uart_inited) init_serial();
@@ -353,6 +360,12 @@ extern "C" int PREFIX(_read)(FILEHANDLE fh, unsigned char *buffer, unsigned int
353360
#endif
354361
int n; // n is the number of bytes read
355362

363+
#if defined(MBED_TRAP_ERRORS_ENABLED) && MBED_TRAP_ERRORS_ENABLED
364+
if (core_util_is_isr_active() || !core_util_are_interrupts_enabled()) {
365+
error("Error - reading from a file in an ISR or critical section\r\n");
366+
}
367+
#endif
368+
356369
if (fh < 3) {
357370
// only read a character at a time from stdin
358371
#if DEVICE_SERIAL

0 commit comments

Comments
 (0)