[LWN Logo]
[Timeline]
Date:	Mon, 16 Oct 2000 16:57:56 +0100 (BST)
From:	Bernd Schmidt <bernds@redhat.co.uk>
To:	torvalds@transmeta.com
Subject: Patch to remove undefined C code

I've been playing with some gcc patches to detect code with undefined
behaviour of the i = i++ variety.  The patch below fixes all places in
the kernel that I could find.  Note that in some cases, it wasn't
entirely clear what the code intended, so I had to guess.

I haven't tested this patch at all other than to make sure it compiles.

Bernd

diff -x log.build -x .* -dru linux-2.4/drivers/i2o/i2o_core.c linux-2.4-fixed/drivers/i2o/i2o_core.c
--- linux-2.4/drivers/i2o/i2o_core.c	Mon Jun 19 21:30:56 2000
+++ linux-2.4-fixed/drivers/i2o/i2o_core.c	Mon Oct 16 14:52:46 2000
@@ -183,7 +183,7 @@
 static int evt_in = 0;
 static int evt_out = 0;
 static int evt_q_len = 0;
-#define MODINC(x,y) (x = x++ % y)
+#define MODINC(x,y) ((x) = ((x) + 1) % (y))
 
 /*
  * I2O configuration spinlock. This isnt a big deal for contention
diff -x log.build -x .* -dru linux-2.4/drivers/ide/alim15x3.c linux-2.4-fixed/drivers/ide/alim15x3.c
--- linux-2.4/drivers/ide/alim15x3.c	Tue Jun 20 15:52:36 2000
+++ linux-2.4-fixed/drivers/ide/alim15x3.c	Mon Oct 16 15:10:24 2000
@@ -171,11 +171,11 @@
 				((reg5yh & 0x30)>>4) + 12 );
 		}
 	} else {
-		p += sprintf(p, q,
-			(tmp = (reg5xh & 0x03)) ? (tmp << 3) : 4,
-			(tmp = ((reg5xh & 0x30)>>4)) ? (tmp << 3) : 4,
-			(tmp = (reg5yh & 0x03)) ? (tmp << 3) : 4,
-			(tmp = ((reg5yh & 0x30)>>4)) ? (tmp << 3) : 4 );
+		int t1 = (tmp = (reg5xh & 0x03)) ? (tmp << 3) : 4;
+		int t2 = (tmp = ((reg5xh & 0x30)>>4)) ? (tmp << 3) : 4;
+		int t3 = (tmp = (reg5yh & 0x03)) ? (tmp << 3) : 4;
+		int t4 = (tmp = ((reg5yh & 0x30)>>4)) ? (tmp << 3) : 4;
+		p += sprintf(p, q, t1, t2, t3, t4);
 	}
 
 #if 0
diff -x log.build -x .* -dru linux-2.4/drivers/ide/ide-disk.c linux-2.4-fixed/drivers/ide/ide-disk.c
--- linux-2.4/drivers/ide/ide-disk.c	Tue Jun 20 15:52:36 2000
+++ linux-2.4-fixed/drivers/ide/ide-disk.c	Mon Oct 16 14:48:49 2000
@@ -64,8 +64,8 @@
 	u16 *p = buffer;
 
 	while (wcount--) {
-		*p++ = *p << 8 | *p >> 8;
-		*p++ = *p << 8 | *p >> 8;
+		*p = *p << 8 | *p >> 8; p++;
+		*p = *p << 8 | *p >> 8; p++;
 	}
 }
 
diff -x log.build -x .* -dru linux-2.4/drivers/isdn/sc/debug.c linux-2.4-fixed/drivers/isdn/sc/debug.c
--- linux-2.4/drivers/isdn/sc/debug.c	Thu Apr  2 01:21:04 1998
+++ linux-2.4-fixed/drivers/isdn/sc/debug.c	Mon Oct 16 14:53:49 2000
@@ -70,6 +70,6 @@
 	int i = 0;
 
 	while(dn[i] != ',')
-		str[i] = dn[i++];
+		str[i] = dn[i], i++;
 	str[i] = 0x0;
 }
diff -x log.build -x .* -dru linux-2.4/drivers/net/tulip/tulip_core.c linux-2.4-fixed/drivers/net/tulip/tulip_core.c
--- linux-2.4/drivers/net/tulip/tulip_core.c	Mon Oct 16 13:51:23 2000
+++ linux-2.4-fixed/drivers/net/tulip/tulip_core.c	Mon Oct 16 15:40:12 2000
@@ -924,18 +924,20 @@
 				 i++, mclist = mclist->next)
 				set_bit(ether_crc_le(ETH_ALEN, mclist->dmi_addr) & 0x1ff,
 						hash_table);
-			for (i = 0; i < 32; i++)
-				*setup_frm++ = *setup_frm++ = hash_table[i];
+			for (i = 0; i < 32; i++) {
+				*setup_frm++ = hash_table[i];
+				*setup_frm++ = hash_table[i];
+			}
 			setup_frm = &tp->setup_frame[13*6];
 		} else {
 			/* We have <= 14 addresses so we can use the wonderful
 			   16 address perfect filtering of the Tulip. */
 			for (i = 0, mclist = dev->mc_list; i < dev->mc_count;
 				 i++, mclist = mclist->next) {
-				eaddrs = (u16 *)mclist->dmi_addr;
-				*setup_frm++ = *setup_frm++ = *eaddrs++;
-				*setup_frm++ = *setup_frm++ = *eaddrs++;
-				*setup_frm++ = *setup_frm++ = *eaddrs++;
+				u16 *eaddrs = (u16 *)mclist->dmi_addr;
+				*setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
+				*setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
+				*setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
 			}
 			/* Fill the unused entries with the broadcast address. */
 			memset(setup_frm, 0xff, (15-i)*12);
@@ -944,9 +946,9 @@
 
 		/* Fill the final entry with our physical address. */
 		eaddrs = (u16 *)dev->dev_addr;
-		*setup_frm++ = *setup_frm++ = eaddrs[0];
-		*setup_frm++ = *setup_frm++ = eaddrs[1];
-		*setup_frm++ = *setup_frm++ = eaddrs[2];
+		*setup_frm++ = eaddrs[0]; *setup_frm++ = eaddrs[0];
+		*setup_frm++ = eaddrs[1]; *setup_frm++ = eaddrs[1];
+		*setup_frm++ = eaddrs[2]; *setup_frm++ = eaddrs[2];
 
 		spin_lock_irqsave(&tp->lock, flags);
 
diff -x log.build -x .* -dru linux-2.4/drivers/scsi/aha152x.c linux-2.4-fixed/drivers/scsi/aha152x.c
--- linux-2.4/drivers/scsi/aha152x.c	Mon Oct 16 13:51:24 2000
+++ linux-2.4-fixed/drivers/scsi/aha152x.c	Mon Oct 16 14:51:29 2000
@@ -1280,7 +1280,8 @@
 			scsi_unregister(shpnt);
 			registered_count--;
 			release_region(shpnt->io_port, IO_RANGE);
-			aha152x_host[shpnt->irq - IRQ_MIN] = shpnt = 0;
+			aha152x_host[shpnt->irq - IRQ_MIN] = 0;
+			shpnt = 0;
 			continue;
 		}
 		HOSTDATA(shpnt)->swint = 0;
diff -x log.build -x .* -dru linux-2.4/drivers/scsi/wd7000.c linux-2.4-fixed/drivers/scsi/wd7000.c
--- linux-2.4/drivers/scsi/wd7000.c	Mon Oct 16 13:51:24 2000
+++ linux-2.4-fixed/drivers/scsi/wd7000.c	Mon Oct 16 14:52:08 2000
@@ -951,7 +951,7 @@
 	    break;
 	}
 	else
-	    ogmb = (++ogmb) % OGMB_CNT;
+	    ogmb = (ogmb + 1) % OGMB_CNT;
     }
     restore_flags (flags);
 
diff -x log.build -x .* -dru linux-2.4/drivers/video/clgenfb.c linux-2.4-fixed/drivers/video/clgenfb.c
--- linux-2.4/drivers/video/clgenfb.c	Mon Oct 16 14:48:12 2000
+++ linux-2.4-fixed/drivers/video/clgenfb.c	Mon Oct 16 14:48:09 2000
@@ -997,10 +997,10 @@
 		vsync *= 2;
 		vback *= 2;
 	} else if (_par->var.vmode & FB_VMODE_INTERLACED) {
-		yres = ++yres / 2;
-		vfront = ++vfront / 2;
-		vsync = ++vsync / 2;
-		vback = ++vback / 2;
+		yres = (yres + 1) / 2;
+		vfront = (vfront + 1) / 2;
+		vsync = (vsync + 1) / 2;
+		vback = (vback + 1) / 2;
 	}
 	_par->HorizRes = xres;
 	_par->HorizTotal = (xres + hfront + hsync + hback) / 8 - 5;
diff -x log.build -x .* -dru linux-2.4/drivers/video/mdacon.c linux-2.4-fixed/drivers/video/mdacon.c
--- linux-2.4/drivers/video/mdacon.c	Tue Jul 25 01:51:41 2000
+++ linux-2.4-fixed/drivers/video/mdacon.c	Mon Oct 16 14:46:32 2000
@@ -422,7 +422,8 @@
 static void mdacon_invert_region(struct vc_data *c, u16 *p, int count)
 {
 	for (; count > 0; count--) {
-		scr_writew(scr_readw(p) ^ 0x0800, p++);
+		scr_writew(scr_readw(p) ^ 0x0800, p);
+		p++;
 	}
 }
 
diff -x log.build -x .* -dru linux-2.4/fs/nfs/nfs2xdr.c linux-2.4-fixed/fs/nfs/nfs2xdr.c
--- linux-2.4/fs/nfs/nfs2xdr.c	Mon May 15 20:11:10 2000
+++ linux-2.4-fixed/fs/nfs/nfs2xdr.c	Mon Oct 16 16:23:56 2000
@@ -104,7 +104,8 @@
 static inline u32*
 xdr_decode_time(u32 *p, u64 *timep)
 {
-	*timep = ((u64)ntohl(*p++) << 32) + (u64)ntohl(*p++);
+	u64 tmp = (u64)ntohl(*p++) << 32;
+	*timep = tmp + (u64)ntohl(*p++);
 	return p;
 }
 
diff -x log.build -x .* -dru linux-2.4/fs/nfs/nfs3xdr.c linux-2.4-fixed/fs/nfs/nfs3xdr.c
--- linux-2.4/fs/nfs/nfs3xdr.c	Sat Apr  1 17:04:27 2000
+++ linux-2.4-fixed/fs/nfs/nfs3xdr.c	Mon Oct 16 16:24:55 2000
@@ -143,7 +143,8 @@
 static inline u32 *
 xdr_decode_time3(u32 *p, u64 *timep)
 {
-	*timep = ((u64)ntohl(*p++) << 32) + (u64)ntohl(*p++);
+	u64 tmp = (u64)ntohl(*p++) << 32;
+	*timep = tmp + (u64)ntohl(*p++);
 	return p;
 }
 
@@ -184,7 +185,8 @@
 	p = xdr_decode_hyper(p, &fattr->size);
 	p = xdr_decode_hyper(p, &fattr->du.nfs3.used);
 	/* Turn remote device info into Linux-specific dev_t */
-	fattr->rdev = (ntohl(*p++) << MINORBITS) | (ntohl(*p++) & MINORMASK);
+	fattr->rdev = ntohl(*p++) << MINORBITS;
+	fattr->rdev |= ntohl(*p++) & MINORMASK;
 	p = xdr_decode_hyper(p, &fattr->fsid);
 	p = xdr_decode_hyper(p, &fattr->fileid);
 	p = xdr_decode_time3(p, &fattr->atime);
diff -x log.build -x .* -dru linux-2.4/include/linux/byteorder/big_endian.h linux-2.4-fixed/include/linux/byteorder/big_endian.h
--- linux-2.4/include/linux/byteorder/big_endian.h	Thu Feb 17 17:20:13 2000
+++ linux-2.4-fixed/include/linux/byteorder/big_endian.h	Mon Oct 16 15:27:03 2000
@@ -14,12 +14,12 @@
 #define __constant_ntohl(x) ((__u32)(x))
 #define __constant_htons(x) ((__u16)(x))
 #define __constant_ntohs(x) ((__u16)(x))
-#define __constant_cpu_to_le64(x) ___swab64((x))
-#define __constant_le64_to_cpu(x) ___swab64((x))
-#define __constant_cpu_to_le32(x) ___swab32((x))
-#define __constant_le32_to_cpu(x) ___swab32((x))
-#define __constant_cpu_to_le16(x) ___swab16((x))
-#define __constant_le16_to_cpu(x) ___swab16((x))
+#define __constant_cpu_to_le64(x) ___constant_swab64((x))
+#define __constant_le64_to_cpu(x) ___constant_swab64((x))
+#define __constant_cpu_to_le32(x) ___constant_swab32((x))
+#define __constant_le32_to_cpu(x) ___constant_swab32((x))
+#define __constant_cpu_to_le16(x) ___constant_swab16((x))
+#define __constant_le16_to_cpu(x) ___constant_swab16((x))
 #define __constant_cpu_to_be64(x) ((__u64)(x))
 #define __constant_be64_to_cpu(x) ((__u64)(x))
 #define __constant_cpu_to_be32(x) ((__u32)(x))
diff -x log.build -x .* -dru linux-2.4/include/linux/byteorder/little_endian.h linux-2.4-fixed/include/linux/byteorder/little_endian.h
--- linux-2.4/include/linux/byteorder/little_endian.h	Mon Oct 16 14:03:26 2000
+++ linux-2.4-fixed/include/linux/byteorder/little_endian.h	Mon Oct 16 15:50:56 2000
@@ -10,22 +10,22 @@
 
 #include <linux/byteorder/swab.h>
 
-#define __constant_htonl(x) ___swab32((x))
-#define __constant_ntohl(x) ___swab32((x))
-#define __constant_htons(x) ___swab16((x))
-#define __constant_ntohs(x) ___swab16((x))
+#define __constant_htonl(x) ___constant_swab32((x))
+#define __constant_ntohl(x) ___constant_swab32((x))
+#define __constant_htons(x) ___constant_swab16((x))
+#define __constant_ntohs(x) ___constant_swab16((x))
 #define __constant_cpu_to_le64(x) ((__u64)(x))
 #define __constant_le64_to_cpu(x) ((__u64)(x))
 #define __constant_cpu_to_le32(x) ((__u32)(x))
 #define __constant_le32_to_cpu(x) ((__u32)(x))
 #define __constant_cpu_to_le16(x) ((__u16)(x))
 #define __constant_le16_to_cpu(x) ((__u16)(x))
-#define __constant_cpu_to_be64(x) ___swab64((x))
-#define __constant_be64_to_cpu(x) ___swab64((x))
-#define __constant_cpu_to_be32(x) ___swab32((x))
-#define __constant_be32_to_cpu(x) ___swab32((x))
-#define __constant_cpu_to_be16(x) ___swab16((x))
-#define __constant_be16_to_cpu(x) ___swab16((x))
+#define __constant_cpu_to_be64(x) ___constant_swab64((x))
+#define __constant_be64_to_cpu(x) ___constant_swab64((x))
+#define __constant_cpu_to_be32(x) ___constant_swab32((x))
+#define __constant_be32_to_cpu(x) ___constant_swab32((x))
+#define __constant_cpu_to_be16(x) ___constant_swab16((x))
+#define __constant_be16_to_cpu(x) ___constant_swab16((x))
 #define __cpu_to_le64(x) ((__u64)(x))
 #define __le64_to_cpu(x) ((__u64)(x))
 #define __cpu_to_le32(x) ((__u32)(x))
diff -x log.build -x .* -dru linux-2.4/include/linux/byteorder/pdp_endian.h linux-2.4-fixed/include/linux/byteorder/pdp_endian.h
--- linux-2.4/include/linux/byteorder/pdp_endian.h	Thu Feb 17 17:20:13 2000
+++ linux-2.4-fixed/include/linux/byteorder/pdp_endian.h	Mon Oct 16 15:31:35 2000
@@ -30,22 +30,22 @@
 #include <linux/byteorder/swab.h>
 #include <linux/byteorder/swabb.h>
 
-#define __constant_htonl(x) ___swahb32((x))
-#define __constant_ntohl(x) ___swahb32((x))
-#define __constant_htons(x) ___swab16((x))
-#define __constant_ntohs(x) ___swab16((x))
+#define __constant_htonl(x) ___constant_swahb32((x))
+#define __constant_ntohl(x) ___constant_swahb32((x))
+#define __constant_htons(x) ___constant_swab16((x))
+#define __constant_ntohs(x) ___constant_swab16((x))
 #define __constant_cpu_to_le64(x) I DON'T KNOW
 #define __constant_le64_to_cpu(x) I DON'T KNOW
-#define __constant_cpu_to_le32(x) ___swahw32((x))
-#define __constant_le32_to_cpu(x) ___swahw32((x))
+#define __constant_cpu_to_le32(x) ___constant_swahw32((x))
+#define __constant_le32_to_cpu(x) ___constant_swahw32((x))
 #define __constant_cpu_to_le16(x) ((__u16)(x)
 #define __constant_le16_to_cpu(x) ((__u16)(x)
 #define __constant_cpu_to_be64(x) I DON'T KNOW
 #define __constant_be64_to_cpu(x) I DON'T KNOW
-#define __constant_cpu_to_be32(x) ___swahb32((x))
-#define __constant_be32_to_cpu(x) ___swahb32((x))
-#define __constant_cpu_to_be16(x) ___swab16((x))
-#define __constant_be16_to_cpu(x) ___swab16((x))
+#define __constant_cpu_to_be32(x) ___constant_swahb32((x))
+#define __constant_be32_to_cpu(x) ___constant_swahb32((x))
+#define __constant_cpu_to_be16(x) ___constant_swab16((x))
+#define __constant_be16_to_cpu(x) ___constant_swab16((x))
 #define __cpu_to_le64(x) I DON'T KNOW
 #define __le64_to_cpu(x) I DON'T KNOW
 #define __cpu_to_le32(x) ___swahw32((x))
diff -x log.build -x .* -dru linux-2.4/include/linux/byteorder/swab.h linux-2.4-fixed/include/linux/byteorder/swab.h
--- linux-2.4/include/linux/byteorder/swab.h	Mon Oct 16 13:51:26 2000
+++ linux-2.4-fixed/include/linux/byteorder/swab.h	Mon Oct 16 15:50:29 2000
@@ -19,16 +19,48 @@
  * how U/UL/ULL map to __u16, __u32, __u64. At least not in a portable way.
  */
 #define ___swab16(x) \
+({ \
+	__u16 __x = (x); \
+	((__u16)( \
+		(((__u16)(__x) & (__u16)0x00ffU) << 8) | \
+		(((__u16)(__x) & (__u16)0xff00U) >> 8) )); \
+})
+
+#define ___swab32(x) \
+({ \
+	__u32 __x = (x); \
+	((__u32)( \
+		(((__u32)(__x) & (__u32)0x000000ffUL) << 24) | \
+		(((__u32)(__x) & (__u32)0x0000ff00UL) <<  8) | \
+		(((__u32)(__x) & (__u32)0x00ff0000UL) >>  8) | \
+		(((__u32)(__x) & (__u32)0xff000000UL) >> 24) )); \
+})
+
+#define ___swab64(x) \
+({ \
+	__u64 __x = (x); \
+	((__u64)( \
+		(__u64)(((__u64)(__x) & (__u64)0x00000000000000ffULL) << 56) | \
+		(__u64)(((__u64)(__x) & (__u64)0x000000000000ff00ULL) << 40) | \
+		(__u64)(((__u64)(__x) & (__u64)0x0000000000ff0000ULL) << 24) | \
+		(__u64)(((__u64)(__x) & (__u64)0x00000000ff000000ULL) <<  8) | \
+	        (__u64)(((__u64)(__x) & (__u64)0x000000ff00000000ULL) >>  8) | \
+		(__u64)(((__u64)(__x) & (__u64)0x0000ff0000000000ULL) >> 24) | \
+		(__u64)(((__u64)(__x) & (__u64)0x00ff000000000000ULL) >> 40) | \
+		(__u64)(((__u64)(__x) & (__u64)0xff00000000000000ULL) >> 56) )); \
+})
+
+#define ___constant_swab16(x) \
 	((__u16)( \
 		(((__u16)(x) & (__u16)0x00ffU) << 8) | \
 		(((__u16)(x) & (__u16)0xff00U) >> 8) ))
-#define ___swab32(x) \
+#define ___constant_swab32(x) \
 	((__u32)( \
 		(((__u32)(x) & (__u32)0x000000ffUL) << 24) | \
 		(((__u32)(x) & (__u32)0x0000ff00UL) <<  8) | \
 		(((__u32)(x) & (__u32)0x00ff0000UL) >>  8) | \
 		(((__u32)(x) & (__u32)0xff000000UL) >> 24) ))
-#define ___swab64(x) \
+#define ___constant_swab64(x) \
 	((__u64)( \
 		(__u64)(((__u64)(x) & (__u64)0x00000000000000ffULL) << 56) | \
 		(__u64)(((__u64)(x) & (__u64)0x000000000000ff00ULL) << 40) | \
diff -x log.build -x .* -dru linux-2.4/include/linux/byteorder/swabb.h linux-2.4-fixed/include/linux/byteorder/swabb.h
--- linux-2.4/include/linux/byteorder/swabb.h	Tue Sep  1 18:50:11 1998
+++ linux-2.4-fixed/include/linux/byteorder/swabb.h	Mon Oct 16 15:50:43 2000
@@ -25,12 +25,26 @@
  *
  */
 
-
 #define ___swahw32(x) \
+({ \
+	__u32 __x = (x); \
+	((__u32)( \
+		(((__u32)(__x) & (__u32)0x0000ffffUL) << 16) | \
+		(((__u32)(__x) & (__u32)0xffff0000UL) >> 16) )); \
+})
+#define ___swahb32(x) \
+({ \
+	__u32 __x = (x); \
+	((__u32)( \
+		(((__u32)(__x) & (__u32)0x00ff00ffUL) << 8) | \
+		(((__u32)(__x) & (__u32)0xff00ff00UL) >> 8) )) \
+})
+
+#define ___constant_swahw32(x) \
 	((__u32)( \
 		(((__u32)(x) & (__u32)0x0000ffffUL) << 16) | \
 		(((__u32)(x) & (__u32)0xffff0000UL) >> 16) ))
-#define ___swahb32(x) \
+#define ___constant_swahb32(x) \
 	((__u32)( \
 		(((__u32)(x) & (__u32)0x00ff00ffUL) << 8) | \
 		(((__u32)(x) & (__u32)0xff00ff00UL) >> 8) ))

-
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@vger.kernel.org
Please read the FAQ at http://www.tux.org/lkml/