linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: xkernel.wang@foxmail.com
To: linux@dominikbrodowski.net, akpm@linux-foundation.org,
	pombredanne@nexb.com, gregkh@linuxfoundation.org, arnd@arndb.de,
	luc.vanoostenryck@gmail.com
Cc: linux-kernel@vger.kernel.org, Xiaoke Wang <xkernel.wang@foxmail.com>
Subject: [PATCH] init/initramfs.c: check the return value of kstrdup()
Date: Fri,  4 Mar 2022 17:27:34 +0800	[thread overview]
Message-ID: <tencent_DE1C3D1422E1DD4A35FFDE2048CC48B94E08@qq.com> (raw)

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);
 }
-- 

             reply	other threads:[~2022-03-04  9:28 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-04  9:27 xkernel.wang [this message]
2022-03-04 14:14 ` [PATCH] init/initramfs.c: check the return value of kstrdup() 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

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_DE1C3D1422E1DD4A35FFDE2048CC48B94E08@qq.com \
    --to=xkernel.wang@foxmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=arnd@arndb.de \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@dominikbrodowski.net \
    --cc=luc.vanoostenryck@gmail.com \
    --cc=pombredanne@nexb.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).