All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] cpuidle_set accepts alpha numeric values for idle-set operation
@ 2021-01-05 12:24 Brahadambal Srinivasan
  2021-01-05 18:03 ` Shuah Khan
  0 siblings, 1 reply; 4+ messages in thread
From: Brahadambal Srinivasan @ 2021-01-05 12:24 UTC (permalink / raw)
  To: shuah, trenn; +Cc: linux-pm, linux-kernel, latha

For both the d and e options in cpuidle_set, an atoi() conversion is
done without checking if the input argument is all numeric. So, an
atoi conversion is done on any character provided as input and the
CPU idle_set operation continues with that integer value, which may
not be what is intended or entirely correct.

A similar check is present for cpufreq-set already.

This patch adds a check to see that the idle_set value is all numeric
before doing a string-to-int conversion.

Signed-off-by: Brahadambal Srinivasan <latha@linux.vnet.ibm.com>
---
 tools/power/cpupower/utils/cpuidle-set.c | 39 +++++++++++++++++++++---
 1 file changed, 34 insertions(+), 5 deletions(-)

diff --git a/tools/power/cpupower/utils/cpuidle-set.c b/tools/power/cpupower/utils/cpuidle-set.c
index 46158928f9ad..b3dec48e7141 100644
--- a/tools/power/cpupower/utils/cpuidle-set.c
+++ b/tools/power/cpupower/utils/cpuidle-set.c
@@ -21,6 +21,19 @@ static struct option info_opts[] = {
      { },
 };
 
+int is_number(char *arg)
+{
+	size_t len, i = 0;
+
+	len = strlen(arg);
+
+	for (i = 0; i < len; i++) {
+		if (!isdigit(arg[i]))
+			return 0;
+	}
+
+	return 1;
+}
 
 int cmd_idle_set(int argc, char **argv)
 {
@@ -47,7 +60,12 @@ int cmd_idle_set(int argc, char **argv)
 				break;
 			}
 			param = ret;
-			idlestate = atoi(optarg);
+			if (is_number(optarg))
+				idlestate = atoi(optarg);
+			else {
+				printf(_("Bad idle_set value : %s. Integer expected\n"), optarg);
+				exit(EXIT_FAILURE);
+			}
 			break;
 		case 'e':
 			if (param) {
@@ -56,7 +74,12 @@ int cmd_idle_set(int argc, char **argv)
 				break;
 			}
 			param = ret;
-			idlestate = atoi(optarg);
+			if (is_number(optarg))
+				idlestate = atoi(optarg);
+			else {
+				printf(_("Bad idle_set value : %s\n. Integer expected"), optarg);
+				exit(EXIT_FAILURE);
+			}
 			break;
 		case 'D':
 			if (param) {
@@ -65,9 +88,15 @@ int cmd_idle_set(int argc, char **argv)
 				break;
 			}
 			param = ret;
-			latency = strtoull(optarg, &endptr, 10);
-			if (*endptr != '\0') {
-				printf(_("Bad latency value: %s\n"), optarg);
+			if (is_number(optarg)) {
+				latency = strtoull(optarg, &endptr, 10);
+				if (*endptr != '\0') {
+					printf(_("Bad latency value: %s. Integer expected\n"),
+						optarg);
+					exit(EXIT_FAILURE);
+				}
+			} else {
+				printf(_("Bad latency value: %s. Integer expected\n"), optarg);
 				exit(EXIT_FAILURE);
 			}
 			break;
-- 
2.24.3 (Apple Git-128)


^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpuidle_set accepts alpha numeric values for idle-set operation
  2021-01-05 12:24 [PATCH] cpuidle_set accepts alpha numeric values for idle-set operation Brahadambal Srinivasan
@ 2021-01-05 18:03 ` Shuah Khan
  2021-01-05 18:13   ` Shuah Khan
  2021-01-05 19:51   ` Shuah Khan
  0 siblings, 2 replies; 4+ messages in thread
From: Shuah Khan @ 2021-01-05 18:03 UTC (permalink / raw)
  To: Brahadambal Srinivasan, shuah, trenn; +Cc: linux-pm, linux-kernel, Shuah Khan

On 1/5/21 5:24 AM, Brahadambal Srinivasan wrote:
> For both the d and e options in cpuidle_set, an atoi() conversion is
> done without checking if the input argument is all numeric. So, an
> atoi conversion is done on any character provided as input and the
> CPU idle_set operation continues with that integer value, which may
> not be what is intended or entirely correct.
> 
> A similar check is present for cpufreq-set already.
> 
> This patch adds a check to see that the idle_set value is all numeric
> before doing a string-to-int conversion.
> 
> Signed-off-by: Brahadambal Srinivasan <latha@linux.vnet.ibm.com>
> ---
>   tools/power/cpupower/utils/cpuidle-set.c | 39 +++++++++++++++++++++---
>   1 file changed, 34 insertions(+), 5 deletions(-)
> 
> diff --git a/tools/power/cpupower/utils/cpuidle-set.c b/tools/power/cpupower/utils/cpuidle-set.c
> index 46158928f9ad..b3dec48e7141 100644
> --- a/tools/power/cpupower/utils/cpuidle-set.c
> +++ b/tools/power/cpupower/utils/cpuidle-set.c
> @@ -21,6 +21,19 @@ static struct option info_opts[] = {
>        { },
>   };
>   
> +int is_number(char *arg)
> +{
> +	size_t len, i = 0;
> +
> +	len = strlen(arg);
> +
> +	for (i = 0; i < len; i++) {
> +		if (!isdigit(arg[i]))
> +			return 0;
> +	}
> +
> +	return 1;
> +}
>   

Any reason why you can't use isdigit()? Please see isdigit()
usages examples in other tools and cpupower itself.

thanks,
-- Shuah

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpuidle_set accepts alpha numeric values for idle-set operation
  2021-01-05 18:03 ` Shuah Khan
@ 2021-01-05 18:13   ` Shuah Khan
  2021-01-05 19:51   ` Shuah Khan
  1 sibling, 0 replies; 4+ messages in thread
From: Shuah Khan @ 2021-01-05 18:13 UTC (permalink / raw)
  To: Brahadambal Srinivasan, shuah, trenn; +Cc: linux-pm, linux-kernel, Shuah Khan

On 1/5/21 11:03 AM, Shuah Khan wrote:
> On 1/5/21 5:24 AM, Brahadambal Srinivasan wrote:
>> For both the d and e options in cpuidle_set, an atoi() conversion is
>> done without checking if the input argument is all numeric. So, an
>> atoi conversion is done on any character provided as input and the
>> CPU idle_set operation continues with that integer value, which may
>> not be what is intended or entirely correct.
>>
>> A similar check is present for cpufreq-set already.
>>
>> This patch adds a check to see that the idle_set value is all numeric
>> before doing a string-to-int conversion.
>>
>> Signed-off-by: Brahadambal Srinivasan <latha@linux.vnet.ibm.com>
>> ---
>>   tools/power/cpupower/utils/cpuidle-set.c | 39 +++++++++++++++++++++---
>>   1 file changed, 34 insertions(+), 5 deletions(-)
>>
>> diff --git a/tools/power/cpupower/utils/cpuidle-set.c 
>> b/tools/power/cpupower/utils/cpuidle-set.c
>> index 46158928f9ad..b3dec48e7141 100644
>> --- a/tools/power/cpupower/utils/cpuidle-set.c
>> +++ b/tools/power/cpupower/utils/cpuidle-set.c
>> @@ -21,6 +21,19 @@ static struct option info_opts[] = {
>>        { },
>>   };
>> +int is_number(char *arg)
>> +{
>> +    size_t len, i = 0;
>> +
>> +    len = strlen(arg);
>> +
>> +    for (i = 0; i < len; i++) {
>> +        if (!isdigit(arg[i]))
>> +            return 0;
>> +    }
>> +
>> +    return 1;
>> +}
> 
> Any reason why you can't use isdigit()? Please see isdigit()
> usages examples in other tools and cpupower itself.
> 

Another thing. Please add "cpupower:" to identify what you are
changing clearly.

thanks,
-- Shuah




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: [PATCH] cpuidle_set accepts alpha numeric values for idle-set operation
  2021-01-05 18:03 ` Shuah Khan
  2021-01-05 18:13   ` Shuah Khan
@ 2021-01-05 19:51   ` Shuah Khan
  1 sibling, 0 replies; 4+ messages in thread
From: Shuah Khan @ 2021-01-05 19:51 UTC (permalink / raw)
  To: Brahadambal Srinivasan, shuah, trenn; +Cc: linux-pm, linux-kernel, Shuah Khan

On 1/5/21 11:03 AM, Shuah Khan wrote:
> On 1/5/21 5:24 AM, Brahadambal Srinivasan wrote:
>> For both the d and e options in cpuidle_set, an atoi() conversion is
>> done without checking if the input argument is all numeric. So, an
>> atoi conversion is done on any character provided as input and the
>> CPU idle_set operation continues with that integer value, which may
>> not be what is intended or entirely correct.
>>
>> A similar check is present for cpufreq-set already.
>>
>> This patch adds a check to see that the idle_set value is all numeric
>> before doing a string-to-int conversion.
>>
>> Signed-off-by: Brahadambal Srinivasan <latha@linux.vnet.ibm.com>
>> ---
>>   tools/power/cpupower/utils/cpuidle-set.c | 39 +++++++++++++++++++++---
>>   1 file changed, 34 insertions(+), 5 deletions(-)
>>
>> diff --git a/tools/power/cpupower/utils/cpuidle-set.c 
>> b/tools/power/cpupower/utils/cpuidle-set.c
>> index 46158928f9ad..b3dec48e7141 100644
>> --- a/tools/power/cpupower/utils/cpuidle-set.c
>> +++ b/tools/power/cpupower/utils/cpuidle-set.c
>> @@ -21,6 +21,19 @@ static struct option info_opts[] = {
>>        { },
>>   };
>> +int is_number(char *arg)
>> +{
>> +    size_t len, i = 0;
>> +
>> +    len = strlen(arg);
>> +
>> +    for (i = 0; i < len; i++) {
>> +        if (!isdigit(arg[i]))
>> +            return 0;
>> +    }
>> +
>> +    return 1;
>> +}
> 
> Any reason why you can't use isdigit()? Please see isdigit()
> usages examples in other tools and cpupower itself.
> 

Okay - Let me clarify my question a bit. What I am looking for
is if there is an existing function that can do what you are doing
here. It appears there is one:

Please take a look at the exiting is_number() in
tools/perf/tests/pmu-events.c

It uses strtod() to do what you are doing with strlen() and loop
over it with isdigit().

It is static and you can't use it as is.

Unfortunately you will have to duplicate this routine to keep
cpupower not dependent on tools/lib. Please add it to cpupower
lib instead.

thanks,
-- Shuah

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-01-05 19:52 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-01-05 12:24 [PATCH] cpuidle_set accepts alpha numeric values for idle-set operation Brahadambal Srinivasan
2021-01-05 18:03 ` Shuah Khan
2021-01-05 18:13   ` Shuah Khan
2021-01-05 19:51   ` Shuah Khan

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.