2626
2727#define MEMFAULT_REBOOT_INFO_VERSION 2
2828
29+ // clang-format off
30+ // Internal value used to initialize sMfltRebootInfo.last_reboot_reason. Care must be taken when
31+ // comparing this value to eMemfaultRebootReason as different platforms produce different behavior
32+ // comparing uint32_t to enum types. In general, cast this value to the type it is compared against.
33+ // Examples:
34+ //
35+ // uint32_t last_reboot_reason != MEMFAULT_REBOOT_REASON_NOT_SET (GOOD)
36+ // eMemfaultRebootReason reboot_reason != (eMemfaultRebootReason)MEMFAULT_REBOOT_REASON_NOT_SET (GOOD)
37+ // uint32_t last_reboot_reason != (eMemfaultRebootReason)MEMFAULT_REBOOT_REASON_NOT_SET (BAD)
38+ // clang-format on
2939#define MEMFAULT_REBOOT_REASON_NOT_SET 0xffffffff
3040
3141typedef MEMFAULT_PACKED_STRUCT MfltRebootInfo {
@@ -56,6 +66,10 @@ sMfltRebootInfo;
5666MEMFAULT_STATIC_ASSERT (sizeof (sMfltRebootInfo ) == MEMFAULT_REBOOT_TRACKING_REGION_SIZE ,
5767 "struct doesn't match expected size" );
5868
69+ MEMFAULT_STATIC_ASSERT (sizeof (eMemfaultRebootReason ) <=
70+ sizeof (((sMfltRebootInfo ){ 0 }).last_reboot_reason ),
71+ "enum does not fit within sMfltRebootInfo.last_reboot_reason" );
72+
5973static sMfltRebootInfo * s_mflt_reboot_info ;
6074
6175//! Struct to retrieve reboot reason data from. Matches the fields of sMfltRebootReason
@@ -90,6 +104,7 @@ static bool prv_check_or_init_struct(void) {
90104}
91105
92106static bool prv_read_reset_info (sMfltResetReasonInfo * info ) {
107+ // prior_stored_reason is a uint32_t, no need to cast MEMFAULT_REBOOT_REASON_NOT_SET
93108 if ((s_mflt_reboot_info -> last_reboot_reason == MEMFAULT_REBOOT_REASON_NOT_SET ) &&
94109 (s_mflt_reboot_info -> reset_reason_reg0 == 0 )) {
95110 return false; // no reset crashes!
@@ -118,7 +133,8 @@ static void prv_record_reboot_reason(eMemfaultRebootReason reboot_reg_reason,
118133 uint32_t prior_stored_reason ) {
119134 s_reboot_reason_data .reboot_reg_reason = reboot_reg_reason ;
120135
121- if (prior_stored_reason != (eMemfaultRebootReason )MEMFAULT_REBOOT_REASON_NOT_SET ) {
136+ // prior_stored_reason is a uint32_t, no need to cast MEMFAULT_REBOOT_REASON_NOT_SET
137+ if (prior_stored_reason != MEMFAULT_REBOOT_REASON_NOT_SET ) {
122138 s_reboot_reason_data .prior_stored_reason = (eMemfaultRebootReason )prior_stored_reason ;
123139 } else {
124140 s_reboot_reason_data .prior_stored_reason = reboot_reg_reason ;
@@ -131,6 +147,9 @@ static bool prv_get_unexpected_reboot_occurred(void) {
131147 // Use prior_stored_reason as source of reboot reason. Fallback to reboot_reg_reason if
132148 // prior_stored_reason is not set
133149 eMemfaultRebootReason reboot_reason ;
150+
151+ // s_reboot_reason_data.prior_stored_reason is an eMemfaultRebootReason type, cast
152+ // MEMFAULT_REBOOT_REASON_NOT_SET
134153 if (s_reboot_reason_data .prior_stored_reason !=
135154 (eMemfaultRebootReason )MEMFAULT_REBOOT_REASON_NOT_SET ) {
136155 reboot_reason = s_reboot_reason_data .prior_stored_reason ;
@@ -151,6 +170,7 @@ static void prv_record_reboot_event(eMemfaultRebootReason reboot_reason,
151170 // s_mflt_reboot_info can be cleared by any call to memfault_reboot_tracking_collect_reset_info
152171 prv_record_reboot_reason (reboot_reason , s_mflt_reboot_info -> last_reboot_reason );
153172
173+ // last_reboot_reason is a uint32_t, no need to cast MEMFAULT_REBOOT_REASON_NOT_SET
154174 if (s_mflt_reboot_info -> last_reboot_reason != MEMFAULT_REBOOT_REASON_NOT_SET ) {
155175 // we are already tracking a reboot. We don't overwrite this because generally the first reboot
156176 // in a loop reveals what started the crash loop
0 commit comments