public inbox for linux-iio@vger.kernel.org
 help / color / mirror / Atom feed
* [bug report] iio: hrtimer: Allow sub Hz granularity
@ 2021-03-31 14:50 Dan Carpenter
  2021-04-05 16:27 ` Jonathan Cameron
  0 siblings, 1 reply; 2+ messages in thread
From: Dan Carpenter @ 2021-03-31 14:50 UTC (permalink / raw)
  To: gwendal; +Cc: linux-iio

Hello Gwendal Grignou,

The patch dafcf4ed8392: "iio: hrtimer: Allow sub Hz granularity" from
Feb 25, 2021, leads to the following static checker warning:

	drivers/iio/trigger/iio-trig-hrtimer.c:68 iio_hrtimer_store_sampling_frequency()
	warn: assigned value is less than 'u32max'

drivers/iio/trigger/iio-trig-hrtimer.c
    49  static
    50  ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
    51                                               struct device_attribute *attr,
    52                                               const char *buf, size_t len)
    53  {
    54          struct iio_trigger *trig = to_iio_trigger(dev);
    55          struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
    56          unsigned long long val;
    57          u64 period;
    58          int integer, fract, ret;
    59  
    60          ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
    61          if (ret)
    62                  return ret;
    63          if (integer < 0 || fract < 0)
    64                  return -ERANGE;
    65  
    66          val = fract + 1000 * integer;  /* mHz */
                      ^^^^^^^^^^^^^^^^^^^^^^
"fract" and "integer" are integers so the arithmatic will wrap instead
of going above UINT_MAX

    67  
    68          if (!val || val > UINT_MAX)
                            ^^^^^^^^^^^^^^
Unpossible!

    69                  return -EINVAL;
    70  
    71          info->sampling_frequency[0] = integer;  /* Hz */
    72          info->sampling_frequency[1] = fract * 1000;  /* uHz */
    73          period = PSEC_PER_SEC;
    74          do_div(period, val);
    75          info->period = period;  /* nS */
    76  
    77          return len;
    78  }

regards,
dan carpenter

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

* Re: [bug report] iio: hrtimer: Allow sub Hz granularity
  2021-03-31 14:50 [bug report] iio: hrtimer: Allow sub Hz granularity Dan Carpenter
@ 2021-04-05 16:27 ` Jonathan Cameron
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Cameron @ 2021-04-05 16:27 UTC (permalink / raw)
  To: Dan Carpenter; +Cc: gwendal, linux-iio

On Wed, 31 Mar 2021 17:50:37 +0300
Dan Carpenter <dan.carpenter@oracle.com> wrote:

> Hello Gwendal Grignou,
> 
> The patch dafcf4ed8392: "iio: hrtimer: Allow sub Hz granularity" from
> Feb 25, 2021, leads to the following static checker warning:
> 
> 	drivers/iio/trigger/iio-trig-hrtimer.c:68 iio_hrtimer_store_sampling_frequency()
> 	warn: assigned value is less than 'u32max'
Coverity + Gustavo spotted this one as well. I've just queued up a fix
based on 1000ULL to ensure the maths is done at appropriate precision.

Thanks,

Jonathan

> 
> drivers/iio/trigger/iio-trig-hrtimer.c
>     49  static
>     50  ssize_t iio_hrtimer_store_sampling_frequency(struct device *dev,
>     51                                               struct device_attribute *attr,
>     52                                               const char *buf, size_t len)
>     53  {
>     54          struct iio_trigger *trig = to_iio_trigger(dev);
>     55          struct iio_hrtimer_info *info = iio_trigger_get_drvdata(trig);
>     56          unsigned long long val;
>     57          u64 period;
>     58          int integer, fract, ret;
>     59  
>     60          ret = iio_str_to_fixpoint(buf, 100, &integer, &fract);
>     61          if (ret)
>     62                  return ret;
>     63          if (integer < 0 || fract < 0)
>     64                  return -ERANGE;
>     65  
>     66          val = fract + 1000 * integer;  /* mHz */
>                       ^^^^^^^^^^^^^^^^^^^^^^
> "fract" and "integer" are integers so the arithmatic will wrap instead
> of going above UINT_MAX
> 
>     67  
>     68          if (!val || val > UINT_MAX)
>                             ^^^^^^^^^^^^^^
> Unpossible!
> 
>     69                  return -EINVAL;
>     70  
>     71          info->sampling_frequency[0] = integer;  /* Hz */
>     72          info->sampling_frequency[1] = fract * 1000;  /* uHz */
>     73          period = PSEC_PER_SEC;
>     74          do_div(period, val);
>     75          info->period = period;  /* nS */
>     76  
>     77          return len;
>     78  }
> 
> regards,
> dan carpenter


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

end of thread, other threads:[~2021-04-05 16:27 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-03-31 14:50 [bug report] iio: hrtimer: Allow sub Hz granularity Dan Carpenter
2021-04-05 16:27 ` Jonathan Cameron

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox