linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: xkernel.wang@foxmail.com
To: andrew@lunn.ch, gregory.clement@bootlin.com,
	sebastian.hesselbarth@gmail.com
Cc: linux@armlinux.org.uk, linux-arm-kernel@lists.infradead.org,
	linux-kernel@vger.kernel.org,
	Xiaoke Wang <xkernel.wang@foxmail.com>
Subject: [PATCH] ARM: mvebu: check the validation of memory allocation
Date: Fri, 25 Mar 2022 16:55:18 +0800	[thread overview]
Message-ID: <tencent_1D9E7394538085872BE9FD6780483137E70A@qq.com> (raw)

From: Xiaoke Wang <xkernel.wang@foxmail.com>

kzalloc() and kstrdup() are memory allocation functions which can return
NULL when some internal memory errors happen. So it is better to check
the return value of them to prevent potential wrong memory access or
memory leak.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
 arch/arm/mach-mvebu/board-v7.c  | 8 ++++++++
 arch/arm/mach-mvebu/coherency.c | 6 ++++++
 2 files changed, 14 insertions(+)

diff --git a/arch/arm/mach-mvebu/board-v7.c b/arch/arm/mach-mvebu/board-v7.c
index d2df5ef..86d1f4e 100644
--- a/arch/arm/mach-mvebu/board-v7.c
+++ b/arch/arm/mach-mvebu/board-v7.c
@@ -128,11 +128,19 @@ static void __init i2c_quirk(void)
 		struct property *new_compat;
 
 		new_compat = kzalloc(sizeof(*new_compat), GFP_KERNEL);
+		if (!new_compat)
+			continue;
 
 		new_compat->name = kstrdup("compatible", GFP_KERNEL);
 		new_compat->length = sizeof("marvell,mv78230-a0-i2c");
 		new_compat->value = kstrdup("marvell,mv78230-a0-i2c",
 						GFP_KERNEL);
+		if (!new_compat->name || !new_compat->value) {
+			kfree(new_compat->name);
+			kfree(new_compat->value);
+			kfree(new_compat);
+			continue;
+		}
 
 		of_update_property(np, new_compat);
 	}
diff --git a/arch/arm/mach-mvebu/coherency.c b/arch/arm/mach-mvebu/coherency.c
index 49e3c8d..eb6b349 100644
--- a/arch/arm/mach-mvebu/coherency.c
+++ b/arch/arm/mach-mvebu/coherency.c
@@ -194,7 +194,13 @@ static void __init armada_375_380_coherency_init(struct device_node *np)
 		struct property *p;
 
 		p = kzalloc(sizeof(*p), GFP_KERNEL);
+		if (!p)
+			continue;
 		p->name = kstrdup("arm,io-coherent", GFP_KERNEL);
+		if (!p->name) {
+			kfree(p);
+			continue;
+		}
 		of_add_property(cache_dn, p);
 	}
 }
-- 

             reply	other threads:[~2022-03-25  8:56 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-25  8:55 xkernel.wang [this message]
2022-04-29 15:19 ` [PATCH] ARM: mvebu: check the validation of memory allocation Russell King (Oracle)
2022-05-01  7:01   ` xkernel.wang

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=tencent_1D9E7394538085872BE9FD6780483137E70A@qq.com \
    --to=xkernel.wang@foxmail.com \
    --cc=andrew@lunn.ch \
    --cc=gregory.clement@bootlin.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=sebastian.hesselbarth@gmail.com \
    /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;
as well as URLs for NNTP newsgroup(s).