Linux USB
 help / color / mirror / Atom feed
From: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
To: bence98@sch.bme.hu
Cc: linux-usb@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] USB: core: Use `krealloc()` in `usb_cache_string()`
Date: Thu, 12 Mar 2026 06:02:25 +0100	[thread overview]
Message-ID: <2026031209-shawl-unshackle-f4eb@gregkh> (raw)
In-Reply-To: <20260312-usb-krealloc-v1-1-f76b92b92402@sch.bme.hu>

On Thu, Mar 12, 2026 at 12:06:35AM +0100, Bence Csókás via B4 Relay wrote:
> From: Bence Csókás <bence98@sch.bme.hu>
> 
> Instead of "shrinking" the allocation by `kmalloc()`ing a new, smaller
> buffer, utilize `krealloc()` to shrink the existing allocation. This saves
> a `memcpy()`, as well as guards against `smallbuf` allocation failure.
> 
> Signed-off-by: Bence Csókás <bence98@sch.bme.hu>
> ---
> Using `krealloc()` makes this code from 2005 more readable as well as
> robust. Nested `if`s were also unrolled.

How is it more "robust" now?

> ---
>  drivers/usb/core/message.c | 20 ++++++++++----------
>  1 file changed, 10 insertions(+), 10 deletions(-)

Same number of lines. Well, not quite, because I'm going to ask you to
remove the ?: stuff below...

> diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
> index ea970ddf8879..dfe61d8b913b 100644
> --- a/drivers/usb/core/message.c
> +++ b/drivers/usb/core/message.c
> @@ -1005,7 +1005,7 @@ int usb_string(struct usb_device *dev, int index, char *buf, size_t size)
>  }
>  EXPORT_SYMBOL_GPL(usb_string);
>  
> -/* one UTF-8-encoded 16-bit character has at most three bytes */
> +/* one 16-bit character, when UTF-8-encoded, has at most three bytes */

Why change this?

>  #define MAX_USB_STRING_SIZE (127 * 3 + 1)
>  
>  /**
> @@ -1026,17 +1026,17 @@ char *usb_cache_string(struct usb_device *udev, int index)
>  		return NULL;
>  
>  	buf = kmalloc(MAX_USB_STRING_SIZE, GFP_NOIO);
> -	if (buf) {
> -		len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
> -		if (len > 0) {
> -			smallbuf = kmalloc(++len, GFP_NOIO);
> -			if (!smallbuf)
> -				return buf;
> -			memcpy(smallbuf, buf, len);
> -		}
> +	if (!buf)
> +		return NULL;
> +
> +	len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
> +	if (len <= 0) {
>  		kfree(buf);
> +		return NULL;
>  	}
> -	return smallbuf;
> +
> +	smallbuf = krealloc(buf, len + 1, GFP_NOIO);
> +	return smallbuf ? : buf;

I hate ? : except where it can only be used (i.e. in function
arguments), so please spell it out exactly what you are doing here.

Also, how was this tested?

thanks,

greg k-h

  reply	other threads:[~2026-03-12  5:02 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-11 23:06 [PATCH] USB: core: Use `krealloc()` in `usb_cache_string()` Bence Csókás via B4 Relay
2026-03-12  5:02 ` Greg Kroah-Hartman [this message]
2026-03-15  9:40   ` Bence Csókás
2026-03-15  9:47     ` Greg Kroah-Hartman
2026-03-15  9:59       ` Bence Csókás
2026-03-15 10:34         ` Bence Csókás
2026-03-15 10:47           ` Greg Kroah-Hartman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=2026031209-shawl-unshackle-f4eb@gregkh \
    --to=gregkh@linuxfoundation.org \
    --cc=bence98@sch.bme.hu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-usb@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox