Skip to contentSkip to conversation

staging: rtl8723bs: simplify control flow

PATCH In review Sevinj Aghayeva 3 participants 5 messages Apr 1, 2022 – Apr 3, 2022 Staging

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 staging: rtl8723bs: simplify control flow

The function iterates an index from 0 to NUM_PMKID_CACHE and returns the first index for which the condition is true. If no such index is found, the function returns -1. Current code has a complex control flow that obfuscates this simple task. Replace it with a loop.

Also, given the shortened function body, replace the long variable name psecuritypriv with a short variable name p.

Reported by checkpatch:

WARNING: else is not generally useful after a break or return

---
 drivers/staging/rtl8723bs/core/rtw_mlme.c | 28 ++++++-----------------
 1 file changed, 7 insertions(+), 21 deletions(-)
drivers/staging/rtl8723bs/core/rtw_mlme.c +7 -21
index d5bb3a5bd2fb..3eacf8f9d236 100644
@@ -2036,28 +2036,14 @@ int rtw_restruct_wmm_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_
 
 static int SecIsInPMKIDList(struct adapter *Adapter, u8 *bssid)
 {
-	struct security_priv *psecuritypriv = &Adapter->securitypriv;
-	int i = 0;
-
-	do {
-		if ((psecuritypriv->PMKIDList[i].bUsed) &&
-				(!memcmp(psecuritypriv->PMKIDList[i].Bssid, bssid, ETH_ALEN))) {
-			break;
-		} else {
-			i++;
-			/* continue; */
-		}
-
-	} while (i < NUM_PMKID_CACHE);
-
-	if (i == NUM_PMKID_CACHE) {
-		i = -1;/*  Could not find. */
-	} else {
-		/*  There is one Pre-Authentication Key for the specific BSSID. */
-	}
-
-	return i;
+	struct security_priv *p = &Adapter->securitypriv;
+	int i;
 
+	for (i = 0; i < NUM_PMKID_CACHE; i++)
+		if ((p->PMKIDList[i].bUsed) &&
+				(!memcmp(p->PMKIDList[i].Bssid, bssid, ETH_ALEN)))
+			return i;
+	return -1;
 }
 
 /*  */
Signature · 1 line
2.25.1
Reviewed-by on v1 1/1
#1 lore raw
Quoted 5 lines · Sevinj Aghayeva

The function iterates an index from 0 to NUM_PMKID_CACHE and returns the first index for which the condition is true. If no such index is found, the function returns -1. Current code has a complex control flow that obfuscates this simple task. Replace it with a loop.

Quoted 8 lines

Also, given the shortened function body, replace the long variable name psecuritypriv with a short variable name p.

Reported by checkpatch:

WARNING: else is not generally useful after a break or return

Wow! Nice find! This is a huge clean up. Extra kudos recognizing that it is not just the else statement which is broken here!

The only issue for the patch is that I don't see any maintainer emailed? However, I don't see a maintainer listed in the MAINTAINERS file so ...

Quoted 47 lines
---
 drivers/staging/rtl8723bs/core/rtw_mlme.c | 28 ++++++-----------------
 1 file changed, 7 insertions(+), 21 deletions(-)
drivers/staging/rtl8723bs/core/rtw_mlme.c +7 -21
index d5bb3a5bd2fb..3eacf8f9d236 100644
@@ -2036,28 +2036,14 @@ int rtw_restruct_wmm_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_
 
 static int SecIsInPMKIDList(struct adapter *Adapter, u8 *bssid)
 {
-	struct security_priv *psecuritypriv = &Adapter->securitypriv;
-	int i = 0;
-
-	do {
-		if ((psecuritypriv->PMKIDList[i].bUsed) &&
-				(!memcmp(psecuritypriv->PMKIDList[i].Bssid, bssid, ETH_ALEN))) {
-			break;
-		} else {
-			i++;
-			/* continue; */
-		}
-
-	} while (i < NUM_PMKID_CACHE);
-
-	if (i == NUM_PMKID_CACHE) {
-		i = -1;/*  Could not find. */
-	} else {
-		/*  There is one Pre-Authentication Key for the specific BSSID. */
-	}
-
-	return i;
+	struct security_priv *p = &Adapter->securitypriv;
+	int i;
 
+	for (i = 0; i < NUM_PMKID_CACHE; i++)
+		if ((p->PMKIDList[i].bUsed) &&
+				(!memcmp(p->PMKIDList[i].Bssid, bssid, ETH_ALEN)))
+			return i;
+	return -1;
 }
 
 /*  */
Signature · 1 line
2.25.1
#2 lore raw
Quoted 9 lines · Ira Weiny
Quoted 8 lines · Sevinj Aghayeva

The function iterates an index from 0 to NUM_PMKID_CACHE and returns the first index for which the condition is true. If no such index is found, the function returns -1. Current code has a complex control flow that obfuscates this simple task. Replace it with a loop.

Also, given the shortened function body, replace the long variable name psecuritypriv with a short variable name p.

Quoted 8 lines
Quoted 5 lines

Reported by checkpatch:

WARNING: else is not generally useful after a break or return

Wow! Nice find! This is a huge clean up. Extra kudos recognizing that it is not just the else statement which is broken here!

Thanks! It took me a while to realize what this loop is doing.

Quoted 4 lines

The only issue for the patch is that I don't see any maintainer emailed? However, I don't see a maintainer listed in the MAINTAINERS file so ...

Thanks for the review!

Greg, please do not apply this yet. After I sent out the patch, I noticed the comment at the top of the function:

/* Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */

So I did a git grep to find the original function and fix it as well, and it looks like there are three copies of the same function in different files:

