diff mbox series

pm: cpupower: bench: Prevent NULL dereference on malloc failure

Message ID 20250219122715.3892223-1-quic_zhonhan@quicinc.com (mailing list archive)
State Accepted
Headers show
Series pm: cpupower: bench: Prevent NULL dereference on malloc failure | expand

Commit Message

Zhongqiu Han Feb. 19, 2025, 12:27 p.m. UTC
If malloc returns NULL due to low memory, 'config' pointer can be NULL.
Add a check to prevent NULL dereference.

Signed-off-by: Zhongqiu Han <quic_zhonhan@quicinc.com>
---
 tools/power/cpupower/bench/parse.c | 4 ++++
 1 file changed, 4 insertions(+)

Comments

Shuah Khan Feb. 19, 2025, 8:51 p.m. UTC | #1
On 2/19/25 05:27, Zhongqiu Han wrote:
> If malloc returns NULL due to low memory, 'config' pointer can be NULL.
> Add a check to prevent NULL dereference.
> 
> Signed-off-by: Zhongqiu Han <quic_zhonhan@quicinc.com>
> ---
>   tools/power/cpupower/bench/parse.c | 4 ++++
>   1 file changed, 4 insertions(+)
> 
> diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c
> index 080678d9d74e..bd67c758b33a 100644
> --- a/tools/power/cpupower/bench/parse.c
> +++ b/tools/power/cpupower/bench/parse.c
> @@ -121,6 +121,10 @@ FILE *prepare_output(const char *dirname)
>   struct config *prepare_default_config()
>   {
>   	struct config *config = malloc(sizeof(struct config));
> +	if (!config) {
> +		perror("malloc");
> +		return NULL;
> +	}
>   
>   	dprintf("loading defaults\n");
>   

Thanks. Applied for Linux 6.15-rc1 - will be included in pull request
to PM maintainer.

https://git.kernel.org/pub/scm/linux/kernel/git/shuah/linux.git/log/?h=cpupower

thanks,
-- Shuah
diff mbox series

Patch

diff --git a/tools/power/cpupower/bench/parse.c b/tools/power/cpupower/bench/parse.c
index 080678d9d74e..bd67c758b33a 100644
--- a/tools/power/cpupower/bench/parse.c
+++ b/tools/power/cpupower/bench/parse.c
@@ -121,6 +121,10 @@  FILE *prepare_output(const char *dirname)
 struct config *prepare_default_config()
 {
 	struct config *config = malloc(sizeof(struct config));
+	if (!config) {
+		perror("malloc");
+		return NULL;
+	}
 
 	dprintf("loading defaults\n");