Linux USB
 help / color / mirror / Atom feed
* [PATCH] usb: gadget: avoid redundant strlen() in make_name_string()
@ 2026-11-17 17:02 Dmitry Antipov
  2026-11-17 17:10 ` sashiko-bot
  2026-11-17 17:50 ` Takashi Iwai
  0 siblings, 2 replies; 6+ messages in thread
From: Dmitry Antipov @ 2026-11-17 17:02 UTC (permalink / raw)
  To: Neill Kapron, Greg Kroah-Hartman; +Cc: Takashi Iwai, linux-usb, Dmitry Antipov

In make_name_string(), prefer strchrnul() over strchr() to
avoid redundant call to strlen().

Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
---
 drivers/usb/gadget/function/f_midi2.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/drivers/usb/gadget/function/f_midi2.c b/drivers/usb/gadget/function/f_midi2.c
index d693de427641..76738ea55dba 100644
--- a/drivers/usb/gadget/function/f_midi2.c
+++ b/drivers/usb/gadget/function/f_midi2.c
@@ -2101,11 +2101,10 @@ static void make_name_string(char *s)
 {
 	char *p;
 
-	p = strchr(s, '\n');
-	if (p)
+	p = strchrnul(s, '\n');
+	if (*p)
 		*p = 0;
 
-	p = s + strlen(s);
 	for (; p > s && isspace(*p); p--)
 		*p = 0;
 }
-- 
2.56.1


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [PATCH] usb: gadget: avoid redundant strlen() in make_name_string()
  2026-11-17 17:02 [PATCH] usb: gadget: avoid redundant strlen() in make_name_string() Dmitry Antipov
@ 2026-11-17 17:10 ` sashiko-bot
  2026-11-17 17:50 ` Takashi Iwai
  1 sibling, 0 replies; 6+ messages in thread
From: sashiko-bot @ 2026-11-17 17:10 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: linux-usb

> In make_name_string(), prefer strchrnul() over strchr() to
> avoid redundant call to strlen().
> 
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>

Sashiko has reviewed this patch and found no issues.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261117170207.642330-1-dmantipov@yandex.ru?part=1


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] usb: gadget: avoid redundant strlen() in make_name_string()
  2026-11-17 17:02 [PATCH] usb: gadget: avoid redundant strlen() in make_name_string() Dmitry Antipov
  2026-11-17 17:10 ` sashiko-bot
@ 2026-11-17 17:50 ` Takashi Iwai
  2026-11-17 18:10   ` Takashi Iwai
  1 sibling, 1 reply; 6+ messages in thread
From: Takashi Iwai @ 2026-11-17 17:50 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Neill Kapron, Greg Kroah-Hartman, Takashi Iwai, linux-usb

On Tue, 17 Nov 2026 18:02:07 +0100,
Dmitry Antipov wrote:
> 
> In make_name_string(), prefer strchrnul() over strchr() to
> avoid redundant call to strlen().
> 
> Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> ---
>  drivers/usb/gadget/function/f_midi2.c | 5 ++---
>  1 file changed, 2 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/usb/gadget/function/f_midi2.c b/drivers/usb/gadget/function/f_midi2.c
> index d693de427641..76738ea55dba 100644
> --- a/drivers/usb/gadget/function/f_midi2.c
> +++ b/drivers/usb/gadget/function/f_midi2.c
> @@ -2101,11 +2101,10 @@ static void make_name_string(char *s)
>  {
>  	char *p;
>  
> -	p = strchr(s, '\n');
> -	if (p)
> +	p = strchrnul(s, '\n');
> +	if (*p)
>  		*p = 0;
>  
> -	p = s + strlen(s);
>  	for (; p > s && isspace(*p); p--)
>  		*p = 0;
>  }

Hmm, does it really result in the same?  Wouldn't you need to
decrement p at first before the loop?

And I guess this can be better replaced with strim().


thanks,

Takashi

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] usb: gadget: avoid redundant strlen() in make_name_string()
  2026-11-17 17:50 ` Takashi Iwai
@ 2026-11-17 18:10   ` Takashi Iwai
  2026-11-17 19:08     ` Dmitry Antipov
  0 siblings, 1 reply; 6+ messages in thread
From: Takashi Iwai @ 2026-11-17 18:10 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Neill Kapron, Greg Kroah-Hartman, linux-usb

On Tue, 17 Nov 2026 18:50:03 +0100,
Takashi Iwai wrote:
> 
> On Tue, 17 Nov 2026 18:02:07 +0100,
> Dmitry Antipov wrote:
> > 
> > In make_name_string(), prefer strchrnul() over strchr() to
> > avoid redundant call to strlen().
> > 
> > Signed-off-by: Dmitry Antipov <dmantipov@yandex.ru>
> > ---
> >  drivers/usb/gadget/function/f_midi2.c | 5 ++---
> >  1 file changed, 2 insertions(+), 3 deletions(-)
> > 
> > diff --git a/drivers/usb/gadget/function/f_midi2.c b/drivers/usb/gadget/function/f_midi2.c
> > index d693de427641..76738ea55dba 100644
> > --- a/drivers/usb/gadget/function/f_midi2.c
> > +++ b/drivers/usb/gadget/function/f_midi2.c
> > @@ -2101,11 +2101,10 @@ static void make_name_string(char *s)
> >  {
> >  	char *p;
> >  
> > -	p = strchr(s, '\n');
> > -	if (p)
> > +	p = strchrnul(s, '\n');
> > +	if (*p)
> >  		*p = 0;
> >  
> > -	p = s + strlen(s);
> >  	for (; p > s && isspace(*p); p--)
> >  		*p = 0;
> >  }
> 
> Hmm, does it really result in the same?  Wouldn't you need to
> decrement p at first before the loop?

... and looking again, I think it's a bug in my original code ;)

> And I guess this can be better replaced with strim().

... which can be fixed by replacing with strim() gracefully.

Care to resubmit as a fix?


thanks,

Takashi

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] usb: gadget: avoid redundant strlen() in make_name_string()
  2026-11-17 18:10   ` Takashi Iwai
@ 2026-11-17 19:08     ` Dmitry Antipov
  2026-11-17 21:10       ` Takashi Iwai
  0 siblings, 1 reply; 6+ messages in thread
From: Dmitry Antipov @ 2026-11-17 19:08 UTC (permalink / raw)
  To: Takashi Iwai; +Cc: Neill Kapron, Greg Kroah-Hartman, linux-usb

On 11/17/26 9:10 PM, Takashi Iwai wrote:

>> Hmm, does it really result in the same?  Wouldn't you need to
>> decrement p at first before the loop?
> 
> ... and looking again, I think it's a bug in my original code ;)
> 
>> And I guess this can be better replaced with strim().
> 
> ... which can be fixed by replacing with strim() gracefully.
> 
> Care to resubmit as a fix?

strim() implies strlen() which is not actually needed here :-).
So (assuming I've hit an original intention) I'm voting for:

void make_name_string(char *s)
{
	char *p;

	p = strchrnul(s, '\n');
	if (*p)
		*p = 0;

	while (--p > s && isspace(*p))
		*p = 0;
}

Note this trims " \n" to " " (string of the only space character).
If this is not what we want here,

while (--p >= s && isspace(*p))

converts " \n" to an empty string.

Dmitry


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] usb: gadget: avoid redundant strlen() in make_name_string()
  2026-11-17 19:08     ` Dmitry Antipov
@ 2026-11-17 21:10       ` Takashi Iwai
  0 siblings, 0 replies; 6+ messages in thread
From: Takashi Iwai @ 2026-11-17 21:10 UTC (permalink / raw)
  To: Dmitry Antipov; +Cc: Takashi Iwai, Neill Kapron, Greg Kroah-Hartman, linux-usb

On Tue, 17 Nov 2026 20:08:01 +0100,
Dmitry Antipov wrote:
> 
> On 11/17/26 9:10 PM, Takashi Iwai wrote:
> 
> >> Hmm, does it really result in the same?  Wouldn't you need to
> >> decrement p at first before the loop?
> > 
> > ... and looking again, I think it's a bug in my original code ;)
> > 
> >> And I guess this can be better replaced with strim().
> > 
> > ... which can be fixed by replacing with strim() gracefully.
> > 
> > Care to resubmit as a fix?
> 
> strim() implies strlen() which is not actually needed here :-).
> So (assuming I've hit an original intention) I'm voting for:
> 
> void make_name_string(char *s)
> {
> 	char *p;
> 
> 	p = strchrnul(s, '\n');
> 	if (*p)
> 		*p = 0;
> 
> 	while (--p > s && isspace(*p))
> 		*p = 0;
> }
> 
> Note this trims " \n" to " " (string of the only space character).
> If this is not what we want here,
> 
> while (--p >= s && isspace(*p))
> 
> converts " \n" to an empty string.

For that code path, chasing for performance with open-code has no
merit.  A simpler code would have much more benefit, instead.

Basically make_name_string() call can be simply replaced strim(), and
you can get rid of buggy (or error-prone) open code.


Takashi

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-11-17 21:10 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17 17:02 [PATCH] usb: gadget: avoid redundant strlen() in make_name_string() Dmitry Antipov
2026-11-17 17:10 ` sashiko-bot
2026-11-17 17:50 ` Takashi Iwai
2026-11-17 18:10   ` Takashi Iwai
2026-11-17 19:08     ` Dmitry Antipov
2026-11-17 21:10       ` Takashi Iwai

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox