[PATCH] virtual_region: address Misra C:2012 rule 18.3

Dmytro Prokopchuk1 posted 1 patch 1 month, 2 weeks ago
xen/common/virtual_region.c | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
[PATCH] virtual_region: address Misra C:2012 rule 18.3
Posted by Dmytro Prokopchuk1 1 month, 2 weeks ago
find_text_region() tests whether an arbitrary address lies inside
[text_start, text_end). The address and those bounds are not known to
point into one object, so relational operators on the pointers are
undefined behaviour and violate Misra C:2012 rule 18.3.

The bounds are cast to unsigned long because addr already has that
type. The type is wide enough to hold a pointer, so the comparison
result stays the same.

No functional changes.

Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>
---
A deviation does not fit because find_text_region() compares an arbitrary address
with bounds (as was proposed here:
https://patchew.org/Xen/901917945f704643e95842a773be9e8676f76d10.1755690681.git.dmytro._5Fprokopchuk1@epam.com/).

Casting resolve this MISRA violation.
---
 xen/common/virtual_region.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/xen/common/virtual_region.c b/xen/common/virtual_region.c
index 1dc2e9f592..a30e6b9c30 100644
--- a/xen/common/virtual_region.c
+++ b/xen/common/virtual_region.c
@@ -83,8 +83,8 @@ const struct virtual_region *find_text_region(unsigned long addr)
     rcu_read_lock(&rcu_virtual_region_lock);
     list_for_each_entry_rcu ( iter, &virtual_region_list, list )
     {
-        if ( (void *)addr >= iter->text_start &&
-             (void *)addr <  iter->text_end )
+        if ( addr >= (unsigned long)iter->text_start &&
+             addr < (unsigned long)iter->text_end )
         {
             region = iter;
             break;
-- 
2.43.0
Re: [PATCH] virtual_region: address Misra C:2012 rule 18.3
Posted by Jan Beulich 1 month, 2 weeks ago
On 08.10.2026 13:52, Dmytro Prokopchuk1 wrote:
> find_text_region() tests whether an arbitrary address lies inside
> [text_start, text_end). The address and those bounds are not known to
> point into one object, so relational operators on the pointers are
> undefined behaviour and violate Misra C:2012 rule 18.3.
> 
> The bounds are cast to unsigned long because addr already has that
> type. The type is wide enough to hold a pointer, so the comparison
> result stays the same.
> 
> No functional changes.
> 
> Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>

Reviewed-by: Jan Beulich <jbeulich@suse.com>
Re: [PATCH] virtual_region: address Misra C:2012 rule 18.3
Posted by Andrew Cooper 1 month, 2 weeks ago
On 08/10/2026 12:52 pm, Dmytro Prokopchuk1 wrote:
> find_text_region() tests whether an arbitrary address lies inside
> [text_start, text_end). The address and those bounds are not known to
> point into one object, so relational operators on the pointers are
> undefined behaviour and violate Misra C:2012 rule 18.3.
>
> The bounds are cast to unsigned long because addr already has that
> type. The type is wide enough to hold a pointer, so the comparison
> result stays the same.
>
> No functional changes.
>
> Signed-off-by: Dmytro Prokopchuk <dmytro_prokopchuk1@epam.com>
> ---
> A deviation does not fit because find_text_region() compares an arbitrary address
> with bounds (as was proposed here:
> https://patchew.org/Xen/901917945f704643e95842a773be9e8676f76d10.1755690681.git.dmytro._5Fprokopchuk1@epam.com/).
>
> Casting resolve this MISRA violation.
> ---
>  xen/common/virtual_region.c | 4 ++--
>  1 file changed, 2 insertions(+), 2 deletions(-)
>
> diff --git a/xen/common/virtual_region.c b/xen/common/virtual_region.c
> index 1dc2e9f592..a30e6b9c30 100644
> --- a/xen/common/virtual_region.c
> +++ b/xen/common/virtual_region.c
> @@ -83,8 +83,8 @@ const struct virtual_region *find_text_region(unsigned long addr)
>      rcu_read_lock(&rcu_virtual_region_lock);
>      list_for_each_entry_rcu ( iter, &virtual_region_list, list )
>      {
> -        if ( (void *)addr >= iter->text_start &&
> -             (void *)addr <  iter->text_end )
> +        if ( addr >= (unsigned long)iter->text_start &&
> +             addr < (unsigned long)iter->text_end )


Please don't break the tabulation.   It's important for legibility.

~Andrew