diff options
author | Gregor Boirie <gregor.boirie@parrot.com> | 2016-09-02 14:27:46 -0400 |
---|---|---|
committer | Jonathan Cameron <jic23@kernel.org> | 2016-09-05 16:21:32 -0400 |
commit | 171c0091837c81ed5c949fec6966bb5afff2d1cf (patch) | |
tree | a1d5f5ac471ef9c9a224632479e00623fd4c4a7e | |
parent | 5dba4b14bafe801083d01e1f400816df7e5a8f2e (diff) |
iio:core: fix IIO_VAL_FRACTIONAL sign handling
7985e7c100 ("iio: Introduce a new fractional value type") introduced a
new IIO_VAL_FRACTIONAL value type meant to represent rational type numbers
expressed by a numerator and denominator combination.
Formating of IIO_VAL_FRACTIONAL values relies upon do_div() usage. This
fails handling negative values properly since parameters are reevaluated
as unsigned values.
Fix this by using div_s64_rem() instead. Computed integer part will carry
properly signed value. Formatted fractional part will always be positive.
Fixes: 7985e7c100 ("iio: Introduce a new fractional value type")
Signed-off-by: Gregor Boirie <gregor.boirie@parrot.com>
Reviewed-by: Lars-Peter Clausen <lars@metafoo.de>
Cc: <Stable@vger.kernel.org>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>
-rw-r--r-- | drivers/iio/industrialio-core.c | 5 |
1 files changed, 2 insertions, 3 deletions
diff --git a/drivers/iio/industrialio-core.c b/drivers/iio/industrialio-core.c index f914d5d140e4..d2b889918c3e 100644 --- a/drivers/iio/industrialio-core.c +++ b/drivers/iio/industrialio-core.c | |||
@@ -613,9 +613,8 @@ ssize_t iio_format_value(char *buf, unsigned int type, int size, int *vals) | |||
613 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); | 613 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); |
614 | case IIO_VAL_FRACTIONAL: | 614 | case IIO_VAL_FRACTIONAL: |
615 | tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); | 615 | tmp = div_s64((s64)vals[0] * 1000000000LL, vals[1]); |
616 | vals[1] = do_div(tmp, 1000000000LL); | 616 | vals[0] = (int)div_s64_rem(tmp, 1000000000, &vals[1]); |
617 | vals[0] = tmp; | 617 | return sprintf(buf, "%d.%09u\n", vals[0], abs(vals[1])); |
618 | return sprintf(buf, "%d.%09u\n", vals[0], vals[1]); | ||
619 | case IIO_VAL_FRACTIONAL_LOG2: | 618 | case IIO_VAL_FRACTIONAL_LOG2: |
620 | tmp = (s64)vals[0] * 1000000000LL >> vals[1]; | 619 | tmp = (s64)vals[0] * 1000000000LL >> vals[1]; |
621 | vals[1] = do_div(tmp, 1000000000LL); | 620 | vals[1] = do_div(tmp, 1000000000LL); |