Skip to contentSkip to conversation

power: cpupower: utils: Add malloc return value check

PATCH Superseded Li kunyu 2 participants 2 messages Oct 14, 2022 – Oct 18, 2022 CPU power monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore

Li kunyu

author
#0 lore raw

PATCH 1/1 power: cpupower: utils: Add malloc return value check

Add the check and judgment statement of malloc return value.

---
 tools/power/cpupower/utils/helpers/misc.c | 4 ++++
 1 file changed, 4 insertions(+)
tools/power/cpupower/utils/helpers/misc.c +4 -0
index 9547b29254a7..eafdbd6d760c 100644
@@ -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);
Signature · 1 line
2.18.2
maintainer
#1 lore raw
Quoted 12 lines · Li kunyu

Add the check and judgment statement of malloc return value.

---
  tools/power/cpupower/utils/helpers/misc.c | 4 ++++
  1 file changed, 4 insertions(+)
tools/power/cpupower/utils/helpers/misc.c +0 -0
index 9547b29254a7..eafdbd6d760c 100644

tools/power/cpupower/utils/helpers/misc.c void print_online_cpus(void) · line 143

Quoted 5 lines
tools/power/cpupower/utils/helpers/misc.c +2 -0
@@ -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.

tools/power/cpupower/utils/helpers/misc.c void print_offline_cpus(void) · line 163

Quoted 9 lines
tools/power/cpupower/utils/helpers/misc.c +2 -0
  	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.

tools/power/cpupower/utils/helpers/misc.c void print_offline_cpus(void) · line 166

Quoted 3 lines
tools/power/cpupower/utils/helpers/misc.c +0 -0
  	if (!bitmask_isallclear(offline_cpus)) {
  		bitmask_displaylist(offline_cpus_str, str_len, offline_cpus);

thanks,
-- Shuah