public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] init/initramfs.c: check the return value of kstrdup()
@ 2022-03-04  9:27 xkernel.wang
  2022-03-04 14:14 ` Greg KH
  2022-03-07  1:28 ` Andrew Morton
  0 siblings, 2 replies; 5+ messages in thread
From: xkernel.wang @ 2022-03-04  9:27 UTC (permalink / raw)
  To: linux, akpm, pombredanne, gregkh, arnd, luc.vanoostenryck
  Cc: linux-kernel, Xiaoke Wang

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

kstrdup() is also a memory allocation function which is similar
with kmalloc() in some way. Once some internal memory errors
happen, it will return NULL. It is better to check the return
value of it so to catch the memory error in time.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
 init/initramfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/init/initramfs.c b/init/initramfs.c
index a842c05..49deffb 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -139,8 +139,12 @@ static void __init dir_add(const char *name, time64_t mtime)
 	struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
 	if (!de)
 		panic_show_mem("can't allocate dir_entry buffer");
-	INIT_LIST_HEAD(&de->list);
 	de->name = kstrdup(name, GFP_KERNEL);
+	if (!de->name) {
+		kfree(de);
+		panic_show_mem("can't duplicate dir name");
+	}
+	INIT_LIST_HEAD(&de->list);
 	de->mtime = mtime;
 	list_add(&de->list, &dir_list);
 }
-- 

^ permalink raw reply related	[flat|nested] 5+ messages in thread
* [PATCH] init/initramfs.c: check the return value of kstrdup()
@ 2021-12-13  8:58 Xiaoke Wang
  0 siblings, 0 replies; 5+ messages in thread
From: Xiaoke Wang @ 2021-12-13  8:58 UTC (permalink / raw)
  To: linux-kernel; +Cc: Xiaoke Wang

kstrdup() is also a memory allocation function and it is similar
with kmalloc() in some way. Once some internal memory errors
happen, it will return NULL. It is better to check the return
value of it so to catch the memory error in time.

Signed-off-by: Xiaoke Wang <xkernel.wang@foxmail.com>
---
 init/initramfs.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/init/initramfs.c b/init/initramfs.c
index a842c05..49deffb 100644
--- a/init/initramfs.c
+++ b/init/initramfs.c
@@ -139,8 +139,12 @@ static void __init dir_add(const char *name, time64_t mtime)
 	struct dir_entry *de = kmalloc(sizeof(struct dir_entry), GFP_KERNEL);
 	if (!de)
 		panic_show_mem("can't allocate dir_entry buffer");
-	INIT_LIST_HEAD(&de->list);
 	de->name = kstrdup(name, GFP_KERNEL);
+	if (!de->name) {
+		kfree(de);
+		panic_show_mem("can't duplicate dir name");
+	}
+	INIT_LIST_HEAD(&de->list);
 	de->mtime = mtime;
 	list_add(&de->list, &dir_list);
 }
-- 

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

end of thread, other threads:[~2022-03-07  1:29 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-03-04  9:27 [PATCH] init/initramfs.c: check the return value of kstrdup() xkernel.wang
2022-03-04 14:14 ` Greg KH
2022-03-04 15:55   ` Xiaoke Wang
2022-03-07  1:28 ` Andrew Morton
  -- strict thread matches above, loose matches on Subject: below --
2021-12-13  8:58 Xiaoke Wang

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