Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id 40BD9C00A8F for ; Mon, 23 Oct 2023 15:17:13 +0000 (UTC) Received: from list by lists.xenproject.org with outflank-mailman.621542.968106 (Exim 4.92) (envelope-from ) id 1quwfl-00045K-GO; Mon, 23 Oct 2023 15:16:53 +0000 X-Outflank-Mailman: Message body and most headers restored to incoming version Received: by outflank-mailman (output) from mailman id 621542.968106; Mon, 23 Oct 2023 15:16:53 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1quwfl-00045D-DU; Mon, 23 Oct 2023 15:16:53 +0000 Received: by outflank-mailman (input) for mailman id 621542; Mon, 23 Oct 2023 15:16:52 +0000 Received: from se1-gles-sth1-in.inumbo.com ([159.253.27.254] helo=se1-gles-sth1.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1quwfk-000455-67 for xen-devel@lists.xenproject.org; Mon, 23 Oct 2023 15:16:52 +0000 Received: from support.bugseng.com (mail.bugseng.com [162.55.131.47]) by se1-gles-sth1.inumbo.com (Halon) with ESMTPS id 30c51cb5-71b7-11ee-98d5-6d05b1d4d9a1; Mon, 23 Oct 2023 17:16:50 +0200 (CEST) Received: from support.bugseng.com (support.bugseng.com [162.55.131.47]) by support.bugseng.com (Postfix) with ESMTPA id 58B5C4EE0747; Mon, 23 Oct 2023 17:16:50 +0200 (CEST) X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 30c51cb5-71b7-11ee-98d5-6d05b1d4d9a1 MIME-Version: 1.0 Date: Mon, 23 Oct 2023 17:16:50 +0200 From: Nicola Vetrini To: Julien Grall Cc: Xen-devel , Stefano Stabellini , Bertrand Marquis , Volodymyr Babchuk , consulting@bugseng.com Subject: Re: Refactor arm64/domctl.c 'subarch_do_domctl' to avoid unreachable break. In-Reply-To: References: <6c118d318d68ea3613d9a0a79d75c0c9@bugseng.com> User-Agent: Roundcube Webmail/1.4.3 Message-ID: <902a34031e0173e1219f084560522d0d@bugseng.com> X-Sender: nicola.vetrini@bugseng.com Organization: BUGSENG s.r.l. Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit On 23/10/2023 17:00, Julien Grall wrote: > On 23/10/2023 15:51, Nicola Vetrini wrote: >> Hi, > > Hi Nicola, > >> 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. > > - move the nested switch in a separate function, so the code in > subarch_do_domctl() can be replaced with: > > return set_address_size(...); > >> >> 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? -- Nicola Vetrini, BSc Software Engineer, BUGSENG srl (https://bugseng.com)