[PATCH 3/5] Add type checking to swap macros

Matt Turner mattst88 at gmail.com
Thu Aug 4 15:06:34 PDT 2011


In a few cases, char* are passed to the swap macros, so cast them.

Signed-off-by: Matt Turner <mattst88 at gmail.com>
---
 dix/swaprep.c   |    4 ++--
 include/misc.h  |   14 +++++++++++---
 render/render.c |    6 +++---
 xkb/xkb.c       |   12 ++++++------
 4 files changed, 22 insertions(+), 14 deletions(-)

diff --git a/dix/swaprep.c b/dix/swaprep.c
index a02333d..28c354c 100644
--- a/dix/swaprep.c
+++ b/dix/swaprep.c
@@ -397,9 +397,9 @@ SwapFont(xQueryFontReply *pr, Bool hasGlyphs)
      * they are always 2 4 byte values */
     for(i = 0; i < nprops; i++)
     {
-	swapl(pby);
+	swapl((int *)pby);
 	pby += 4;
-	swapl(pby);
+	swapl((int *)pby);
 	pby += 4;
     }
     if (hasGlyphs)
diff --git a/include/misc.h b/include/misc.h
index 3bd08be..ebfbc1a 100644
--- a/include/misc.h
+++ b/include/misc.h
@@ -258,9 +258,13 @@ version_compare(uint16_t a_major, uint16_t a_minor,
 #define SwapRestL(stuff) \
     SwapLongs((CARD32 *)(stuff + 1), LengthRestL(stuff))
 
+void __attribute__((error("wrong sized variable passed to swap"))) wrong_size(void);
+
 /* byte swap a 32-bit value */
 #define swapl(x) do { \
-		 char n = ((char *) (x))[0];\
+		 char n;\
+		 if (sizeof(*(x)) != 4) wrong_size();\
+		 n = ((char *) (x))[0];\
 		 ((char *) (x))[0] = ((char *) (x))[3];\
 		 ((char *) (x))[3] = n;\
 		 n = ((char *) (x))[1];\
@@ -270,13 +274,16 @@ version_compare(uint16_t a_major, uint16_t a_minor,
 
 /* byte swap a short */
 #define swaps(x) do { \
-		 char  n = ((char *) (x))[0];\
+		 char n;\
+		 if (sizeof(*(x)) != 2) wrong_size();\
+		 n = ((char *) (x))[0];\
 		 ((char *) (x))[0] = ((char *) (x))[1];\
-		 ((char *) (x))[1] = n;
+		 ((char *) (x))[1] = n;\
 	} while (0)
 
 /* copy 32-bit value from src to dst byteswapping on the way */
 #define cpswapl(src, dst) { \
+                  if (sizeof((src)) != 4 || sizeof((dst)) != 4) wrong_size();\
                  ((char *)&(dst))[0] = ((char *) &(src))[3];\
                  ((char *)&(dst))[1] = ((char *) &(src))[2];\
                  ((char *)&(dst))[2] = ((char *) &(src))[1];\
@@ -284,6 +291,7 @@ version_compare(uint16_t a_major, uint16_t a_minor,
 
 /* copy short from src to dst byteswapping on the way */
 #define cpswaps(src, dst) { \
+		 if (sizeof((src)) != 2 || sizeof((dst)) != 2) wrong_size();\
 		 ((char *) &(dst))[0] = ((char *) &(src))[1];\
 		 ((char *) &(dst))[1] = ((char *) &(src))[0]; }
 
diff --git a/render/render.c b/render/render.c
index facc32a..cc13dbc 100644
--- a/render/render.c
+++ b/render/render.c
@@ -2375,7 +2375,7 @@ SProcRenderCompositeGlyphs (ClientPtr client)
 	i = elt->len;
 	if (i == 0xff)
 	{
-	    swapl(buffer);
+	    swapl((int *)buffer);
 	    buffer += 4;
 	}
 	else
@@ -2388,14 +2388,14 @@ SProcRenderCompositeGlyphs (ClientPtr client)
 	    case 2:
 		while (i--)
 		{
-		    swaps(buffer);
+		    swaps((short *)buffer);
 		    buffer += 2;
 		}
 		break;
 	    case 4:
 		while (i--)
 		{
-		    swapl(buffer);
+		    swapl((int *)buffer);
 		    buffer += 4;
 		}
 		break;
diff --git a/xkb/xkb.c b/xkb/xkb.c
index 8e42a7f..0b85953 100644
--- a/xkb/xkb.c
+++ b/xkb/xkb.c
@@ -1109,7 +1109,7 @@ register unsigned	i;
 	    register int nSyms= outMap->nSyms;
 	    swaps(&outMap->nSyms);
 	    while (nSyms-->0) {
-		swapl(buf);
+		swapl((int *)buf);
 		buf+= 4;
 	    }
 	}
@@ -3646,21 +3646,21 @@ char *			desc;
         if (which&XkbKeycodesNameMask) {
             *((CARD32 *)desc)= xkb->names->keycodes;
             if (client->swapped) {
-                swapl(desc);
+                swapl((int *)desc);
             }
             desc+= 4;
         }
         if (which&XkbGeometryNameMask)  {
             *((CARD32 *)desc)= xkb->names->geometry;
             if (client->swapped) {
-                swapl(desc);
+                swapl((int *)desc);
             }
             desc+= 4;
         }
         if (which&XkbSymbolsNameMask) {
             *((CARD32 *)desc)= xkb->names->symbols;
             if (client->swapped) {
-                swapl(desc);
+                swapl((int *)desc);
             }
             desc+= 4;
         }
@@ -3675,14 +3675,14 @@ char *			desc;
         if (which&XkbTypesNameMask) {
             *((CARD32 *)desc)= (CARD32)xkb->names->types;
             if (client->swapped) {
-                swapl(desc);
+                swapl((int *)desc);
             }
             desc+= 4;
         }
         if (which&XkbCompatNameMask) {
             *((CARD32 *)desc)= (CARD32)xkb->names->compat;
             if (client->swapped) {
-                swapl(desc);
+                swapl((int *)desc);
             }
             desc+= 4;
         }
-- 
1.7.3.4



More information about the xorg-devel mailing list