linux-usb.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH -next] USB: core: Switch to use kmemdup_nul() helper
@ 2023-08-07 12:46 Ruan Jinjie
  2023-08-08  8:22 ` Greg KH
  0 siblings, 1 reply; 3+ messages in thread
From: Ruan Jinjie @ 2023-08-07 12:46 UTC (permalink / raw)
  To: gregkh, hadess, benjamin.tissoires, herve.codina, robh,
	mailhol.vincent, linux-usb
  Cc: ruanjinjie

Use kmemdup_nul() helper instead of open-coding it to simplify the code.

Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
---
 drivers/usb/core/message.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
index 0d2bfc909019..5762fd04f0d5 100644
--- a/drivers/usb/core/message.c
+++ b/drivers/usb/core/message.c
@@ -1029,10 +1029,9 @@ char *usb_cache_string(struct usb_device *udev, int index)
 	if (buf) {
 		len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
 		if (len > 0) {
-			smallbuf = kmalloc(++len, GFP_NOIO);
+			smallbuf = kmemdup_nul(buf, len, GFP_NOIO);
 			if (!smallbuf)
 				return buf;
-			memcpy(smallbuf, buf, len);
 		}
 		kfree(buf);
 	}
-- 
2.34.1


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

* Re: [PATCH -next] USB: core: Switch to use kmemdup_nul() helper
  2023-08-07 12:46 [PATCH -next] USB: core: Switch to use kmemdup_nul() helper Ruan Jinjie
@ 2023-08-08  8:22 ` Greg KH
  2023-08-08  8:57   ` Ruan Jinjie
  0 siblings, 1 reply; 3+ messages in thread
From: Greg KH @ 2023-08-08  8:22 UTC (permalink / raw)
  To: Ruan Jinjie
  Cc: hadess, benjamin.tissoires, herve.codina, robh, mailhol.vincent,
	linux-usb

On Mon, Aug 07, 2023 at 08:46:10PM +0800, Ruan Jinjie wrote:
> Use kmemdup_nul() helper instead of open-coding it to simplify the code.
> 
> Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
> ---
>  drivers/usb/core/message.c | 3 +--
>  1 file changed, 1 insertion(+), 2 deletions(-)
> 
> diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
> index 0d2bfc909019..5762fd04f0d5 100644
> --- a/drivers/usb/core/message.c
> +++ b/drivers/usb/core/message.c
> @@ -1029,10 +1029,9 @@ char *usb_cache_string(struct usb_device *udev, int index)
>  	if (buf) {
>  		len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
>  		if (len > 0) {
> -			smallbuf = kmalloc(++len, GFP_NOIO);
> +			smallbuf = kmemdup_nul(buf, len, GFP_NOIO);
>  			if (!smallbuf)
>  				return buf;
> -			memcpy(smallbuf, buf, len);

But you changed the logic here, you now added an extra \0 where the
existing code did not.  Are you sure you mean to do this?  If so, why,
and it needs to be documented in the changelog text.

What this could be is a call to kmemdup() if you really want it, but be
careful about the ++len usage...

Also, does this need to be changed at all?  How was it tested?

thanks,

greg k-h

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

* Re: [PATCH -next] USB: core: Switch to use kmemdup_nul() helper
  2023-08-08  8:22 ` Greg KH
@ 2023-08-08  8:57   ` Ruan Jinjie
  0 siblings, 0 replies; 3+ messages in thread
From: Ruan Jinjie @ 2023-08-08  8:57 UTC (permalink / raw)
  To: Greg KH
  Cc: hadess, benjamin.tissoires, herve.codina, robh, mailhol.vincent,
	linux-usb



On 2023/8/8 16:22, Greg KH wrote:
> On Mon, Aug 07, 2023 at 08:46:10PM +0800, Ruan Jinjie wrote:
>> Use kmemdup_nul() helper instead of open-coding it to simplify the code.
>>
>> Signed-off-by: Ruan Jinjie <ruanjinjie@huawei.com>
>> ---
>>  drivers/usb/core/message.c | 3 +--
>>  1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/drivers/usb/core/message.c b/drivers/usb/core/message.c
>> index 0d2bfc909019..5762fd04f0d5 100644
>> --- a/drivers/usb/core/message.c
>> +++ b/drivers/usb/core/message.c
>> @@ -1029,10 +1029,9 @@ char *usb_cache_string(struct usb_device *udev, int index)
>>  	if (buf) {
>>  		len = usb_string(udev, index, buf, MAX_USB_STRING_SIZE);
>>  		if (len > 0) {
>> -			smallbuf = kmalloc(++len, GFP_NOIO);
>> +			smallbuf = kmemdup_nul(buf, len, GFP_NOIO);
>>  			if (!smallbuf)
>>  				return buf;
>> -			memcpy(smallbuf, buf, len);
> 
> But you changed the logic here, you now added an extra \0 where the
> existing code did not.  Are you sure you mean to do this?  If so, why,
> and it needs to be documented in the changelog text.

Right! There is a problem because of the ++len, and the logic has been
changed. Sorry, I'll carefully check the patches issued in the future.

> 
> What this could be is a call to kmemdup() if you really want it, but be
> careful about the ++len usage...
> 
> Also, does this need to be changed at all?  How was it tested?

It's best to keep it as it is. Sorry, just walk through the code.

> 
> thanks,
> 
> greg k-h

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

end of thread, other threads:[~2023-08-08 20:48 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-08-07 12:46 [PATCH -next] USB: core: Switch to use kmemdup_nul() helper Ruan Jinjie
2023-08-08  8:22 ` Greg KH
2023-08-08  8:57   ` Ruan Jinjie

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).