From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-17.5 required=3.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS,INCLUDES_CR_TRAILER, INCLUDES_PATCH,MAILING_LIST_MULTI,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS, USER_AGENT_SANE_1 autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id CFF84C433E6 for ; Tue, 5 Jan 2021 19:52:04 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id A3DF522D72 for ; Tue, 5 Jan 2021 19:52:04 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1731003AbhAETv7 (ORCPT ); Tue, 5 Jan 2021 14:51:59 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:51190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726171AbhAETv6 (ORCPT ); Tue, 5 Jan 2021 14:51:58 -0500 Received: from mail-il1-x132.google.com (mail-il1-x132.google.com [IPv6:2607:f8b0:4864:20::132]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 08D07C061574 for ; Tue, 5 Jan 2021 11:51:18 -0800 (PST) Received: by mail-il1-x132.google.com with SMTP id u12so852500ilv.3 for ; Tue, 05 Jan 2021 11:51:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linuxfoundation.org; s=google; h=subject:to:cc:references:from:message-id:date:user-agent :mime-version:in-reply-to:content-language:content-transfer-encoding; bh=yuUp96Dy9EvE7pFdc2UT55pn6ORXJwMoBW6BB0BgLiY=; b=bQP7qIexcx4SZMa8hnp7djt8zuWQK49oCKLFrDYhJE0sDnGAMowJQ9lmYtVomf/p2K hU/kE2ebouwRhxiIrgclabCBKCYE/E5D14nO/8AQRbT4y7CYwvSxfvB2VcHpTEen0nnF E4A24VFEkT6v1gL7dWvqKLKd1qjJoKAcu+i9g= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:cc:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language :content-transfer-encoding; bh=yuUp96Dy9EvE7pFdc2UT55pn6ORXJwMoBW6BB0BgLiY=; b=E1Gg3aUMCKOq5lacjP0OvsfZx4zWWXejSSHqnruMxHjlT0JpCzitbmIqwfNZyUYe6S h4OvV9wtLAqd7hVN6pot0nyX1bYdZLqFxmTycA59jI8sosr2P39oBCixV9twDrd7KXDN s0DVK98RISwqmEMeMTDHpruTUFEVsl8OzafxHCwUHNGJTPRS3nd3+dNAUHYqH2dIpOGP racNK/JSHN53J+UfcNyDLb2aNxmwmKTSPO6upjPXVG0AsizRd7OOU4r6GV3rHoReLDO1 imcDbDYc9eoP0qF+Ktr6498ZfL+jRxx33528x+W7Os54ZnRf1MSGn8NVSn0L9hOn0YmZ wWOg== X-Gm-Message-State: AOAM5305S5rr8NWPzVp55BuNnJZgYkPBMFKFGsRYkarYzvf0xX56AddF u+L5phhPk+EIjyFBheUb4EQ0yA== X-Google-Smtp-Source: ABdhPJySg+2QcVGOwLgTLYpZnekmN0LjSaEldgKPqGwUnNANFqbM8WHdNyPi9pXxYXaIGyBPaeVVvQ== X-Received: by 2002:a92:8495:: with SMTP id y21mr1131074ilk.55.1609876277463; Tue, 05 Jan 2021 11:51:17 -0800 (PST) Received: from [192.168.1.112] (c-24-9-64-241.hsd1.co.comcast.net. [24.9.64.241]) by smtp.gmail.com with ESMTPSA id i6sm139679ilm.70.2021.01.05.11.51.16 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 05 Jan 2021 11:51:16 -0800 (PST) Subject: Re: [PATCH] cpuidle_set accepts alpha numeric values for idle-set operation To: Brahadambal Srinivasan , shuah@kernel.org, trenn@suse.com Cc: linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org, Shuah Khan References: <20210105122452.8687-1-latha@linux.vnet.ibm.com> From: Shuah Khan Message-ID: <5bf14c30-0c47-a93c-fd31-8b5d4caf1616@linuxfoundation.org> Date: Tue, 5 Jan 2021 12:51:16 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 >> --- >>   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