$ git grep IsInPreAuthKeyList
r8188eu/core/rtw_mlme.c:/*  Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */
rtl8712/rtl871x_mlme.c: * Ported from 8185: IsInPreAuthKeyList().
rtl8723bs/core/rtw_mlme.c:/*  Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */

I will later send a v2 patch that replaces all of them.

Thanks

Quoted 47 lines
Quoted 47 lines
---
 drivers/staging/rtl8723bs/core/rtw_mlme.c | 28 ++++++-----------------
 1 file changed, 7 insertions(+), 21 deletions(-)
drivers/staging/rtl8723bs/core/rtw_mlme.c +7 -21
index d5bb3a5bd2fb..3eacf8f9d236 100644
@@ -2036,28 +2036,14 @@ int rtw_restruct_wmm_ie(struct adapter *adapter, u8 *in_ie, u8 *out_ie, uint in_
 
 static int SecIsInPMKIDList(struct adapter *Adapter, u8 *bssid)
 {
-	struct security_priv *psecuritypriv = &Adapter->securitypriv;
-	int i = 0;
-
-	do {
-		if ((psecuritypriv->PMKIDList[i].bUsed) &&
-				(!memcmp(psecuritypriv->PMKIDList[i].Bssid, bssid, ETH_ALEN))) {
-			break;
-		} else {
-			i++;
-			/* continue; */
-		}
-
-	} while (i < NUM_PMKID_CACHE);
-
-	if (i == NUM_PMKID_CACHE) {
-		i = -1;/*  Could not find. */
-	} else {
-		/*  There is one Pre-Authentication Key for the specific BSSID. */
-	}
-
-	return i;
+	struct security_priv *p = &Adapter->securitypriv;
+	int i;
 
+	for (i = 0; i < NUM_PMKID_CACHE; i++)
+		if ((p->PMKIDList[i].bUsed) &&
+				(!memcmp(p->PMKIDList[i].Bssid, bssid, ETH_ALEN)))
+			return i;
+	return -1;
 }
 
 /*  */
Signature · 1 line
2.25.1
#3 lore raw
Quoted 37 lines · Sevinj Aghayeva
Quoted 9 lines · Ira Weiny
Quoted 8 lines · Sevinj Aghayeva

The function iterates an index from 0 to NUM_PMKID_CACHE and returns the first index for which the condition is true. If no such index is found, the function returns -1. Current code has a complex control flow that obfuscates this simple task. Replace it with a loop.

Also, given the shortened function body, replace the long variable name psecuritypriv with a short variable name p.

Quoted 8 lines
Quoted 5 lines

Reported by checkpatch:

WARNING: else is not generally useful after a break or return

Wow! Nice find! This is a huge clean up. Extra kudos recognizing that it is not just the else statement which is broken here!

Thanks! It took me a while to realize what this loop is doing.

Quoted 4 lines

The only issue for the patch is that I don't see any maintainer emailed? However, I don't see a maintainer listed in the MAINTAINERS file so ...

Thanks for the review!

Greg, please do not apply this yet. After I sent out the patch, I noticed the comment at the top of the function:

/* Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */

So I did a git grep to find the original function and fix it as well, and it looks like there are three copies of the same function in different files:

Quoted 6 lines
$ git grep IsInPreAuthKeyList
r8188eu/core/rtw_mlme.c:/*  Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */
rtl8712/rtl871x_mlme.c: * Ported from 8185: IsInPreAuthKeyList().
rtl8723bs/core/rtw_mlme.c:/*  Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */

I will later send a v2 patch that replaces all of them.

No, please do one patch per driver. These are all different drivers (cut/pasted from some original source), so this patch is fine as-is. You can make 2 other patches as well for the other drivers.

thanks,

greg k-h

#4 lore raw
Quoted 44 lines · Greg Kroah-Hartman
Quoted 42 lines · Sevinj Aghayeva
Quoted 9 lines · Ira Weiny
Quoted 8 lines · Sevinj Aghayeva

The function iterates an index from 0 to NUM_PMKID_CACHE and returns the first index for which the condition is true. If no such index is found, the function returns -1. Current code has a complex control flow that obfuscates this simple task. Replace it with a loop.

Also, given the shortened function body, replace the long variable name psecuritypriv with a short variable name p.

Quoted 8 lines
Quoted 5 lines

Reported by checkpatch:

WARNING: else is not generally useful after a break or return

Wow! Nice find! This is a huge clean up. Extra kudos recognizing that it is not just the else statement which is broken here!

Thanks! It took me a while to realize what this loop is doing.

Quoted 4 lines

The only issue for the patch is that I don't see any maintainer emailed? However, I don't see a maintainer listed in the MAINTAINERS file so ...

Thanks for the review!

Greg, please do not apply this yet. After I sent out the patch, I noticed the comment at the top of the function:

/* Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */

So I did a git grep to find the original function and fix it as well, and it looks like there are three copies of the same function in different files:

$ git grep IsInPreAuthKeyList
r8188eu/core/rtw_mlme.c:/*  Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */
rtl8712/rtl871x_mlme.c: * Ported from 8185: IsInPreAuthKeyList().
rtl8723bs/core/rtw_mlme.c:/*  Ported from 8185: IsInPreAuthKeyList(). (Renamed from SecIsInPreAuthKeyList(), 2006-10-13.) */
Quoted 5 lines
Quoted 1 line

I will later send a v2 patch that replaces all of them.

No, please do one patch per driver. These are all different drivers (cut/pasted from some original source), so this patch is fine as-is. You can make 2 other patches as well for the other drivers.

Sure, will send two more patches.

Thanks

Quoted 4 lines

thanks,

greg k-h

Signature · 1 line
Sevinj.Aghayeva