Skip to content

Commit 1b9aaf3

Browse files
kaushlenshuahkh
authored andcommitted
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>
1 parent 24858a8 commit 1b9aaf3

1 file changed

Lines changed: 2 additions & 4 deletions

File tree

tools/power/cpupower/lib/cpuidle.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,7 @@ static char *cpuidle_state_get_one_string(unsigned int cpu,
193193
if (result == NULL)
194194
return NULL;
195195

196-
if (result[strlen(result) - 1] == '\n')
197-
result[strlen(result) - 1] = '\0';
196+
result[strcspn(result, "\n")] = '\0';
198197

199198
return result;
200199
}
@@ -366,8 +365,7 @@ static char *sysfs_cpuidle_get_one_string(enum cpuidle_string which)
366365
if (result == NULL)
367366
return NULL;
368367

369-
if (result[strlen(result) - 1] == '\n')
370-
result[strlen(result) - 1] = '\0';
368+
result[strcspn(result, "\n")] = '\0';
371369

372370
return result;
373371
}

0 commit comments

Comments
 (0)