Skip to content

Commit 254062f

Browse files
committed
Add thread safety across reboot error info operations
1 parent 4ec30e3 commit 254062f

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

platform/mbed_error.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ static mbed_error_status_t handle_error(mbed_error_status_t error_status, unsign
168168
//Increment error count
169169
error_count++;
170170

171-
//Capture the fist system error and store it
171+
//Capture the first system error and store it
172172
if (error_count == 1) { //first error
173173
memcpy(&first_error_ctx, &current_error_ctx, sizeof(mbed_error_ctx));
174174
}
@@ -281,7 +281,10 @@ WEAK MBED_NORETURN mbed_error_status_t mbed_error(mbed_error_status_t error_stat
281281
last_error_ctx.is_error_processed = 0;//Set the flag that this is a new error
282282
//Update the struct with crc
283283
last_error_ctx.crc_error_ctx = compute_crc32( (unsigned char *)&last_error_ctx, ((uint32_t)&(last_error_ctx.crc_error_ctx) - (uint32_t)&last_error_ctx) );
284+
//Protect report_error_ctx while we update it
285+
core_util_critical_section_enter();
284286
memcpy(report_error_ctx, &last_error_ctx, sizeof(mbed_error_ctx));
287+
core_util_critical_section_exit();
285288
//We need not call delete_mbed_crc(crc_obj) here as we are going to reset the system anyway, and calling delete while handling a fatal error may cause nested exception
286289
#if MBED_CONF_PLATFORM_FATAL_ERROR_AUTO_REBOOT_ENABLED
287290
system_reset();//do a system reset to get the system rebooted
@@ -310,7 +313,10 @@ mbed_error_status_t mbed_set_error_hook(mbed_error_hook_t error_hook_in)
310313
mbed_error_status_t mbed_reset_reboot_error_info()
311314
{
312315
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
313-
memset(report_error_ctx, 0, sizeof(mbed_error_ctx) );
316+
//Protect for thread safety
317+
core_util_critical_section_enter();
318+
memset(report_error_ctx, 0, sizeof(mbed_error_ctx) );
319+
core_util_critical_section_exit();
314320
#endif
315321
return MBED_SUCCESS;
316322
}
@@ -321,10 +327,12 @@ mbed_error_status_t mbed_reset_reboot_count()
321327
#if MBED_CONF_PLATFORM_CRASH_CAPTURE_ENABLED
322328
if(is_reboot_error_valid) {
323329
uint32_t crc_val = 0;
330+
core_util_critical_section_enter();
324331
report_error_ctx->error_reboot_count = 0;//Set reboot count to 0
325332
//Update CRC
326333
crc_val = compute_crc32( (unsigned char *)report_error_ctx, ((uint32_t)&(report_error_ctx->crc_error_ctx) - (uint32_t)report_error_ctx) );
327334
report_error_ctx->crc_error_ctx = crc_val;
335+
core_util_critical_section_exit();
328336
return MBED_SUCCESS;
329337
}
330338
#endif

0 commit comments

Comments
 (0)