2022-03-25 18:46:27

by Xiaoke Wang

[permalink] [raw]
Subject: [PATCH] mips: sgi-ip22: add a check for the return of kzalloc()

From: Xiaoke Wang <[email protected]>

kzalloc() is a memory allocation function which can return NULL when
some internal memory errors happen. So it is better to check it to
prevent potential wrong memory access.

Signed-off-by: Xiaoke Wang <[email protected]>
---
arch/mips/sgi-ip22/ip22-gio.c | 2 ++
1 file changed, 2 insertions(+)

diff --git a/arch/mips/sgi-ip22/ip22-gio.c b/arch/mips/sgi-ip22/ip22-gio.c
index dfc52f6..38d12f4 100644
--- a/arch/mips/sgi-ip22/ip22-gio.c
+++ b/arch/mips/sgi-ip22/ip22-gio.c
@@ -363,6 +363,8 @@ static void ip22_check_gio(int slotno, unsigned long addr, int irq)
printk(KERN_INFO "GIO: slot %d : %s (id %x)\n",
slotno, name, id);
gio_dev = kzalloc(sizeof *gio_dev, GFP_KERNEL);
+ if (!gio_dev)
+ return;
gio_dev->name = name;
gio_dev->slotno = slotno;
gio_dev->id.id = id;
--


2022-03-31 04:30:06

by Thomas Bogendoerfer

[permalink] [raw]
Subject: Re: [PATCH] mips: sgi-ip22: add a check for the return of kzalloc()

On Fri, Mar 25, 2022 at 06:57:18PM +0800, [email protected] wrote:
> From: Xiaoke Wang <[email protected]>
>
> kzalloc() is a memory allocation function which can return NULL when
> some internal memory errors happen. So it is better to check it to
> prevent potential wrong memory access.
>
> Signed-off-by: Xiaoke Wang <[email protected]>
> ---
> arch/mips/sgi-ip22/ip22-gio.c | 2 ++
> 1 file changed, 2 insertions(+)
>
> diff --git a/arch/mips/sgi-ip22/ip22-gio.c b/arch/mips/sgi-ip22/ip22-gio.c
> index dfc52f6..38d12f4 100644
> --- a/arch/mips/sgi-ip22/ip22-gio.c
> +++ b/arch/mips/sgi-ip22/ip22-gio.c
> @@ -363,6 +363,8 @@ static void ip22_check_gio(int slotno, unsigned long addr, int irq)
> printk(KERN_INFO "GIO: slot %d : %s (id %x)\n",
> slotno, name, id);
> gio_dev = kzalloc(sizeof *gio_dev, GFP_KERNEL);
> + if (!gio_dev)
> + return;
> gio_dev->name = name;
> gio_dev->slotno = slotno;
> gio_dev->id.id = id;
> --

applied to mips-next.

Thomas.

--
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea. [ RFC1925, 2.3 ]