Hello,
In the file lib/xlat_tables/xlat_tables_common.c and other associated files, there are instances where if...else if constructs lack an else statement, resulting in violations during the Coverity MISRA-C analysis for the ZynqMP platform. Addressing this issue added empty else statement to resolve the issue but it is related to core translational table logic function. Is it possible to address this issue? Please provide your suggestions.
Regards, Nithin G
In the file lib/xlat_tables/xlat_tables_common.c and other files, getting misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. during the Coverity MISRA-C analysis. if() { } else if () { } Resolution: added empty else statement to resolve the issue. if() { } else if () { } else { /* added empty else */ } Is it possible to add an empty else statement in open source? Please suggest.
Regards, Nithin
Hi Nithin,
lib/xlat_tables/ corresponds to version 1 of the translation table library in TF-A, which is deprecated. As such, it does not get any update - other than bug / security fixes. The library will be removed in a future version of TF-A (possibly in 3.0 next year).
I strongly recommend you migrate to version 2 of the library (under lib/xlat_tables_v2/ directory). Besides, it has no violation to MISRA rule 15.7 AFAICS, according to ECLAIR tool (which is TF-A OpenCI tool for MISRA compliance checks).
Did you spot any violation to rule 15.7 in version 2 of the translation table library on your side?
Best regards, Sandrine
On 12/8/23 11:00, Nithin G via TF-A wrote:
In the file lib/xlat_tables/xlat_tables_common.c and other files, getting misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. during the Coverity MISRA-C analysis. if() { } else if () { } Resolution: added empty else statement to resolve the issue. if() { } else if () { } else { /* added empty else */ } Is it possible to add an empty else statement in open source? Please suggest.
Regards, Nithin
Hi Sandrine,
Thanks for responding.
Getting the same violation for the file /lib/el3_runtime/aarch64/context_mgmt.c also.
misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if ((scr_el3 & SCR_HCE_BIT) != 0U) {
Resolution: added empty else statement to resolve the issue.
diff --git a/lib/el3_runtime/aarch64/context_mgmt.c b/lib/el3_runtime/aarch64/context_mgmt.c index 8e6bfc5..8996746 100644 --- a/lib/el3_runtime/aarch64/context_mgmt.c +++ b/lib/el3_runtime/aarch64/context_mgmt.c @@ -789,6 +789,8 @@ void cm_prepare_el3_exit(uint32_t security_state) */ write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT)); + } else { + /* To fix the MISRA 15.7 warning - Added an empty else statement */ } manage_extensions_nonsecure(el2_unused, ctx); }
Hi,
Adding to the above misra_c_2012_rule_15_7_violation. getting same violation for this file /plat/common/aarch64/plat_common.c:
const char *get_el_str(unsigned int el) { (1) Event misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if (el == MODE_EL3) { return "EL3"; } else if (el == MODE_EL2) { return "EL2"; } return "EL1"; }
Resolution: added empty else statement to resolve the issue.
const char *get_el_str(unsigned int el) { if (el == MODE_EL3) { return "EL3"; } else if (el == MODE_EL2) { return "EL2"; } else { } return "EL1"; }
Hi,
Adding to the above misra_c_2012_rule_15_7_violation. getting same violation for this file /plat/common/aarch64/plat_common.c:
const char *get_el_str(unsigned int el) { (1) Event misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if (el == MODE_EL3) { return "EL3"; } else if (el == MODE_EL2) { return "EL2"; } return "EL1"; }
Resolution: added empty else statement to resolve the issue.
const char *get_el_str(unsigned int el) { if (el == MODE_EL3) { return "EL3"; } else if (el == MODE_EL2) { return "EL2"; } else { /* To fix the MISRA 15.7 warning - Added an empty else statement */ } return "EL1"; }
Please suggest? Is it possible to add an empty else statement.
Regards, Nithin G
Hi
Rather than adding an empty else (which seems quite artificial), wouldn't putting return "EL1"; into an else be more in keeping with the structure of the code?
Thanks
Tom ________________________________ From: Nithin G via TF-A tf-a@lists.trustedfirmware.org Sent: 16 January 2024 09:17 To: tf-a@lists.trustedfirmware.org tf-a@lists.trustedfirmware.org Subject: [TF-A] Re: All if...else if constructs shall be terminated with an else statement.
Hi,
Adding to the above misra_c_2012_rule_15_7_violation. getting same violation for this file /plat/common/aarch64/plat_common.c:
const char *get_el_str(unsigned int el) { (1) Event misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if (el == MODE_EL3) { return "EL3"; } else if (el == MODE_EL2) { return "EL2"; } return "EL1"; }
Resolution: added empty else statement to resolve the issue.
const char *get_el_str(unsigned int el) { if (el == MODE_EL3) { return "EL3"; } else if (el == MODE_EL2) { return "EL2"; } else { } return "EL1"; } -- TF-A mailing list -- tf-a@lists.trustedfirmware.org To unsubscribe send an email to tf-a-leave@lists.trustedfirmware.org
Hi Tom,
Thank you for your reply.
Yes, I will put return "EL1" into an else statement to enhance the code structure. But we have a situation where the return statement is not present. What can we do with the below code to fix this violation?
Event misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if() {
} else if () {
}
Regards, Nithin G
Hi,
On 1/17/24 10:24, Nithin G via TF-A wrote:
Yes, I will put return "EL1" into an else statement to enhance the code structure. But we have a situation where the return statement is not present. What can we do with the below code to fix this violation?
Event misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if() {
} else if () {
}
I don't understand... If we massage the code like this:
const char *get_el_str(unsigned int el) { if (el == MODE_EL3) { return "EL3"; } else if (el == MODE_EL2) { return "EL2"; } else { return "EL1"; } }
where do you a situation where the return statement is not present? I see return statements in all cases: EL3, EL2 and anything else.
Regards, Sandrine
Hi Sandrine,
Thanks for responding.
I will put return "EL1" into an else statement here in this file, but I am getting in the file xlat_tables_common.c also No non-empty terminating "else" statement where I have to add empty else. Below attached the code, please suggest in this situation.
misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if (mm->base_va > (base_va + level_size - 1U)) { /* Next region is after this area. Nothing to map yet */ desc = INVALID_DESC; /* Make sure that the current level allows block descriptors */ } else if (level >= XLAT_BLOCK_LEVEL_MIN) { /* * Try to get attributes of this area. It will fail if * there are partially overlapping regions. On success, * it will return the innermost region's attributes. */ unsigned int attr; unsigned int r = mmap_region_attr(mm, base_va, level_size, &attr); if (r == 0U) { desc = mmap_desc(attr, base_va - mm->base_va + mm->base_pa, level); } }
Regards, Nithin
Hi Nithin,
On 1/17/24 11:59, Nithin G via TF-A wrote:
Hi Sandrine,
Thanks for responding.
I will put return "EL1" into an else statement here in this file, but I am getting in the file xlat_tables_common.c also No non-empty terminating "else" statement where I have to add empty else. Below attached the code, please suggest in this situation.
misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if (mm->base_va > (base_va + level_size - 1U)) { /* Next region is after this area. Nothing to map yet */ desc = INVALID_DESC; /* Make sure that the current level allows block descriptors */ } else if (level >= XLAT_BLOCK_LEVEL_MIN) { /* * Try to get attributes of this area. It will fail if * there are partially overlapping regions. On success, * it will return the innermost region's attributes. */ unsigned int attr; unsigned int r = mmap_region_attr(mm, base_va, level_size, &attr); if (r == 0U) { desc = mmap_desc(attr, base_va - mm->base_va + mm->base_pa, level); } }
At the risk of repeating myself, xlat_tables_common.c is part of the version 1 of the translation tables library, which is no longer maintained. Thus, we don't plan to make any change to its code, beyond bug / security fixes.
Regards, Sandrine
Hello
Thanks for your reply.
But we have a situation in the file /lib/el3_runtime/aarch64/context_mgmt.c. Getting the same violation MISRA 15.7. What can we do with the below code to fix this violation?
void cm_prepare_el3_exit(uint32_t security_state) { u_register_t sctlr_elx, scr_el3, mdcr_el2;
if (security_state == NON_SECURE) { scr_el3 = read_ctx_reg(get_el3state_ctx(ctx), CTX_SCR_EL3); misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if ((scr_el3 & SCR_HCE_BIT) != 0U) { ... ... ... write_sctlr_el2(sctlr_elx); } else if (el_implemented(2) != EL_IMPL_NONE) { el2_unused = true; ... ... ... write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT)); } manage_extensions_nonsecure(el2_unused, ctx); }
cm_el1_sysregs_context_restore(security_state); cm_set_next_eret_context(security_state); }
Resolution: added empty else statement to resolve the issue.
diff --git a/lib/el3_runtime/aarch64/context_mgmt.c b/lib/el3_runtime/aarch64/context_mgmt.c index 8e6bfc5..8996746 100644 --- a/lib/el3_runtime/aarch64/context_mgmt.c +++ b/lib/el3_runtime/aarch64/context_mgmt.c @@ -789,6 +789,8 @@ void cm_prepare_el3_exit(uint32_t security_state) */ write_cnthp_ctl_el2(CNTHP_CTL_RESET_VAL & ~(CNTHP_CTL_ENABLE_BIT)); + } else { + /* To fix the MISRA 15.7 warning - Added an empty else statement */ } manage_extensions_nonsecure(el2_unused, ctx); }
Hi Sandrine,
we understand from below conversation , apart from bugs / security fixes any other change won't be done to xlat table v1 library code. but we see many MISRA issues getting reported in "Required" category for xlat tables v1 . can these "required/mandatory" category issues be considered in "bug" category and taken up for fixup in upstream ?
Regards Amit
-----Original Message----- From: Sandrine Bailleux via TF-A tf-a@lists.trustedfirmware.org Sent: Thursday, January 18, 2024 8:43 PM To: Nithin G nithing1999@gmail.com; tf-a@lists.trustedfirmware.org Subject: [TF-A] Re: All if...else if constructs shall be terminated with an else statement.
Hi Nithin,
On 1/17/24 11:59, Nithin G via TF-A wrote:
Hi Sandrine,
Thanks for responding.
I will put return "EL1" into an else statement here in this file, but I am getting in the file xlat_tables_common.c also No non-empty terminating "else" statement where I have to add empty else. Below attached the code, please suggest in this situation.
misra_c_2012_rule_15_7_violation: No non-empty terminating "else" statement. if (mm->base_va > (base_va + level_size - 1U)) { /* Next region is after this area. Nothing to map yet */ desc = INVALID_DESC; /* Make sure that the current level allows block descriptors */ } else if (level >= XLAT_BLOCK_LEVEL_MIN) { /* * Try to get attributes of this area. It will fail if * there are partially overlapping regions. On success, * it will return the innermost region's attributes. */ unsigned int attr; unsigned int r = mmap_region_attr(mm, base_va, level_size, &attr); if (r == 0U) { desc = mmap_desc(attr, base_va - mm->base_va + mm->base_pa, level); } }
At the risk of repeating myself, xlat_tables_common.c is part of the version 1 of the translation tables library, which is no longer maintained. Thus, we don't plan to make any change to its code, beyond bug / security fixes.
Regards, Sandrine -- TF-A mailing list -- tf-a@lists.trustedfirmware.org To unsubscribe send an email to tf-a-leave@lists.trustedfirmware.org
tf-a@lists.trustedfirmware.org