diff mbox series

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

Message ID 20220603164414.48436-1-andriy.shevchenko@linux.intel.com (mailing list archive)
State Awaiting Upstream
Delegated to: Netdev Maintainers
Headers show
Series [v1,1/2] wireless: ray_cs: Utilize strnlen() in parse_addr() | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Andy Shevchenko June 3, 2022, 4:44 p.m. UTC
Instead of doing simple operations and using an additional variable on stack,
utilize strnlen() and reuse len variable.

Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>
---
 drivers/net/wireless/ray_cs.c | 16 +++++++---------
 1 file changed, 7 insertions(+), 9 deletions(-)

Comments

Joe Perches June 3, 2022, 4:50 p.m. UTC | #1
On Fri, 2022-06-03 at 19:44 +0300, Andy Shevchenko wrote:
> Instead of doing simple operations and using an additional variable on stack,
> utilize strnlen() and reuse len variable.
[]
> diff --git a/drivers/net/wireless/ray_cs.c b/drivers/net/wireless/ray_cs.c
[]
> +	while (len > 0) {
> +		if ((k = hex_to_bin(in_str[len--])) != -1)
>  			out[i] = k;
>  		else
>  			return 0;

could be reversed and unindented

>  
> -		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
Andy Shevchenko June 3, 2022, 5:05 p.m. UTC | #2
On Fri, Jun 03, 2022 at 09:50:55AM -0700, Joe Perches wrote:
> On Fri, 2022-06-03 at 19:44 +0300, Andy Shevchenko wrote:
> > Instead of doing simple operations and using an additional variable on stack,
> > utilize strnlen() and reuse len variable.
> []
> > diff --git a/drivers/net/wireless/ray_cs.c b/drivers/net/wireless/ray_cs.c
> []
> > +	while (len > 0) {
> > +		if ((k = hex_to_bin(in_str[len--])) != -1)
> >  			out[i] = k;
> >  		else
> >  			return 0;
> 
> could be reversed and unindented
> 
> >  
> > -		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!
Kalle Valo June 8, 2022, 8:08 a.m. UTC | #3
Andy Shevchenko <andriy.shevchenko@linux.intel.com> wrote:

> Instead of doing simple operations and using an additional variable on stack,
> utilize strnlen() and reuse len variable.
> 
> Signed-off-by: Andy Shevchenko <andriy.shevchenko@linux.intel.com>

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()
diff mbox series

Patch

diff --git a/drivers/net/wireless/ray_cs.c b/drivers/net/wireless/ray_cs.c
index 87e98ab068ed..9ac371d6cd6c 100644
--- a/drivers/net/wireless/ray_cs.c
+++ b/drivers/net/wireless/ray_cs.c
@@ -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;