[PATCH] tools/bootconfig: Fix null pointer when free buf

lihongtao posted 1 patch 5 days, 23 hours ago
tools/bootconfig/main.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
[PATCH] tools/bootconfig: Fix null pointer when free buf
Posted by lihongtao 5 days, 23 hours ago
In show_xbc() and delete_xbc(), if load_xbc_from_initrd failed,
the buf may be NULL.

Fixes: 950313ebf79c ("tools: bootconfig: Add bootconfig command")
Signed-off-by: lihongtao <lihongtao@kylinos.cn>
---
 tools/bootconfig/main.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
index ddabde20585f..417d07a46f92 100644
--- a/tools/bootconfig/main.c
+++ b/tools/bootconfig/main.c
@@ -328,7 +328,8 @@ static int show_xbc(const char *path, bool list)
 		xbc_show_compact_tree();
 	ret = 0;
 out:
-	free(buf);
+	if (buf)
+		free(buf);
 
 	return ret;
 }
@@ -360,7 +361,8 @@ static int delete_xbc(const char *path)
 	} /* Ignore if there is no boot config in initrd */
 
 	close(fd);
-	free(buf);
+	if (buf)
+		free(buf);
 
 	return ret;
 }
-- 
2.25.1
Re: [PATCH] tools/bootconfig: Fix null pointer when free buf
Posted by Masami Hiramatsu (Google) 5 days, 11 hours ago
On Tue, 19 May 2026 11:14:58 +0800
lihongtao <lihongtao@kylinos.cn> wrote:

> In show_xbc() and delete_xbc(), if load_xbc_from_initrd failed,
> the buf may be NULL.

NACK, because free() can handle NULL correctly. See free(3)

   free()
       The free() function frees the memory space pointed to by ptr, which must have been
       returned by a previous call to malloc() or related functions.   Otherwise,  or  if
       ptr  has already been freed, undefined behavior occurs.  If ptr is NULL, no opera‐
       tion is performed.

Thanks,

> 
> Fixes: 950313ebf79c ("tools: bootconfig: Add bootconfig command")
> Signed-off-by: lihongtao <lihongtao@kylinos.cn>
> ---
>  tools/bootconfig/main.c | 6 ++++--
>  1 file changed, 4 insertions(+), 2 deletions(-)
> 
> diff --git a/tools/bootconfig/main.c b/tools/bootconfig/main.c
> index ddabde20585f..417d07a46f92 100644
> --- a/tools/bootconfig/main.c
> +++ b/tools/bootconfig/main.c
> @@ -328,7 +328,8 @@ static int show_xbc(const char *path, bool list)
>  		xbc_show_compact_tree();
>  	ret = 0;
>  out:
> -	free(buf);
> +	if (buf)
> +		free(buf);
>  
>  	return ret;
>  }
> @@ -360,7 +361,8 @@ static int delete_xbc(const char *path)
>  	} /* Ignore if there is no boot config in initrd */
>  
>  	close(fd);
> -	free(buf);
> +	if (buf)
> +		free(buf);
>  
>  	return ret;
>  }
> -- 
> 2.25.1
> 


-- 
Masami Hiramatsu (Google) <mhiramat@kernel.org>