linux-spi.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v1] Input: touchscreen: ads7846.c: fix integer overflow on Rt calculation
@ 2020-11-13 11:22 Oleksij Rempel
  2020-11-18  0:31 ` Dmitry Torokhov
  0 siblings, 1 reply; 2+ messages in thread
From: Oleksij Rempel @ 2020-11-13 11:22 UTC (permalink / raw)
  To: Dmitry Torokhov, Alexandru Ardelean, Mark Brown
  Cc: Oleksij Rempel, David Jander, kernel, linux-kernel, linux-input,
	linux-spi

In some rare cases the 32 bit Rt value will overflow if z2 and x is max,
z1 is minimal value and x_plate_ohms is relatively high (for example 800
ohm). This would happen on some screen age with low pressure.

There are two possible fixes:
- make Rt 64bit
- reorder calculation to avoid overflow

The second variant seems to be preferable, since 64 bit calculation on
32 bit system is a bit more expensive.

Fixes: ffa458c1bd9b6f653008d450f337602f3d52a646 ("spi: ads7846 driver")
Co-Developed-by: David Jander <david@protonic.nl>
Signed-off-by: David Jander <david@protonic.nl>
Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
---
 drivers/input/touchscreen/ads7846.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/input/touchscreen/ads7846.c b/drivers/input/touchscreen/ads7846.c
index 652d9bcfdaee..78d7159e92fe 100644
--- a/drivers/input/touchscreen/ads7846.c
+++ b/drivers/input/touchscreen/ads7846.c
@@ -993,10 +993,11 @@ static void ads7846_report_state(struct ads7846 *ts)
 		/* compute touch pressure resistance using equation #2 */
 		Rt = z2;
 		Rt -= z1;
-		Rt *= x;
 		Rt *= ts->x_plate_ohms;
+		Rt = DIV_ROUND_CLOSEST(Rt, 16);
+		Rt *= x;
 		Rt /= z1;
-		Rt = (Rt + 2047) >> 12;
+		Rt = DIV_ROUND_CLOSEST(Rt, 256);
 	} else {
 		Rt = 0;
 	}
-- 
2.28.0


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

* Re: [PATCH v1] Input: touchscreen: ads7846.c: fix integer overflow on Rt calculation
  2020-11-13 11:22 [PATCH v1] Input: touchscreen: ads7846.c: fix integer overflow on Rt calculation Oleksij Rempel
@ 2020-11-18  0:31 ` Dmitry Torokhov
  0 siblings, 0 replies; 2+ messages in thread
From: Dmitry Torokhov @ 2020-11-18  0:31 UTC (permalink / raw)
  To: Oleksij Rempel
  Cc: Alexandru Ardelean, Mark Brown, David Jander, kernel,
	linux-kernel, linux-input, linux-spi

On Fri, Nov 13, 2020 at 12:22:40PM +0100, Oleksij Rempel wrote:
> In some rare cases the 32 bit Rt value will overflow if z2 and x is max,
> z1 is minimal value and x_plate_ohms is relatively high (for example 800
> ohm). This would happen on some screen age with low pressure.
> 
> There are two possible fixes:
> - make Rt 64bit
> - reorder calculation to avoid overflow
> 
> The second variant seems to be preferable, since 64 bit calculation on
> 32 bit system is a bit more expensive.
> 
> Fixes: ffa458c1bd9b6f653008d450f337602f3d52a646 ("spi: ads7846 driver")
> Co-Developed-by: David Jander <david@protonic.nl>
> Signed-off-by: David Jander <david@protonic.nl>
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>

Applied, thank you.

-- 
Dmitry

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

end of thread, other threads:[~2020-11-18  0:32 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-11-13 11:22 [PATCH v1] Input: touchscreen: ads7846.c: fix integer overflow on Rt calculation Oleksij Rempel
2020-11-18  0:31 ` Dmitry Torokhov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).