ARM: mvebu: potential dereference of null pointer

Message ID 202301171822040406304@zte.com.cn
State New
Headers
Series ARM: mvebu: potential dereference of null pointer |

Commit Message

ye.xingchen@zte.com.cn Jan. 17, 2023, 10:22 a.m. UTC
  From: Minghao Chi <chi.minghao@zte.com.cn>

The return value of kzalloc() needs to be checked.
To avoid use of null pointer in case of the failure of alloc.

Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: Minghao Chi <chi.minghao@zte.com.cn>
Signed-off-by: ye xingchen <ye.xingchen@zte.com.cn>
---
 arch/arm/mach-mvebu/board-v7.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
  

Comments

Andrew Lunn Jan. 18, 2023, 1:17 p.m. UTC | #1
On Tue, Jan 17, 2023 at 06:22:04PM +0800, ye.xingchen@zte.com.cn wrote:
> From: Minghao Chi <chi.minghao@zte.com.cn>
> 
> The return value of kzalloc() needs to be checked.
> To avoid use of null pointer in case of the failure of alloc.
> 
> Reported-by: Zeal Robot <zealci@zte.com.cn>
> Signed-off-by: Minghao Chi <chi.minghao@zte.com.cn>
> Signed-off-by: ye xingchen <ye.xingchen@zte.com.cn>
> ---
>  arch/arm/mach-mvebu/board-v7.c | 3 ++-
>  1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/arch/arm/mach-mvebu/board-v7.c b/arch/arm/mach-mvebu/board-v7.c
> index fd5d0c8ff695..b93b4a8a8357 100644
> --- a/arch/arm/mach-mvebu/board-v7.c
> +++ b/arch/arm/mach-mvebu/board-v7.c
> @@ -125,7 +125,8 @@ static void __init i2c_quirk(void)
>  		struct property *new_compat;
> 
>  		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
> -
> +		if (!new_compat)
> +			return;
>  		new_compat->name = kstrdup("compatible", GFP_KERNEL);
>  		new_compat->length = sizeof("marvell,mv78230-a0-i2c");
>  		new_compat->value = kstrdup("marvell,mv78230-a0-i2c",

Please add a blank line after the return statement.

Also, i find it interested your bot is telling you about kzalloc, but
totally ignoring kstrdup().

	Andrew
  

Patch

diff --git a/arch/arm/mach-mvebu/board-v7.c b/arch/arm/mach-mvebu/board-v7.c
index fd5d0c8ff695..b93b4a8a8357 100644
--- a/arch/arm/mach-mvebu/board-v7.c
+++ b/arch/arm/mach-mvebu/board-v7.c
@@ -125,7 +125,8 @@  static void __init i2c_quirk(void)
 		struct property *new_compat;

 		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
-
+		if (!new_compat)
+			return;
 		new_compat->name = kstrdup("compatible", GFP_KERNEL);
 		new_compat->length = sizeof("marvell,mv78230-a0-i2c");
 		new_compat->value = kstrdup("marvell,mv78230-a0-i2c",