mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH] usb: ffs-test: add NULL check for malloc in descs_to_legacy
@ 2026-08-17  6:48 longlong yan
  2026-11-20 15:55 ` Greg KH
  0 siblings, 1 reply; 2+ messages in thread
From: longlong yan @ 2026-08-17  6:48 UTC (permalink / raw)
  To: gregkh; +Cc: linux-usb, linux-kernel, longlong yan

In descs_to_legacy(), the return value of malloc() is used without a
NULL check. If malloc() fails, dereferencing the resulting pointer
leads to a NULL pointer dereference. Add a NULL check and return 0 on
failure, consistent with the existing error handling at the start of
the function.

Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
---
 tools/usb/ffs-test.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/tools/usb/ffs-test.c b/tools/usb/ffs-test.c
index 22b938fbdfb7..5c9281cbbff1 100644
--- a/tools/usb/ffs-test.c
+++ b/tools/usb/ffs-test.c
@@ -293,6 +293,8 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
 
 		length = sizeof out->header + (descs_end - descs_start);
 		out = malloc(length);
+		if (!out)
+			return 0;
 		out->header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
 		out->header.length = cpu_to_le32(length);
 		out->header.fs_count = cpu_to_le32(fs_count);
-- 
2.43.0


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

* Re: [PATCH] usb: ffs-test: add NULL check for malloc in descs_to_legacy
  2026-08-17  6:48 [PATCH] usb: ffs-test: add NULL check for malloc in descs_to_legacy longlong yan
@ 2026-11-20 15:55 ` Greg KH
  0 siblings, 0 replies; 2+ messages in thread
From: Greg KH @ 2026-11-20 15:55 UTC (permalink / raw)
  To: longlong yan; +Cc: linux-usb, linux-kernel

On Mon, Aug 17, 2026 at 02:48:01PM +0800, longlong yan wrote:
> In descs_to_legacy(), the return value of malloc() is used without a
> NULL check. If malloc() fails, dereferencing the resulting pointer
> leads to a NULL pointer dereference. Add a NULL check and return 0 on
> failure, consistent with the existing error handling at the start of
> the function.
> 
> Signed-off-by: longlong yan <yanlonglong@kylinos.cn>
> ---
>  tools/usb/ffs-test.c | 2 ++
>  1 file changed, 2 insertions(+)
> 
> diff --git a/tools/usb/ffs-test.c b/tools/usb/ffs-test.c
> index 22b938fbdfb7..5c9281cbbff1 100644
> --- a/tools/usb/ffs-test.c
> +++ b/tools/usb/ffs-test.c
> @@ -293,6 +293,8 @@ static size_t descs_to_legacy(void **legacy, const void *descriptors_v2)
>  
>  		length = sizeof out->header + (descs_end - descs_start);
>  		out = malloc(length);
> +		if (!out)
> +			return 0;
>  		out->header.magic = cpu_to_le32(FUNCTIONFS_DESCRIPTORS_MAGIC);
>  		out->header.length = cpu_to_le32(length);
>  		out->header.fs_count = cpu_to_le32(fs_count);
> -- 
> 2.43.0
> 
> 

How did you trigger and test this?  It's userspace code, how can malloc
fail here?

thanks,

greg k-h

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

end of thread, other threads:[~2026-11-20 15:55 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-08-17  6:48 [PATCH] usb: ffs-test: add NULL check for malloc in descs_to_legacy longlong yan
2026-11-20 15:55 ` Greg KH

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

all inboxes | Powered by JetHome®