From: Andrew Morton <akpm@linux-foundation.org>
To: xkernel.wang@foxmail.com
Cc: linux@dominikbrodowski.net, pombredanne@nexb.com,
gregkh@linuxfoundation.org, arnd@arndb.de,
luc.vanoostenryck@gmail.com, linux-kernel@vger.kernel.org
Subject: Re: [PATCH] init/initramfs.c: check the return value of kstrdup()
Date: Sun, 6 Mar 2022 17:28:59 -0800 [thread overview]
Message-ID: <20220306172859.2a35d07284b6aee209b9dcfb@linux-foundation.org> (raw)
In-Reply-To: <tencent_DE1C3D1422E1DD4A35FFDE2048CC48B94E08@qq.com>
On Fri, 4 Mar 2022 17:27:34 +0800 xkernel.wang@foxmail.com wrote:
> 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.
>
> ...
>
> --- 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);
We often assume that memory allocations cannot fail in __init code. If
the kernel runs out of memory at this stage, we have very deep problems
and it's virtually impossible that execution would have got this far.
next prev parent reply other threads:[~2022-03-07 1:29 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-03-04 9:27 xkernel.wang
2022-03-04 14:14 ` Greg KH
2022-03-04 15:55 ` Xiaoke Wang
2022-03-07 1:28 ` Andrew Morton [this message]
-- 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=20220306172859.2a35d07284b6aee209b9dcfb@linux-foundation.org \
--to=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 \
--cc=xkernel.wang@foxmail.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
all inboxes | Powered by JetHome®