Re: [PATCH v4 16/22] cpupower: add the function to get the sysfs value from specific table

From: Juuso Alasuutari
Date: Fri Nov 19 2021 - 17:19:49 EST


On Fri, 19 Nov 2021 14:38:58 -0600, Nathan Fontenot wrote:
> On 11/19/21 4:30 AM, Huang Rui wrote:
> > Expose the helper into cpufreq header, then cpufreq driver can use this
> > function to get the sysfs value if it has any specific sysfs interfaces.
> >
> > Signed-off-by: Huang Rui <ray.huang@xxxxxxx>
> > ---
> > tools/power/cpupower/lib/cpufreq.c | 21 +++++++++++++++------
> > tools/power/cpupower/lib/cpufreq.h | 12 ++++++++++++
> > 2 files changed, 27 insertions(+), 6 deletions(-)
> >
> > diff --git a/tools/power/cpupower/lib/cpufreq.c b/tools/power/cpupower/lib/cpufreq.c
> > index c3b56db8b921..02719cc400a1 100644
> > --- a/tools/power/cpupower/lib/cpufreq.c
> > +++ b/tools/power/cpupower/lib/cpufreq.c
> > @@ -83,20 +83,21 @@ static const char *cpufreq_value_files[MAX_CPUFREQ_VALUE_READ_FILES] = {
> > [STATS_NUM_TRANSITIONS] = "stats/total_trans"
> > };
> >
> > -
> > -static unsigned long sysfs_cpufreq_get_one_value(unsigned int cpu,
> > - enum cpufreq_value which)
> > +unsigned long cpufreq_get_sysfs_value_from_table(unsigned int cpu,
> > + const char **table,
> > + unsigned index,
> > + unsigned size)
> > {
> > unsigned long value;
> > unsigned int len;
> > char linebuf[MAX_LINE_LEN];
> > char *endp;
> >
> > - if (which >= MAX_CPUFREQ_VALUE_READ_FILES)
> > + if (!table && !table[index] && index >= size)
>
> Should you validate the index before accessing table[index]
>
> if (!table && index >= size && !table[index])

The operator is wrong as well. I believe this is what was originally intended:

if (!table || index >= size || !table[index])

- Juuso