Skip to contentSkip to conversation

wireless: ray_cs: Utilize strnlen() in parse_addr()

PATCH In review Andy Shevchenko 3 participants 5 messages Jun 3, 2022 – Jun 8, 2022

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/2 wireless: ray_cs: Utilize strnlen() in parse_addr()

Instead of doing simple operations and using an additional variable on stack, utilize strnlen() and reuse len variable.

---
 drivers/net/wireless/ray_cs.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)
drivers/net/wireless/ray_cs.c +7 -9
index 87e98ab068ed..9ac371d6cd6c 100644
@@ -1643,31 +1643,29 @@ static void authenticate_timeout(struct timer_list *t)
 /*===========================================================================*/
 static int parse_addr(char *in_str, UCHAR *out)
 {
+	int i, k;
 	int len;
-	int i, j, k;
 	int status;
 
 	if (in_str == NULL)
 		return 0;
-	if ((len = strlen(in_str)) < 2)
+	len = strnlen(in_str, ADDRLEN * 2 + 1) - 1;
+	if (len < 1)
 		return 0;
 	memset(out, 0, ADDRLEN);
 
 	status = 1;
-	j = len - 1;
-	if (j > 12)
-		j = 12;
 	i = 5;
 
-	while (j > 0) {
-		if ((k = hex_to_bin(in_str[j--])) != -1)
+	while (len > 0) {
+		if ((k = hex_to_bin(in_str[len--])) != -1)
 			out[i] = k;
 		else
 			return 0;
 
-		if (j == 0)
+		if (len == 0)
 			break;
-		if ((k = hex_to_bin(in_str[j--])) != -1)
+		if ((k = hex_to_bin(in_str[len--])) != -1)
 			out[i] += k << 4;
 		else
 			return 0;
Signature · 1 line
2.35.1
#1 lore raw

PATCH 2/2 wireless: ray_cs: Drop useless status variable in parse_addr()

The status variable assigned only once and used also only once. Replace it's usage by actual value.

---
 drivers/net/wireless/ray_cs.c | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/net/wireless/ray_cs.c +1 -3
index 9ac371d6cd6c..1f57a0055bbd 100644
@@ -1645,7 +1645,6 @@ static int parse_addr(char *in_str, UCHAR *out)
 {
 	int i, k;
 	int len;
-	int status;
 
 	if (in_str == NULL)
 		return 0;
@@ -1654,7 +1653,6 @@ static int parse_addr(char *in_str, UCHAR *out)
 		return 0;
 	memset(out, 0, ADDRLEN);
 
-	status = 1;
 	i = 5;
 
 	while (len > 0) {
@@ -1672,7 +1670,7 @@ static int parse_addr(char *in_str, UCHAR *out)
 		if (!i--)
 			break;
 	}
-	return status;
+	return 1;
 }
 
 /*===========================================================================*/
Signature · 1 line
2.35.1
#2 lore raw
In reply to Andy Shevchenko
Quoted 2 lines · Andy Shevchenko

Instead of doing simple operations and using an additional variable on stack, utilize strnlen() and reuse len variable.

[]

drivers/net/wireless/ray_cs.c

Quoted 1 line
drivers/net/wireless/ray_cs.c +0 -0

[]

drivers/net/wireless/ray_cs.c static void authenticate_timeout(struct timer_list *t) · line 1664

Quoted 5 lines
drivers/net/wireless/ray_cs.c +2 -0
+	while (len > 0) {
+		if ((k = hex_to_bin(in_str[len--])) != -1)
 			out[i] = k;
 		else
 			return 0;

could be reversed and unindented

drivers/net/wireless/ray_cs.c static void authenticate_timeout(struct timer_list *t) · line 1671

Quoted 9 lines
drivers/net/wireless/ray_cs.c +2 -2
-		if (j == 0)
+		if (len == 0)
 			break;
-		if ((k = hex_to_bin(in_str[j--])) != -1)
+		if ((k = hex_to_bin(in_str[len--])) != -1)
 			out[i] += k << 4;
 		else
 			return 0;

and here

#3 lore raw
Quoted 15 lines · Joe Perches
Quoted 2 lines · Andy Shevchenko

Instead of doing simple operations and using an additional variable on stack, utilize strnlen() and reuse len variable.

[]

drivers/net/wireless/ray_cs.c

Quoted 1 line
drivers/net/wireless/ray_cs.c +0 -0

[]

drivers/net/wireless/ray_cs.c static void authenticate_timeout(struct timer_list *t) · line 1664

Quoted 5 lines
drivers/net/wireless/ray_cs.c +2 -0
+	while (len > 0) {
+		if ((k = hex_to_bin(in_str[len--])) != -1)
 			out[i] = k;
 		else
 			return 0;

could be reversed and unindented

Quoted 1 line
Quoted 10 lines

drivers/net/wireless/ray_cs.c static void authenticate_timeout(struct timer_list *t) · line 1671

Quoted 8 lines
drivers/net/wireless/ray_cs.c +2 -2
-		if (j == 0)
+		if (len == 0)
 			break;
-		if ((k = hex_to_bin(in_str[j--])) != -1)
+		if ((k = hex_to_bin(in_str[len--])) != -1)
 			out[i] += k << 4;
 		else
 			return 0;

and here

It might be done as a follow up. Thanks!

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

Re: [v1,1/2] wifi: ray_cs: Utilize strnlen() in parse_addr()

In reply to Andy Shevchenko
Quoted 4 lines · Andy Shevchenko

Instead of doing simple operations and using an additional variable on stack, utilize strnlen() and reuse len variable.

2 patches applied to wireless-next.git, thanks.

9e8e9187673c wifi: ray_cs: Utilize strnlen() in parse_addr() 4dfc63c002a5 wifi: ray_cs: Drop useless status variable in parse_addr()

Signature · 3 lines
https://patchwork.kernel.org/project/linux-wireless/patch/[email protected]/

https://wireless.wiki.kernel.org/en/developers/documentation/submittingpatches