]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
tools/cpupower: Use strcspn() to strip trailing newline
authorKaushlendra Kumar <kaushlendra.kumar@intel.com>
Thu, 27 Nov 2025 04:45:36 +0000 (10:15 +0530)
committerShuah Khan <skhan@linuxfoundation.org>
Mon, 15 Dec 2025 19:33:28 +0000 (12:33 -0700)
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>
tools/power/cpupower/lib/cpuidle.c

index f2c1139adf71692155966f2c30a7e43b6b1af37b..6a881d93d2e991b7aea0b03abf64ab92a4772728 100644 (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;
 }