Received: by 2002:a05:6a10:1d13:0:0:0:0 with SMTP id pp19csp2740398pxb; Tue, 24 Aug 2021 06:34:46 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLecSy4c7EXxMCXU+XPXFWIwIi41RqrSLA2fDyoMAdoNolIQb9cg/S7sheD4o9q/uZEqVm X-Received: by 2002:a6b:6603:: with SMTP id a3mr32173743ioc.68.1629812086753; Tue, 24 Aug 2021 06:34:46 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1629812086; cv=none; d=google.com; s=arc-20160816; b=JaIUt3t8DukuR8IRA7+YguKt7eO3ZeA1X84QP7AdcGQcHr2u2fwD7Whtk1OM60h0mv nCRGPSh0UGEvmiPcZ8nVlHpg2KEwUlYtvmbrsGfacaLZQcCkm1w1GnthycXc6wKlEZPI YjTjcTqDloAzqSYFg1/BKnh/WdyizVvFyRmo/Y2AdfaBS6NiE95MiGCcMJ+hbWgvr/5O /qqFnXsBGOrtRlQPbxb0EHc/HBAmZxwS8R1hm1w22yWRmVOSXn21lDswkd8lr5/2M70c n8cZ/63k0++C8gbGRjWDRyTKlr48bOCFSUhoTEpcocRfirNXi5Tjhy7rhdassuwadRg0 7cRg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version :dkim-signature; bh=xBb3ZyQvb4VRddrOmbezmniUyEwoDvBRNY4xhO/mzFE=; b=sUm08EF73gCrWlP1CvDR7Sbv13Jw23YsE0mbQhKKjZWsDn8kGw4PMYLKSbChmAe6Na USxzTPpkMRz0fzRa0UYoiBbPN50BtRE0cgfGTLk8U3gKUIcI7mB0UPo+jL1rmHVzFVe2 UYvz9Xr8JN1wmkE+BmHlAxnW9vvLhgwu49iL1mHhpqHTUa4fYkVQQ3aSP5pVNbLkSqNF CZLKJh284i1Sx/XKcekKq2G72VgOL8a/2WRf8uPGPawLWwmGCqaFis5Qba/OkYpmBYyn uGmImPIMlpVyRVf2ZM5NC4Lc0YeUy8QS8Y242gt27wowUg3pEfsw4j6Ei0RTo0CRyl51 w8mQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=pmqylbIk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id r5si17197071jan.104.2021.08.24.06.34.35; Tue, 24 Aug 2021 06:34:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=pmqylbIk; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237581AbhHXNeB (ORCPT + 99 others); Tue, 24 Aug 2021 09:34:01 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35878 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237591AbhHXNd7 (ORCPT ); Tue, 24 Aug 2021 09:33:59 -0400 Received: from mail-pg1-x535.google.com (mail-pg1-x535.google.com [IPv6:2607:f8b0:4864:20::535]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0449BC0613C1; Tue, 24 Aug 2021 06:33:15 -0700 (PDT) Received: by mail-pg1-x535.google.com with SMTP id k24so19790111pgh.8; Tue, 24 Aug 2021 06:33:14 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=xBb3ZyQvb4VRddrOmbezmniUyEwoDvBRNY4xhO/mzFE=; b=pmqylbIkUXIiBjp2a0mSg7vZamKOTdoF4mgWbJiekfcmePrnq/uH0McSwzV8vx+BDD xg7+WJYiO5J5OLbSRVPi5kegrODWIU4Rggz+DFfbvG7CYQR1GW3jTSvzM7XO7m/PHkxr c8kTEAAAIb2IZzANhR6N+RueVnfcwcv7sT8oFWW19w9MJyEizfcZHLPepDXTwXJLuln5 5NLU647zmxE9P8cINDdIESQm3pbS5RMlW3byZjEkBCvbA3ULBW0y8ltWuiQKIFNFjrix i4PaV30q2OX7XAcZ3XcNxoaCGfg8cepANkr61wTanFJANcQ5bM6rPeKFA2zYiOZ2wIj9 Em+Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=xBb3ZyQvb4VRddrOmbezmniUyEwoDvBRNY4xhO/mzFE=; b=f15Wk6ImD/l71EtedNi7X1aF33iDNLOcgVsAR3TGGI7ROusmEsAxoMUJSwM9ixbPAz HQDhlUkoQofm0YNWTEG0iFvZ0IkosW+NpJ3sd3N8TEwuNt8B8GVj03GXxAjGZg1s0JDT Y6N+gCdrtF1aPNbePeZ6uumYCg9elbCbc8Yl+VZomMzPMxrWqTja8941BnyZYM1fv1/s ZSfwP+lPxELwJfgMKjs3zy7aT8zQ50tuWHVfWR86bpdSbaxVd8VxX8yQDhCYwwWwaEqo uTA2FKY95NU9ISh3hq1wJNQWdlPL6I14h5F+9lco0q024x1Wu+FJlZvbefQDj0+0e24I 298g== X-Gm-Message-State: AOAM531dXzz5AAbFGEHeEpEX0edmc81t+Avzfs5kKQFV+YzzGwpuXdjZ pXOCoWSBJHZRoQlX89xCu59m7ydsT7yBtXDr9+U= X-Received: by 2002:a63:2242:: with SMTP id t2mr36064978pgm.111.1629811994392; Tue, 24 Aug 2021 06:33:14 -0700 (PDT) MIME-Version: 1.0 References: <20210824022247.GA22908@raspberrypi> In-Reply-To: From: Austin Kim Date: Tue, 24 Aug 2021 22:33:03 +0900 Message-ID: Subject: Re: [PATCH] selinux: remove duplicated initialization of 'i' for clean-up To: Ondrej Mosnacek Cc: Paul Moore , Stephen Smalley , Eric Paris , SElinux list , Linux kernel mailing list , kernel-team@lge.com, =?UTF-8?B?6rmA64+Z7ZiE?= Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2021=EB=85=84 8=EC=9B=94 24=EC=9D=BC (=ED=99=94) =EC=98=A4=ED=9B=84 8:28, O= ndrej Mosnacek =EB=8B=98=EC=9D=B4 =EC=9E=91=EC=84=B1: > > Hi Austin, > > On Tue, Aug 24, 2021 at 4:23 AM Austin Kim wrote= : > > > > From: Austin Kim > > > > The local variable 'i' is used to be incremented inside while loop > > within sidtab_convert_tree(). Before while loop, 'i' is set to 0 > > inside if/else statement. > > > > Since there is no 'goto' statement within sidtab_convert_tree(), > > it had better initialize 'i' as 0 once before if/else statement. > > > > Signed-off-by: Austin Kim > > --- > > security/selinux/ss/sidtab.c | 4 +--- > > 1 file changed, 1 insertion(+), 3 deletions(-) > > > > diff --git a/security/selinux/ss/sidtab.c b/security/selinux/ss/sidtab.= c > > index 656d50b09f76..301620de63d3 100644 > > --- a/security/selinux/ss/sidtab.c > > +++ b/security/selinux/ss/sidtab.c > > @@ -374,7 +374,7 @@ static int sidtab_convert_tree(union sidtab_entry_i= nner *edst, > > struct sidtab_convert_params *convert) > > { > > int rc; > > - u32 i; > > + u32 i =3D 0; > > > > if (level !=3D 0) { > > if (!edst->ptr_inner) { > > @@ -383,7 +383,6 @@ static int sidtab_convert_tree(union sidtab_entry_i= nner *edst, > > if (!edst->ptr_inner) > > return -ENOMEM; > > } > > - i =3D 0; > > while (i < SIDTAB_INNER_ENTRIES && *pos < count) { > > I must say I prefer the original version more, because it makes it > clear when you look at the loop that it starts at 0. Once you move the > initialization to the declaration section, readers will have to scan > the code upwards to find it out. Thanks for sharing the full story of the original version. The original code is more readable since 'i' is mainly used in 'for' loop rather than 'while' loop. Also agreed that reader might attempt to scan the initial value of 'i'. Thanks, Austin Kim As is, it's also less prone to error > if e.g. someone adds another loop before the existing ones and reuses > the variable. > > In case anyone is wondering why I didn't make these for loops when I > wrote this code: Since the loop condition a little more than the usual > "for(i =3D 0; i < n; i++)" pattern, my intention was to emphasize that > this is not a "regular" for loop and that readers should read the loop > carefully to not miss something. But perhaps that's not a good reason > and they would look more natural as for loops. If others think a for > loop would look better here, I'd be OK with a patch that makes these > into for loops instead. > > > rc =3D sidtab_convert_tree(&edst->ptr_inner->en= tries[i], > > &esrc->ptr_inner->entr= ies[i], > > @@ -400,7 +399,6 @@ static int sidtab_convert_tree(union sidtab_entry_i= nner *edst, > > if (!edst->ptr_leaf) > > return -ENOMEM; > > } > > - i =3D 0; > > while (i < SIDTAB_LEAF_ENTRIES && *pos < count) { > > rc =3D convert->func(&esrc->ptr_leaf->entries[i= ].context, > > &edst->ptr_leaf->entries[i].= context, > > -- > > 2.20.1 > > > > -- > Ondrej Mosnacek > Software Engineer, Linux Security - SELinux kernel > Red Hat, Inc. >