address some violations of MISRA C Rule 20.7
cover address some violations of MISRA C Rule 20.7
Hi all,
this series aims to refactor some macros that cause violations of MISRA C Rule 20.7 ("Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses"). All the macros touched by these patches are in some way involved in violations, and the strategy adopted to bring them into compliance is to add parentheses around macro arguments where needed.
Given that the community has previously requested a deviation from the rule, as stated in docs/misra/deviations.rst, and reported below for convenience [1], some macro parameters do not need any adjusting (e.g., when used as lhs to an assignment in statement expressions).
Note that the patch on list.h of the earlier series [2] has been intentionally left out of this series, because it still needs some adjustments.
[1] - Code violating Rule 20.7 is safe when macro parameters are used:
(1) as function arguments;
(2) as macro arguments;
(3) as array indices;
(4) as lhs in assignments.[2] https://lore.kernel.org/xen-devel/[email protected]/
Nicola Vetrini (10):
x86/cpufeature: add parentheses to comply with Rule 20.7 AMD/IOMMU: guest: address violations of MISRA C Rule 20.7 xen/xsm: add parentheses to comply with MISRA C Rule 20.7 xen/device_tree: address violations of MISRA C Rule 20.7
EFI: address violations of MISRA C Rule 20.7 xen/arm: smmu: address violations of MISRA C Rule 20.7 xen/efi: efibind: address violations of MISRA C Rule 20.7 xen/notifier: address violations of MISRA C Rule 20.7 xen/wait: address violations of MISRA C Rule 20.7 xen/sched: address violations of MISRA C Rule 20.7
xen/arch/arm/include/asm/arm64/efibind.h | 4 ++-- xen/arch/x86/include/asm/cpufeatureset.h | 2 +- xen/arch/x86/include/asm/x86_64/efibind.h | 4 ++-- xen/common/sched/private.h | 2 +- xen/drivers/passthrough/amd/iommu_guest.c | 2 +- xen/drivers/passthrough/arm/smmu.c | 2 +- xen/include/efi/efierr.h | 2 +- xen/include/xen/device_tree.h | 2 +- xen/include/xen/notifier.h | 2 +- xen/include/xen/wait.h | 4 ++-- xen/include/xsm/dummy.h | 4 ++-- 11 files changed, 15 insertions(+), 15 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/10 x86/cpufeature: add parentheses to comply with Rule 20.7
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
--- xen/arch/x86/include/asm/cpufeatureset.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/arch/x86/include/asm/cpufeatureset.h +1 -1
@@ -5,7 +5,7 @@#include <xen/stringify.h> -#define XEN_CPUFEATURE (name, value) X86_FEATURE_##name = value, +#define XEN_CPUFEATURE (name, value) X86_FEATURE_##name = (value), enum { #include <public/arch-x86/cpufeatureset.h>#include <asm/cpufeatures.h>
Signature · 1 line
2.34.1
Re: [XEN PATCH 01/10] x86/cpufeature: add parentheses to comply with Rule 20.7
Quoted 9 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
PATCH 2/10 AMD/IOMMU: guest: address violations of MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
---
This local helper may disappear as a follow-up to [1], if the function that remains there after the cleanup ends up being removed, so this patch has a slight dependency on what follows up from that thread (in which case it can be dropped).
--- xen/drivers/passthrough/amd/iommu_guest.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/passthrough/amd/iommu_guest.c +1 -1
@@ -30,7 +30,7 @@#define GUEST_ADDRESS_SIZE_6_LEVEL 0x2#define HOST_ADDRESS_SIZE_6_LEVEL 0x2 -#define reg_to_u64 (reg) (((uint64_t)reg.hi << 32) | reg.lo ) +#define reg_to_u64 (reg) (((uint64_t)((reg).hi << 32)) | (reg).lo )#define u64_to_reg (reg, val) \ do \ { \
Signature · 1 line
2.34.1
Re: [XEN PATCH 02/10] AMD/IOMMU: guest: address violations of MISRA C Rule 20.7
Quoted 6 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
Quoted 7 lines
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>---
This local helper may disappear as a follow-up to [1], if the function that remains there after the cleanup ends up being removed, so this patch has a slight dependency on what follows up from that thread (in which case it can be dropped).
Indeed let's not add more churn to the file before it disappears.
Jan
PATCH 3/10 xen/xsm: add parentheses to comply with MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
--- xen/include/xsm/dummy.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xsm/dummy.h +2 -2
@@ -58,7 +58,7 @@ void __xsm_action_mismatch_detected(void);#define XSM_DEFAULT_ARG /* */ #define XSM_DEFAULT_VOID void -#define XSM_ASSERT_ACTION (def) xsm_default_t action = def; (void)action +#define XSM_ASSERT_ACTION (def) xsm_default_t action = (def); (void)action#else /* CONFIG_XSM */
@@ -71,7 +71,7 @@ void __xsm_action_mismatch_detected(void);#define XSM_INLINE always_inline#define XSM_DEFAULT_ARG xsm_default_t action,#define XSM_DEFAULT_VOID xsm_default_t action -#define XSM_ASSERT_ACTION (def) LINKER_BUG_ON(def != action) +#define XSM_ASSERT_ACTION (def) LINKER_BUG_ON((def) != action)#endif /* CONFIG_XSM */
Signature · 1 line
2.34.1
Re: [XEN PATCH 03/10] xen/xsm: add parentheses to comply with MISRA C Rule 20.7
Quoted 9 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
PATCH 4/10 xen/device_tree: address violations of MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
---
This patch uniforms the way these macros use parentheses, skipping the already deviated cases of the lhs of an assignment. In principle, all three could have the parentheses on the macro argument in the lhs added.
--- xen/include/xen/device_tree.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/device_tree.h +1 -1
@@ -250,7 +250,7 @@ dt_find_interrupt_controller(const struct dt_device_match *matches); for ( pp = (dn)->properties; (pp) != NULL; pp = (pp)->next )#define dt_for_each_device_node (dt, dn) \ - for ( dn = dt; (dn) != NULL; dn = (dn)->allnext ) + for ( dn = (dt); (dn) != NULL; dn = (dn)->allnext )#define dt_for_each_child_node (dt, dn) \ for ( dn = (dt)->child; (dn) != NULL; dn = (dn)->sibling )
Signature · 1 line
2.34.1
Re: [XEN PATCH 04/10] xen/device_tree: address violations of MISRA C Rule 20.7
Quoted 9 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
PATCH 5/10 EFI: address violations of MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
---
This file is matched by exclude-list.json, but the fix is rather trivial and actually benefits code that is in scope for compliance.
--- xen/include/efi/efierr.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/efi/efierr.h +1 -1
@@ -22,7 +22,7 @@ Revision History#define EFIWARN (a) (a) -#define EFI_ERROR (a) (((INTN) a) < 0) +#define EFI_ERROR (a) (((INTN)(a)) < 0)#define EFI_SUCCESS 0
Signature · 1 line
2.34.1
Re: [XEN PATCH 05/10] EFI: address violations of MISRA C Rule 20.7
Quoted 6 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
Quoted 6 lines
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>---
This file is matched by exclude-list.json, but the fix is rather trivial and actually benefits code that is in scope for compliance.
Hmm, yes, the change is simple enough to not be a big hindrance even if we were to pull in incremental updates from gnu-efi, so
Albeit preferably with ...
xen/include/efi/efierr.h Revision History · line 25
Quoted 8 lines
xen/include/efi/efierr.h +1
-1@@ -22,7 +22,7 @@ Revision History#define EFIWARN (a) (a) -#define EFI_ERROR (a) (((INTN) a) < 0) +#define EFI_ERROR (a) (((INTN)(a)) < 0)
... excess parentheses dropped in exchange:
#define EFI_ERROR(a) ((INTN)(a) < 0)
I may take the liberty of doing so while committing.
Jan
PATCH 6/10 xen/arm: smmu: address violations of MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
--- xen/drivers/passthrough/arm/smmu.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/drivers/passthrough/arm/smmu.c +1 -1
@@ -326,7 +326,7 @@ static struct iommu_group *iommu_group_get(struct device *dev)*/ #define ARM_SMMU_GR0_NS (smmu) \ ((smmu)->base + \ - ((smmu->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \ + (((smmu)->options & ARM_SMMU_OPT_SECURE_CFG_ACCESS) \ ? 0x400 : 0))/* Page table bits */
Signature · 1 line
2.34.1
Re: [XEN PATCH 06/10] xen/arm: smmu: address violations of MISRA C Rule 20.7
Quoted 9 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
PATCH 7/10 xen/efi: efibind: address violations of MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
---
This file is matched by exclude-list.json, but the fix is rather trivial and impacts code that in under the scope of MISRA compliance.
--- xen/arch/arm/include/asm/arm64/efibind.h | 4 ++-- xen/arch/x86/include/asm/x86_64/efibind.h | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-)
xen/arch/arm/include/asm/arm64/efibind.h +2 -2
@@ -22,9 +22,9 @@ Revision History#pragma pack()#endif -#define EFIERR (a) (0x8000000000000000ULL | a) +#define EFIERR (a) (0x8000000000000000ULL | (a))#define EFI_ERROR_MASK 0x8000000000000000ULL -#define EFIERR_OEM (a) (0xc000000000000000ULL | a) +#define EFIERR_OEM (a) (0xc000000000000000ULL | (a))#define BAD_POINTER 0xFBFBFBFBFBFBFBFBULL#define MAX_ADDRESS 0xFFFFFFFFFFFFFFFFULL
xen/arch/x86/include/asm/x86_64/efibind.h +2 -2
@@ -117,9 +117,9 @@ typedef uint64_t UINTN;#endif #endif -#define EFIERR (a) (0x8000000000000000 | a) +#define EFIERR (a) (0x8000000000000000 | (a))#define EFI_ERROR_MASK 0x8000000000000000 -#define EFIERR_OEM (a) (0xc000000000000000 | a) +#define EFIERR_OEM (a) (0xc000000000000000 | (a))#define BAD_POINTER 0xFBFBFBFBFBFBFBFB
Signature · 1 line
2.34.1
Re: [XEN PATCH 07/10] xen/efi: efibind: address violations of MISRA C Rule 20.7
Quoted 6 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
Quoted 6 lines
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>---
This file is matched by exclude-list.json, but the fix is rather trivial and impacts code that in under the scope of MISRA compliance.
On the same basis as the other change:
I wonder though: Where do we draw the line?
Jan
Re: [XEN PATCH 07/10] xen/efi: efibind: address violations of MISRA C Rule 20.7
Quoted 9 lines · Jan Beulich
Quoted 8 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
Quoted 8 lines
Quoted 4 lines
Signed-off-by Nicola Vetrini <[email protected]>---
This file is matched by exclude-list.json, but the fix is rather trivial and impacts code that in under the scope of MISRA compliance.On the same basis as the other change:
Acked-by Jan Beulich <[email protected]>
Thanks.
Quoted 3 lines
I wonder though: Where do we draw the line?
Jan
So far the policy, at least from my side, has been to submit a patch if the change is simple enough, to avoid adding a special case that needs to be documented and maintained. In case concerns were raised, propose a deviation and follow up from there.
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
PATCH 8/10 xen/notifier: address violations of MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
--- xen/include/xen/notifier.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/include/xen/notifier.h +1 -1
@@ -34,7 +34,7 @@ struct notifier_head { };#define NOTIFIER_HEAD (name) \ - struct notifier_head name = { .head = LIST_HEAD_INIT(name.head) } + struct notifier_head name = { .head = LIST_HEAD_INIT((name).head) } void notifier_chain_register(
Signature · 1 line
2.34.1
Re: [XEN PATCH 08/10] xen/notifier: address violations of MISRA C Rule 20.7
Quoted 9 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
PATCH 9/10 xen/wait: address violations of MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
--- xen/include/xen/wait.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
xen/include/xen/wait.h +2 -2
@@ -40,12 +40,12 @@ do { \if ( condition ) \break ; \for ( ; ; ) { \ -prepare_to_wait (&wq); \ +prepare_to_wait (&(wq)); \if ( condition ) \break ; \wait (); \ } \ -finish_wait (&wq); \ +finish_wait (&(wq)); \ }while (0 )/* Private functions. */
Signature · 1 line
2.34.1
Re: [XEN PATCH 09/10] xen/wait: address violations of MISRA C Rule 20.7
Quoted 9 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>
PATCH 10/10 xen/sched: address violations of MISRA C Rule 20.7
In reply to Nicola VetriniMISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
--- xen/common/sched/private.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
xen/common/sched/private.h +1 -1
@@ -540,7 +540,7 @@ static inline void sched_unit_unpause(const struct sched_unit *unit) }#define REGISTER_SCHEDULER (x) static const struct scheduler *x##_entry \ - __used_section(".data.schedulers") = &x + __used_section(".data.schedulers") = &(x)struct cpupool {
Signature · 1 line
2.34.1
Re: [XEN PATCH 10/10] xen/sched: address violations of MISRA C Rule 20.7
Quoted 19 lines · Nicola Vetrini
MISRA C Rule 20.7 states: "Expressions resulting from the expansion of macro parameters shall be enclosed in parentheses". Therefore, some macro definitions should gain additional parentheses to ensure that all current and future users will be safe with respect to expansions that can possibly alter the semantics of the passed-in macro parameter.
No functional change.
Signed-off-by Nicola Vetrini <[email protected]>--- xen/common/sched/private.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)xen/common/sched/private.h +0
-0
xen/common/sched/private.h static inline void sched_unit_unpause(const struct sched_unit *unit) · line 543
Quoted 5 lines
xen/common/sched/private.h +1
-1@@ -540,7 +540,7 @@ static inline void sched_unit_unpause(const struct sched_unit *unit) }#define REGISTER_SCHEDULER (x) static const struct scheduler *x##_entry \ - __used_section(".data.schedulers") = &x + __used_section(".data.schedulers") = &(x)
Arguably this is safe, because any x which would be problematic in this line wouldn't compile in the line above.
But it's almost certainly not worth the effort of documenting or deviating, so: