Skip to contentSkip to conversation

drm/i2c/tda998x: Fix signed overflow issue

PATCH New Jean-Francois Moine 2 participants 2 messages Apr 5, 2014

Subsystems (3)
lore
Share & tools
lore
#0 lore raw
Quoted 4 lines · Daniel Vetter

This is C standard hair-splitting, but afaict
- sum will be promoted to signed int in computation since uint8_t fits
- signed overflow is undefined.

	[snip]
Quoted 8 lines
 drivers/gpu/drm/i2c/tda998x_drv.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/gpu/drm/i2c/tda998x_drv.c +0 -0
index 48af5cac1902..ae2754760d77 100644

drivers/gpu/drm/i2c/tda998x_drv.c static irqreturn_t tda998x_irq_thread(int irq, void *data) · line 574

Quoted 8 lines
drivers/gpu/drm/i2c/tda998x_drv.c +1 -1
@@ -568,7 +568,7 @@ static irqreturn_t tda998x_irq_thread(int irq, void *data)
 
 static uint8_t tda998x_cksum(uint8_t *buf, size_t bytes)
 {
-	uint8_t sum = 0;
+	unsigned sum = 0;
 
 	while (bytes--)
 		sum += *buf++;

This function may be simplified by:

tda998x_drv.c renamed from tda998x_drv.c~ +3 -3
@@ -568,11 +568,11 @@
 
 static uint8_t tda998x_cksum(uint8_t *buf, size_t bytes)
 {
-	uint8_t sum = 0;
+	int sum = 0;
 
 	while (bytes--)
-		sum += *buf++;
-	return (255 - sum) + 1;
+		sum -= *buf++;
+	return sum;
 }
 
 #define HB(x) (x)

and the same may be done in hdmi.c:

drivers/video/hdmi.c +3 -3
index 9e758a8..b6c9030 100644
@@ -31,14 +31,14 @@
 static void hdmi_infoframe_checksum(void *buffer, size_t size)
 {
 	u8 *ptr = buffer;
-	u8 csum = 0;
+	int csum = 0;
 	size_t i;
 
 	/* compute checksum */
 	for (i = 0; i < size; i++)
-		csum += ptr[i];
+		csum -= ptr[i];
 
-	ptr[3] = 256 - csum;
+	ptr[3] = csum;
 }
 
 /**
Signature · 6 lines
Ken ar c'hentañ	|	      ** Breizh ha Linux atav! **
Jef		|		http://moinejf.free.fr/
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#1 lore raw

PATCH 1/1 drm/i2c/tda998x: Fix signed overflow issue

This is C standard hair-splitting, but afaict
- sum will be promoted to signed int in computation since uint8_t fits
- signed overflow is undefined.

No we need to add up an awful lot of bytes to actually make it overflow. But I guess the real risk is gcc spotting this and going bananas. Fix this by simply using unsigned in to force all computations to use the well-defined unsigned behaviour.

Spotted by coverity.

v2: Simplify the entire computation as suggested by Jean.

Cc Russell King <[email protected]>
Cc Rob Clark <[email protected]>
Cc Jean-Francois Moine <[email protected]>
---
 drivers/gpu/drm/i2c/tda998x_drv.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/gpu/drm/i2c/tda998x_drv.c +3 -3
index 48af5cac1902..240c331405b9 100644
@@ -568,11 +568,11 @@ static irqreturn_t tda998x_irq_thread(int irq, void *data)
 
 static uint8_t tda998x_cksum(uint8_t *buf, size_t bytes)
 {
-	uint8_t sum = 0;
+	int sum = 0;
 
 	while (bytes--)
-		sum += *buf++;
-	return (255 - sum) + 1;
+		sum -= *buf++;
+	return sum;
 }
 
 #define HB(x) (x)
Signature · 1 line
1.8.5.2