Skip to contentSkip to conversation

selinux: remove duplicated initialization of 'i' for clean-up

PATCH In review Austin Kim 3 participants 4 messages Aug 24, 2021 SELinux security module

lore
Share & tools
lore

Austin Kim

author
#0 lore raw

PATCH 1/1 selinux: remove duplicated initialization of 'i' for clean-up

From: Austin Kim <[email protected]>

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.

---
 security/selinux/ss/sidtab.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
security/selinux/ss/sidtab.c +1 -3
index 656d50b09f76..301620de63d3 100644
@@ -374,7 +374,7 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
 			       struct sidtab_convert_params *convert)
 {
 	int rc;
-	u32 i;
+	u32 i = 0;
 
 	if (level != 0) {
 		if (!edst->ptr_inner) {
@@ -383,7 +383,6 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
 			if (!edst->ptr_inner)
 				return -ENOMEM;
 		}
-		i = 0;
 		while (i < SIDTAB_INNER_ENTRIES && *pos < count) {
 			rc = sidtab_convert_tree(&edst->ptr_inner->entries[i],
 						 &esrc->ptr_inner->entries[i],
@@ -400,7 +399,6 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
 			if (!edst->ptr_leaf)
 				return -ENOMEM;
 		}
-		i = 0;
 		while (i < SIDTAB_LEAF_ENTRIES && *pos < count) {
 			rc = convert->func(&esrc->ptr_leaf->entries[i].context,
 					   &edst->ptr_leaf->entries[i].context,
Signature · 1 line
2.20.1
#1 lore raw

Hi Austin,

Quoted 29 lines · Austin Kim

From: Austin Kim <[email protected]>

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.

---
 security/selinux/ss/sidtab.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
security/selinux/ss/sidtab.c +1 -1
index 656d50b09f76..301620de63d3 100644
@@ -374,7 +374,7 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
                               struct sidtab_convert_params *convert)
 {
        int rc;
-       u32 i;
+       u32 i = 0;
 
        if (level != 0) {
                if (!edst->ptr_inner) {

security/selinux/ss/sidtab.c static int sidtab_convert_tree(union sidtab_entry_inner *edst, · line 386

Quoted 5 lines
security/selinux/ss/sidtab.c +0 -1
@@ -383,7 +383,6 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
                        if (!edst->ptr_inner)
                                return -ENOMEM;
                }
-               i = 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. 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 = 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.

Quoted 11 lines
security/selinux/ss/sidtab.c +0 -2
                        rc = sidtab_convert_tree(&edst->ptr_inner->entries[i],
                                                 &esrc->ptr_inner->entries[i],
@@ -400,7 +399,6 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
                        if (!edst->ptr_leaf)
                                return -ENOMEM;
                }
-               i = 0;
                while (i < SIDTAB_LEAF_ENTRIES && *pos < count) {
                        rc = convert->func(&esrc->ptr_leaf->entries[i].context,
                                           &edst->ptr_leaf->entries[i].context,
--
Quoted 2 lines

2.20.1

--
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.

author
#2 lore raw

2021년 8월 24일 (화) 오후 8:28, Ondrej Mosnacek <[email protected]>님이 작성:

Quoted 30 lines

Hi Austin,

Quoted 26 lines · Austin Kim

From: Austin Kim <[email protected]>

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.

---
 security/selinux/ss/sidtab.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
security/selinux/ss/sidtab.c +1 -1
index 656d50b09f76..301620de63d3 100644
@@ -374,7 +374,7 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
                               struct sidtab_convert_params *convert)
 {
        int rc;
-       u32 i;
+       u32 i = 0;
 
Quoted 13 lines

security/selinux/ss/sidtab.c static int sidtab_convert_tree(union sidtab_entry_inner *edst, · line 386

Quoted 8 lines
security/selinux/ss/sidtab.c +0 -1
        if (level != 0) {
                if (!edst->ptr_inner) {
@@ -383,7 +383,6 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
                        if (!edst->ptr_inner)
                                return -ENOMEM;
                }
-               i = 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

Quoted 31 lines

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 = 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.

Quoted 11 lines
security/selinux/ss/sidtab.c +0 -2
                        rc = sidtab_convert_tree(&edst->ptr_inner->entries[i],
                                                 &esrc->ptr_inner->entries[i],
@@ -400,7 +399,6 @@ static int sidtab_convert_tree(union sidtab_entry_inner *edst,
                        if (!edst->ptr_leaf)
                                return -ENOMEM;
                }
-               i = 0;
                while (i < SIDTAB_LEAF_ENTRIES && *pos < count) {
                        rc = convert->func(&esrc->ptr_leaf->entries[i].context,
                                           &edst->ptr_leaf->entries[i].context,
--
Quoted 2 lines

2.20.1

--
Ondrej Mosnacek
Software Engineer, Linux Security - SELinux kernel Red Hat, Inc.

maintainer
#3 lore raw
In reply to Austin Kim
Quoted 7 lines · Austin Kim

From: Austin Kim <[email protected]>

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.

Quoted 7 lines

Since there is no 'goto' statement within sidtab_convert_tree(), it had better initialize 'i' as 0 once before if/else statement.

---
 security/selinux/ss/sidtab.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

Let's leave the existing code as-is for now.

Signature · 2 lines
paul moore
www.paul-moore.com