2022-10-14 04:42:58

by Li kunyu

[permalink] [raw]
Subject: [PATCH] power: cpupower: utils: Add malloc return value check

Add the check and judgment statement of malloc return value.

Signed-off-by: Li kunyu <[email protected]>
---
tools/power/cpupower/utils/helpers/misc.c | 4 ++++
1 file changed, 4 insertions(+)

diff --git a/tools/power/cpupower/utils/helpers/misc.c b/tools/power/cpupower/utils/helpers/misc.c
index 9547b29254a7..eafdbd6d760c 100644
--- a/tools/power/cpupower/utils/helpers/misc.c
+++ b/tools/power/cpupower/utils/helpers/misc.c
@@ -139,6 +139,8 @@ void print_online_cpus(void)

str_len = online_cpus->size * 5;
online_cpus_str = (void *)malloc(sizeof(char) * str_len);
+ if (unlikely(!online_cpus_str))
+ return;

if (!bitmask_isallclear(online_cpus)) {
bitmask_displaylist(online_cpus_str, str_len, online_cpus);
@@ -157,6 +159,8 @@ void print_offline_cpus(void)

str_len = offline_cpus->size * 5;
offline_cpus_str = (void *)malloc(sizeof(char) * str_len);
+ if (unlikely(!offline_cpus_str))
+ return;

if (!bitmask_isallclear(offline_cpus)) {
bitmask_displaylist(offline_cpus_str, str_len, offline_cpus);
--
2.18.2


2022-10-18 21:21:33

by Shuah Khan

[permalink] [raw]
Subject: Re: [PATCH] power: cpupower: utils: Add malloc return value check

On 10/13/22 22:29, Li kunyu wrote:
> Add the check and judgment statement of malloc return value.
>
> Signed-off-by: Li kunyu <[email protected]>
> ---
> tools/power/cpupower/utils/helpers/misc.c | 4 ++++
> 1 file changed, 4 insertions(+)
>
> diff --git a/tools/power/cpupower/utils/helpers/misc.c b/tools/power/cpupower/utils/helpers/misc.c
> index 9547b29254a7..eafdbd6d760c 100644
> --- a/tools/power/cpupower/utils/helpers/misc.c
> +++ b/tools/power/cpupower/utils/helpers/misc.c
> @@ -139,6 +139,8 @@ void print_online_cpus(void)
>
> str_len = online_cpus->size * 5;
> online_cpus_str = (void *)malloc(sizeof(char) * str_len);
> + if (unlikely(!online_cpus_str))
> + return;

You don't need unlikely here.

>
> if (!bitmask_isallclear(online_cpus)) {
> bitmask_displaylist(online_cpus_str, str_len, online_cpus);
> @@ -157,6 +159,8 @@ void print_offline_cpus(void)
>
> str_len = offline_cpus->size * 5;
> offline_cpus_str = (void *)malloc(sizeof(char) * str_len);
> + if (unlikely(!offline_cpus_str))
> + return;

You don't need unlikely here.

>
> if (!bitmask_isallclear(offline_cpus)) {
> bitmask_displaylist(offline_cpus_str, str_len, offline_cpus);

thanks,
-- Shuah