lkml.org 
[lkml]   [2022]   [Mar]   [4]   [last100]   RSS Feed
Views: [wrap][no wrap]   [headers]  [forward] 
 
Messages in this thread
/
Date
From
SubjectRe: [PATCH] init/initramfs.c: check the return value of kstrdup()
On Fri, Mar 04, 2022 at 05:27:34PM +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.
>
> 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) {

How can this fail? Have you ever hit this in real life?

> + kfree(de);
> + panic_show_mem("can't duplicate dir name");

Why are you freeing memory if you are panicing?

How was this tested?

thanks,

greg k-h

\
 
 \ /
  Last update: 2022-03-04 15:14    [W:0.144 / U:0.124 seconds]
©2003-2020 Jasper Spaans|hosted at Digital Ocean and TransIP|Read the blog|Advertise on this site