tools/cpupower: Fix incorrect size in cpuidle_state_disable()
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
@@ -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
Shuah Khan
maintainerQuoted 24 lines · Kaushlendra Kumar
Fix incorrect size parameter passed to
cpuidle_state_write_file()incpuidle_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.
Signed-off-by Kaushlendra Kumar <[email protected]>--- tools/power/cpupower/lib/cpuidle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/power/cpupower/lib/cpuidle.c +0
-0
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
-1bytes_written =cpuidle_state_write_file (cpu, idlestate,"disable" , - value,sizeof (disable)); + value,strlen (value));if (bytes_written)return 0 ;return -3 ;
thanks,
-- Shuah
Quoted 32 lines · Kaushlendra Kumar
Quoted 25 lines
Fix incorrect size parameter passed to
cpuidle_state_write_file()incpuidle_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.
Signed-off-by Kaushlendra Kumar <[email protected]>--- tools/power/cpupower/lib/cpuidle.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)tools/power/cpupower/lib/cpuidle.c +0
-0tools/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