Skip to contentSkip to conversation

tools/cpupower: Fix incorrect size in cpuidle_state_disable()

PATCH Superseded Kaushlendra Kumar 2 participants 3 messages Aug 26, 2025 – Sep 17, 2025 CPU power monitoring

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 tools/cpupower: Fix incorrect size in cpuidle_state_disable()

Fix incorrect size parameter passed to cpuidle_state_write_file() in cpuidle_state_disable().

The function was incorrectly using sizeof(disable) which returns the size of the unsigned int variable (4 bytes) instead of the actual length of the string stored in the 'value' buffer.

Since 'value' is populated with snprintf() to contain the string representation of the disable value, we should use strlen(value) to get the correct string length for writing to the sysfs file.

This ensures the correct number of bytes is written to the cpuidle state disable file in sysfs.

---
 tools/power/cpupower/lib/cpuidle.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/cpupower/lib/cpuidle.c +1 -1
index 0ecac009273c..2180e63c963a 100644
@@ -244,7 +244,7 @@ int cpuidle_state_disable(unsigned int cpu,
 	snprintf(value, SYSFS_PATH_MAX, "%u", disable);
 
 	bytes_written = cpuidle_state_write_file(cpu, idlestate, "disable",
-						   value, sizeof(disable));
+						   value, strlen(value));
 	if (bytes_written)
 		return 0;
 	return -3;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 24 lines · Kaushlendra Kumar

Fix incorrect size parameter passed to cpuidle_state_write_file() in cpuidle_state_disable().

The function was incorrectly using sizeof(disable) which returns the size of the unsigned int variable (4 bytes) instead of the actual length of the string stored in the 'value' buffer.

Since 'value' is populated with snprintf() to contain the string representation of the disable value, we should use strlen(value) to get the correct string length for writing to the sysfs file.

This ensures the correct number of bytes is written to the cpuidle state disable file in sysfs.

---
  tools/power/cpupower/lib/cpuidle.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/cpupower/lib/cpuidle.c +0 -0
index 0ecac009273c..2180e63c963a 100644

tools/power/cpupower/lib/cpuidle.c int cpuidle_state_disable(unsigned int cpu, · line 244

Quoted 1 line
tools/power/cpupower/lib/cpuidle.c +0 -0
@@ -244,7 +244,7 @@ int cpuidle_state_disable(unsigned int cpu,
  	snprintf(value, SYSFS_PATH_MAX, "%u", disable);

Can't you get the length from snprintf return? You can avoid calling strlen()

tools/power/cpupower/lib/cpuidle.c int cpuidle_state_disable(unsigned int cpu, · line 250

Quoted 7 lines
tools/power/cpupower/lib/cpuidle.c +1 -1
  	bytes_written = cpuidle_state_write_file(cpu, idlestate, "disable",
-						   value, sizeof(disable));
+						   value, strlen(value));
  	if (bytes_written)
  		return 0;
  	return -3;

thanks,
-- Shuah

#2 lore raw
Quoted 32 lines · Kaushlendra Kumar
Quoted 25 lines

Fix incorrect size parameter passed to cpuidle_state_write_file() in cpuidle_state_disable().

The function was incorrectly using sizeof(disable) which returns the size of the unsigned int variable (4 bytes) instead of the actual length of the string stored in the 'value' buffer.

Since 'value' is populated with snprintf() to contain the string representation of the disable value, we should use strlen(value) to get the correct string length for writing to the sysfs file.

This ensures the correct number of bytes is written to the cpuidle state disable file in sysfs.

---
  tools/power/cpupower/lib/cpuidle.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)
tools/power/cpupower/lib/cpuidle.c +0 -0
index 0ecac009273c..2180e63c963a 100644

tools/power/cpupower/lib/cpuidle.c int cpuidle_state_disable(unsigned int cpu, · line 244

Quoted 1 line
tools/power/cpupower/lib/cpuidle.c +0 -0
@@ -244,7 +244,7 @@ int cpuidle_state_disable(unsigned int cpu,
  	snprintf(value, SYSFS_PATH_MAX, "%u", disable);

Can't you get the length from snprintf return? You can avoid calling strlen()

Quoted 1 line
Quoted 9 lines

tools/power/cpupower/lib/cpuidle.c int cpuidle_state_disable(unsigned int cpu, · line 250

Quoted 6 lines

bytes_written = cpuidle_state_write_file(cpu, idlestate, "disable",

tools/power/cpupower/lib/cpuidle.c +1 -1
-						   value, sizeof(disable));
+						   value, strlen(value));
  	if (bytes_written)
  		return 0;
  	return -3;

thanks,
-- Shuah

Hi Shuah,

Thank you for the suggestion! You are absolutely correct - using the return value from snprintf() is more efficient and eliminates the need for an additional strlen() call.

I have sent a v2 patch that captures the snprintf() return value and uses it directly.

Thank you for the review and the optimization suggestion.

Best regards,
Kaushlendra