tools/cpupower: Use strcspn() to strip trailing newline

Replace manual newline removal with strcspn() which is safer and
cleaner. This avoids potential out-of-bounds access on empty strings
and handles the case where no newline exists.

Link: https://lore.kernel.org/r/20251127044536.715722-1-kaushlendra.kumar@intel.com
Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
Signed-off-by: Shuah Khan <skhan@linuxfoundation.org>
This commit is contained in:
Kaushlendra Kumar 2025-11-27 10:15:36 +05:30 committed by Shuah Khan
parent 24858a8416
commit 1b9aaf36b7
1 changed files with 2 additions and 4 deletions

View File

@ -193,8 +193,7 @@ static char *cpuidle_state_get_one_string(unsigned int cpu,
if (result == NULL)
return NULL;
if (result[strlen(result) - 1] == '\n')
result[strlen(result) - 1] = '\0';
result[strcspn(result, "\n")] = '\0';
return result;
}
@ -366,8 +365,7 @@ static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which)
if (result == NULL)
return NULL;
if (result[strlen(result) - 1] == '\n')
result[strlen(result) - 1] = '\0';
result[strcspn(result, "\n")] = '\0';
return result;
}