Skip to contentSkip to conversation

tools/cpupower: Fix incorrect size in cpuidle_state_disable()

PATCH In review Kaushlendra Kumar 2 participants 2 messages Sep 17, 2025 – Sep 25, 2025 CPU power monitoring

lore
Share & tools
lore
#0 lore raw

PATCH v2 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 the length returned by snprintf() 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.

---
Changes in v2:
- Use snprintf() return value instead of strlen() for better efficiency

 tools/power/cpupower/lib/cpuidle.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
tools/power/cpupower/lib/cpuidle.c +3 -2
index 0ecac009273c..f2c1139adf71 100644
@@ -233,6 +233,7 @@ int cpuidle_state_disable(unsigned int cpu,
 {
 	char value[SYSFS_PATH_MAX];
 	int bytes_written;
+	int len;
 
 	if (cpuidle_state_count(cpu) <= idlestate)
 		return -1;
@@ -241,10 +242,10 @@ int cpuidle_state_disable(unsigned int cpu,
 				 idlestate_value_files[IDLESTATE_DISABLE]))
 		return -2;
 
-	snprintf(value, SYSFS_PATH_MAX, "%u", disable);
+	len = snprintf(value, SYSFS_PATH_MAX, "%u", disable);
 
 	bytes_written = cpuidle_state_write_file(cpu, idlestate, "disable",
-						   value, sizeof(disable));
+						   value, len);
 	if (bytes_written)
 		return 0;
 	return -3;
Signature · 1 line
2.34.1
maintainer
#1 lore raw
Quoted 12 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 the length returned by snprintf() to get the correct string length for writing to the sysfs file.

Quoted 8 lines

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

---
Changes in v2:
- Use snprintf() return value instead of strlen() for better efficiency

Thank you. Applied to git.kernel.org/pub/scm/linux/kernel/git/shuah/linux.git/log/?h=cpupower for next PR to PM maintainer.

thanks,
-- Shuah