public inbox for linux-pm@vger.kernel.org
 help / color / mirror / Atom feed
From: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
To: shuah@kernel.org, jwyatt@redhat.com, jkacur@redhat.com
Cc: linux-pm@vger.kernel.org,
	Kaushlendra Kumar <kaushlendra.kumar@intel.com>
Subject: [PATCH] cpupower: replace atoi() with validated parse_int_range() helper
Date: Tue, 31 Mar 2026 09:58:59 +0530	[thread overview]
Message-ID: <20260331042859.1920369-1-kaushlendra.kumar@intel.com> (raw)

atoi() provides no error detection for invalid input or out-of-range
values. Introduce parse_int_range(), which uses strtol() with full
errno and end-pointer checking, returning -EINVAL on parse failure
and -ERANGE when the value falls outside the caller-supplied [min,
max] bounds.

Switch --perf-bias and --turbo-boost option parsing to use this
helper. Also move error messages from printf() to fprintf(stderr)
to follow standard CLI conventions for diagnostic output.

Signed-off-by: Kaushlendra Kumar <kaushlendra.kumar@intel.com>
---
 tools/power/cpupower/utils/cpupower-set.c | 29 +++++++++++++++++------
 1 file changed, 22 insertions(+), 7 deletions(-)

diff --git a/tools/power/cpupower/utils/cpupower-set.c b/tools/power/cpupower/utils/cpupower-set.c
index 550a942e72ce..dcbdff5c9ae5 100644
--- a/tools/power/cpupower/utils/cpupower-set.c
+++ b/tools/power/cpupower/utils/cpupower-set.c
@@ -31,6 +31,24 @@ static void print_wrong_arg_exit(void)
 	exit(EXIT_FAILURE);
 }
 
+/* Safe integer parsing with range validation. Returns 0 on success,
+ * -EINVAL on parse error, -ERANGE if value is outside [min, max].
+ */
+static int parse_int_range(const char *arg, int min, int max, int *out)
+{
+	char *end = NULL;
+	long val;
+
+	errno = 0;
+	val = strtol(arg, &end, 10);
+	if (errno || end == arg || *end != '\0')
+		return -EINVAL;
+	if (val < min || val > max)
+		return -ERANGE;
+	*out = (int)val;
+	return 0;
+}
+
 int cmd_set(int argc, char **argv)
 {
 	extern char *optarg;
@@ -69,10 +87,8 @@ int cmd_set(int argc, char **argv)
 		case 'b':
 			if (params.perf_bias)
 				print_wrong_arg_exit();
-			perf_bias = atoi(optarg);
-			if (perf_bias < 0 || perf_bias > 15) {
-				printf(_("--perf-bias param out "
-					 "of range [0-%d]\n"), 15);
+			if (parse_int_range(optarg, 0, 15, &perf_bias)) {
+				fprintf(stderr, _("--perf-bias param out of range [0-%d]\n"), 15);
 				print_wrong_arg_exit();
 			}
 			params.perf_bias = 1;
@@ -100,9 +116,8 @@ int cmd_set(int argc, char **argv)
 		case 't':
 			if (params.turbo_boost)
 				print_wrong_arg_exit();
-			turbo_boost = atoi(optarg);
-			if (turbo_boost < 0 || turbo_boost > 1) {
-				printf("--turbo-boost param out of range [0-1]\n");
+			if (parse_int_range(optarg, 0, 1, &turbo_boost)) {
+				fprintf(stderr, "--turbo-boost param out of range [0-1]\n");
 				print_wrong_arg_exit();
 			}
 			params.turbo_boost = 1;
-- 
2.34.1


             reply	other threads:[~2026-03-31  4:32 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-31  4:28 Kaushlendra Kumar [this message]
2026-03-31 23:53 ` [PATCH] cpupower: replace atoi() with validated parse_int_range() helper Shuah Khan
2026-04-01  2:57   ` Kumar, Kaushlendra

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260331042859.1920369-1-kaushlendra.kumar@intel.com \
    --to=kaushlendra.kumar@intel.com \
    --cc=jkacur@redhat.com \
    --cc=jwyatt@redhat.com \
    --cc=linux-pm@vger.kernel.org \
    --cc=shuah@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox