Refactor arm64/domctl.c 'subarch_do_domctl' to avoid unreachable break.
Hi,
while taking care of some patches regarding MISRA C Rule 2.1 (code shouldn't be unreachable), I came across this function:
long subarch_do_domctl(struct xen_domctl *domctl, struct domain *d,
XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl)
{
switch ( domctl->cmd )
{
case XEN_DOMCTL_set_address_size:
switch ( domctl->u.address_size.size )
{
case 32:
if ( !cpu_has_el1_32 )
return -EINVAL;
/* SVE is not supported for 32 bit domain */
if ( is_sve_domain(d) )
return -EINVAL;
return switch_mode(d, DOMAIN_32BIT);
case 64:
return switch_mode(d, DOMAIN_64BIT);
default:
return -EINVAL;
}
break;
default:
return -ENOSYS;
}
}here the break after the innermost switch is clearly unreachable, but it's also guarding a possible fallthrough. I can see a couple of solutions to this:
- mark the part after the switch unreachable;
- introduce a variable 'long rc' to store the return value, and consequently rework the control flow of all the switches (e.g. rc = -EINVAL and similar);
- remove the break, but I consider this a risky move, unless -ENOSYS would be an ok value to be returned if some case from the switch above does not have a return statement.
What would be the preferred way of addressing this violation?
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Quoted 1 line · Nicola Vetrini
Hi,
Hi Nicola,
Quoted 33 lines
while taking care of some patches regarding MISRA C Rule 2.1 (code shouldn't be unreachable), I came across this function:
long subarch_do_domctl(struct
xen_domctl*domctl, struct domain *d,
XEN_GUEST_HANDLE_PARAM(xen_domctl_t)u_domctl)
{
switch (domctl->cmd)
{
caseXEN_DOMCTL_set_address_size:
switch (domctl->u.address_size.size)
{
case 32:
if ( !cpu_has_el1_32 )
return-EINVAL;
/* SVE is not supported for 32 bit domain */ if (is_sve_domain(d))
return-EINVAL;
returnswitch_mode(d, DOMAIN_32BIT);
case 64:
returnswitch_mode(d, DOMAIN_64BIT);
default:
return-EINVAL;
}
break;default:
return-ENOSYS;
}
}here the break after the innermost switch is clearly unreachable, but it's also guarding a possible fallthrough. I can see a couple of solutions to this:
Quoted 7 lines
- mark the part after the switch unreachable;
- introduce a variable 'long rc' to store the return value, and consequently rework the control flow of all the switches (e.g. rc =-EINVALand similar);
- remove the break, but I consider this a risky move, unless-ENOSYSwould be an ok value to be returned if some case
from the switch above does not have a return statement.
- move the nested switch in a separate function, so the code in subarch_do_domctl() can be replaced with:
return set_address_size(...);
Quoted 2 lines
What would be the preferred way of addressing this violation?
I would actually prefer the 4th option I suggested.
Cheers,
Signature · 1 line
Julien Grall
Quoted 50 lines · Julien Grall
Quoted 1 line · Nicola Vetrini
Hi,
Hi Nicola,
Quoted 35 lines
while taking care of some patches regarding MISRA C Rule 2.1 (code shouldn't be unreachable), I
came across this function:long subarch_do_domctl(struct
xen_domctl*domctl, struct domain *d,
XEN_GUEST_HANDLE_PARAM(xen_domctl_t)u_domctl)
{
switch (domctl->cmd)
{
caseXEN_DOMCTL_set_address_size:
switch (domctl->u.address_size.size)
{
case 32:
if ( !cpu_has_el1_32 )
return-EINVAL;
/* SVE is not supported for 32 bit domain */ if (is_sve_domain(d))
return-EINVAL;
returnswitch_mode(d, DOMAIN_32BIT);
case 64:
returnswitch_mode(d, DOMAIN_64BIT);
default:
return-EINVAL;
}
break;default:
return-ENOSYS;
}
}here the break after the innermost switch is clearly unreachable, but it's also guarding a possible fallthrough.
I can see a couple of solutions to this:Quoted 7 lines
- mark the part after the switch unreachable;
- introduce a variable 'long rc' to store the return value, and consequently rework the control flow of all the switches (e.g. rc =-EINVALand similar);
- remove the break, but I consider this a risky move, unless-ENOSYSwould be an ok value to be returned if some case
from the switch above does not have a return statement.
Quoted 4 lines
- move the nested switch in a separate function, so the code in
subarch_do_domctl()can be replaced with:return
set_address_size(...);
But that would help only if inside the new function you still re- layout the switch() (or replace it by, say, if/else-if/else), wouldn't it?
Jan
Hi Jan,
Quoted 51 lines · Jan Beulich
Quoted 50 lines · Julien Grall
Quoted 1 line · Nicola Vetrini
Hi,
Hi Nicola,
Quoted 42 lines
while taking care of some patches regarding MISRA C Rule 2.1 (code shouldn't be unreachable), I
came across this function:long subarch_do_domctl(struct
xen_domctl*domctl, struct domain *d,
XEN_GUEST_HANDLE_PARAM(xen_domctl_t)u_domctl)
{
switch (domctl->cmd)
{
caseXEN_DOMCTL_set_address_size:
switch (domctl->u.address_size.size)
{
case 32:
if ( !cpu_has_el1_32 )
return-EINVAL;
/* SVE is not supported for 32 bit domain */ if (is_sve_domain(d))
return-EINVAL;
returnswitch_mode(d, DOMAIN_32BIT);
case 64:
returnswitch_mode(d, DOMAIN_64BIT);
default:
return-EINVAL;
}
break;default:
return-ENOSYS;
}
}here the break after the innermost switch is clearly unreachable, but it's also guarding a possible fallthrough.
I can see a couple of solutions to this:- mark the part after the switch unreachable;
- introduce a variable 'long rc' to store the return value, and consequently rework the control flow of all the switches (e.g. rc =-EINVALand similar);
- remove the break, but I consider this a risky move, unless-ENOSYSwould be an ok value to be returned if some case
from the switch above does not have a return statement.
Quoted 8 lines
Quoted 4 lines
- move the nested switch in a separate function, so the code in
subarch_do_domctl()can be replaced with:return
set_address_size(...);But that would help only if inside the new function you still re- layout the
switch()(or replace it by, say, if/else-if/else), wouldn't it?
I am not sure why I would need to re-layout the switch. This should work (untested):
xen/arch/arm/arm64/domctl.c +19 -15
@@ -33,27 +33,31 @@ static long switch_mode(struct domain *d, enum domain_type type)return 0 ; } +static long set_address_size (struct domain *d,uint32_t address_size) +{ +switch ( address_size ) + { +case 32 : +if ( !cpu_has_el1_32 ) +return -EINVAL ; +/* SVE is not supported for 32 bit domain */ +if (is_sve_domain (d) ) +return -EINVAL ; +return switch_mode (d,DOMAIN_32BIT ); +case 64 : +return switch_mode (d,DOMAIN_64BIT ); +default : +return -EINVAL ; + } +} +long subarch_do_domctl (struct xen_domctl *domctl,struct domain *d,XEN_GUEST_HANDLE_PARAM (xen_domctl_t) u_domctl) {switch ( domctl->cmd ) {case XEN_DOMCTL_set_address_size: -switch ( domctl->u.address_size.size ) - { -case 32 : -if ( !cpu_has_el1_32 ) -return -EINVAL ; -/* SVE is not supported for 32 bit domain */ -if (is_sve_domain (d) ) -return -EINVAL ; -return switch_mode (d,DOMAIN_32BIT ); -case 64 : -return switch_mode (d,DOMAIN_64BIT ); -default : -return -EINVAL ; - } -break ; +return set_address_size (d, domctl->u.address_size.size);default :return -ENOSYS ;
Cheers,
Signature · 1 line
Julien Grall
Quoted 82 lines · Julien Grall
Hi Jan,
Quoted 51 lines · Jan Beulich
Quoted 50 lines · Julien Grall
Quoted 1 line · Nicola Vetrini
Hi,
Hi Nicola,
Quoted 42 lines
while taking care of some patches regarding MISRA C Rule 2.1 (code shouldn't be unreachable), I
came across this function:long subarch_do_domctl(struct
xen_domctl*domctl, struct domain *d,
XEN_GUEST_HANDLE_PARAM(xen_domctl_t)u_domctl)
{
switch (domctl->cmd)
{
caseXEN_DOMCTL_set_address_size:
switch (domctl->u.address_size.size)
{
case 32:
if ( !cpu_has_el1_32 )
return-EINVAL;
/* SVE is not supported for 32 bit domain */ if (is_sve_domain(d))
return-EINVAL;
returnswitch_mode(d, DOMAIN_32BIT);
case 64:
returnswitch_mode(d, DOMAIN_64BIT);
default:
return-EINVAL;
}
break;default:
return-ENOSYS;
}
}here the break after the innermost switch is clearly unreachable, but it's also guarding a possible fallthrough.
I can see a couple of solutions to this:- mark the part after the switch unreachable;
- introduce a variable 'long rc' to store the return value, and consequently rework the control flow of all the switches (e.g. rc =-EINVALand similar);
- remove the break, but I consider this a risky move, unless-ENOSYSwould be an ok value to be returned if some case
from the switch above does not have a return statement.Quoted 8 lines
Quoted 4 lines
- move the nested switch in a separate function, so the code in
subarch_do_domctl()can be replaced with:return
set_address_size(...);But that would help only if inside the new function you still re- layout the
switch()(or replace it by, say, if/else-if/else), wouldn't it?I am not sure why I would need to re-layout the switch. This should work (untested):
xen/arch/arm/arm64/domctl.c +7
-0@@ -33,27 +33,31 @@ static long switch_mode(struct domain *d, enum domain_type type)return 0 ; } +static long set_address_size (struct domain *d,uint32_t address_size) +{ +switch ( address_size ) + { +case 32 : +if ( !cpu_has_el1_32 ) +return -EINVAL ;
xen/arch/arm/arm64/domctl.c static long switch_mode(struct domain *d, enum · line 53
Quoted 10 lines
xen/arch/arm/arm64/domctl.c +10
-0+/* SVE is not supported for 32 bit domain */ +if (is_sve_domain (d) ) +return -EINVAL ; +return switch_mode (d,DOMAIN_32BIT ); +case 64 : +return switch_mode (d,DOMAIN_64BIT ); +default : +return -EINVAL ; + } +}
Well, yes, if you're happy to have a function returning a value without a return statement at its end.
Jan
Quoted 49 lines · Julien Grall
Quoted 1 line · Nicola Vetrini
Hi,
Hi Nicola,
Quoted 33 lines
while taking care of some patches regarding MISRA C Rule 2.1 (code shouldn't be unreachable), I came across this function:
long subarch_do_domctl(struct
xen_domctl*domctl, struct domain *d,
XEN_GUEST_HANDLE_PARAM(xen_domctl_t)u_domctl)
{
switch (domctl->cmd)
{
caseXEN_DOMCTL_set_address_size:
switch (domctl->u.address_size.size)
{
case 32:
if ( !cpu_has_el1_32 )
return-EINVAL;
/* SVE is not supported for 32 bit domain */ if (is_sve_domain(d))
return-EINVAL;
returnswitch_mode(d, DOMAIN_32BIT);
case 64:
returnswitch_mode(d, DOMAIN_64BIT);
default:
return-EINVAL;
}
break;default:
return-ENOSYS;
}
}here the break after the innermost switch is clearly unreachable, but it's also guarding a possible fallthrough. I can see a couple of solutions to this:
Quoted 7 lines
- mark the part after the switch unreachable;
- introduce a variable 'long rc' to store the return value, and consequently rework the control flow of all the switches (e.g. rc =-EINVALand similar);
- remove the break, but I consider this a risky move, unless-ENOSYSwould be an ok value to be returned if some case
from the switch above does not have a return statement.- move the nested switch in a separate function, so the code in
subarch_do_domctl()can be replaced with:
Quoted 8 lines
return
set_address_size(...);Quoted 2 lines
What would be the preferred way of addressing this violation?
I would actually prefer the 4th option I suggested.
Cheers,
Would you mind sending the patch yourself?
Signature · 2 lines
Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)
Hi Nicola.
Quoted 53 lines · Nicola Vetrini
Quoted 52 lines · Julien Grall
Quoted 1 line · Nicola Vetrini
Hi,
Hi Nicola,
Quoted 33 lines
while taking care of some patches regarding MISRA C Rule 2.1 (code shouldn't be unreachable), I came across this function:
long
subarch_do_domctl(struct xen_domctl *domctl, struct domain *d, XEN_GUEST_HANDLE_PARAM(xen_domctl_t) u_domctl){
switch (domctl->cmd)
{
caseXEN_DOMCTL_set_address_size:
switch (domctl->u.address_size.size)
{
case 32:
if ( !cpu_has_el1_32 )
return-EINVAL;
/* SVE is not supported for 32 bit domain */ if (is_sve_domain(d))
return-EINVAL;
returnswitch_mode(d, DOMAIN_32BIT);
case 64:
returnswitch_mode(d, DOMAIN_64BIT);
default:
return-EINVAL;
}
break;default:
return-ENOSYS;
}
}here the break after the innermost switch is clearly unreachable, but it's also guarding a possible fallthrough. I can see a couple of solutions to this:
Quoted 7 lines
- mark the part after the switch unreachable;
- introduce a variable 'long rc' to store the return value, and consequently rework the control flow of all the switches (e.g. rc =-EINVALand similar);
- remove the break, but I consider this a risky move, unless-ENOSYSwould be an ok value to be returned if some case
from the switch above does not have a return statement.- move the nested switch in a separate function, so the code in
subarch_do_domctl()can be replaced with:return
set_address_size(...);Quoted 1 line
Quoted 7 lines
Quoted 5 lines
Quoted 1 line
What would be the preferred way of addressing this violation?
I would actually prefer the 4th option I suggested.
Cheers,
Would you mind sending the patch yourself?
No. It is sent: https://lore.kernel.org/[email protected]
Cheers,
Signature · 1 line
Julien Grall