[quake3-commits] r2086 - trunk/code/qcommon

DONOTREPLY at icculus.org DONOTREPLY at icculus.org
Sat Jul 16 21:41:39 EDT 2011


Author: thilo
Date: 2011-07-16 21:41:39 -0400 (Sat, 16 Jul 2011)
New Revision: 2086

Modified:
   trunk/code/qcommon/msg.c
   trunk/code/qcommon/q_platform.h
   trunk/code/qcommon/q_shared.c
Log:
Fix alignment issues in message sending/reading that would crash IRIX, thanks to Canavan for supplying a shell where I could fix this (#5077)


Modified: trunk/code/qcommon/msg.c
===================================================================
--- trunk/code/qcommon/msg.c	2011-07-16 11:14:20 UTC (rev 2085)
+++ trunk/code/qcommon/msg.c	2011-07-17 01:41:39 UTC (rev 2086)
@@ -140,23 +140,28 @@
 		bits = -bits;
 	}
 	if (msg->oob) {
-		if (bits==8) {
+		if(bits==8)
+		{
 			msg->data[msg->cursize] = value;
 			msg->cursize += 1;
 			msg->bit += 8;
-		} else if (bits==16) {
-			unsigned short *sp = (unsigned short *)&msg->data[msg->cursize];
-			*sp = LittleShort(value);
+		}
+		else if(bits==16)
+		{
+			short temp = value;
+			
+			CopyLittleShort(&msg->data[msg->cursize], &temp);
 			msg->cursize += 2;
 			msg->bit += 16;
-		} else if (bits==32) {
-			unsigned int *ip = (unsigned int *)&msg->data[msg->cursize];
-			*ip = LittleLong(value);
+		}
+		else if(bits==32)
+		{
+			CopyLittleLong(&msg->data[msg->cursize], &value);
 			msg->cursize += 4;
 			msg->bit += 32;
-		} else {
-			Com_Error(ERR_DROP, "can't read %d bits", bits);
 		}
+		else 
+			Com_Error(ERR_DROP, "can't write %d bits", bits);
 	} else {
 //		fp = fopen("c:\\netchan.bin", "a");
 		value &= (0xffffffff>>(32-bits));
@@ -198,23 +203,29 @@
 	}
 
 	if (msg->oob) {
-		if (bits==8) {
+		if(bits==8)
+		{
 			value = msg->data[msg->readcount];
 			msg->readcount += 1;
 			msg->bit += 8;
-		} else if (bits==16) {
-			unsigned short *sp = (unsigned short *)&msg->data[msg->readcount];
-			value = LittleShort(*sp);
+		}
+		else if(bits==16)
+		{
+			short temp;
+			
+			CopyLittleShort(&temp, &msg->data[msg->readcount]);
+			value = temp;
 			msg->readcount += 2;
 			msg->bit += 16;
-		} else if (bits==32) {
-			unsigned int *ip = (unsigned int *)&msg->data[msg->readcount];
-			value = LittleLong(*ip);
+		}
+		else if(bits==32)
+		{
+			CopyLittleLong(&value, &msg->data[msg->readcount]);
 			msg->readcount += 4;
 			msg->bit += 32;
-		} else {
+		}
+		else
 			Com_Error(ERR_DROP, "can't read %d bits", bits);
-		}
 	} else {
 		nbits = 0;
 		if (bits&7) {

Modified: trunk/code/qcommon/q_platform.h
===================================================================
--- trunk/code/qcommon/q_platform.h	2011-07-16 11:14:20 UTC (rev 2085)
+++ trunk/code/qcommon/q_platform.h	2011-07-17 01:41:39 UTC (rev 2086)
@@ -348,6 +348,8 @@
 #error "Endianness defined as both big and little"
 #elif defined( Q3_BIG_ENDIAN )
 
+#define CopyLittleShort(dest, src) CopyShortSwap(dest, src)
+#define CopyLittleLong(dest, src) CopyLongSwap(dest, src)
 #define LittleShort(x) ShortSwap(x)
 #define LittleLong(x) LongSwap(x)
 #define LittleFloat(x) FloatSwap(&x)
@@ -357,6 +359,8 @@
 
 #elif defined( Q3_LITTLE_ENDIAN )
 
+#define CopyLittleShort(dest, src) Com_Memcpy(dest, src, 2)
+#define CopyLittleLong(dest, src) Com_Memcpy(dest, src, 4)
 #define LittleShort
 #define LittleLong
 #define LittleFloat

Modified: trunk/code/qcommon/q_shared.c
===================================================================
--- trunk/code/qcommon/q_shared.c	2011-07-16 11:14:20 UTC (rev 2085)
+++ trunk/code/qcommon/q_shared.c	2011-07-17 01:41:39 UTC (rev 2086)
@@ -129,6 +129,24 @@
 float	LittleFloat (const float *l) {return _LittleFloat(l);}
 */
 
+void CopyShortSwap(void *dest, void *src)
+{
+	byte *to = dest, *from = src;
+
+	to[0] = from[1];
+	to[1] = from[0];
+}
+
+void CopyLongSwap(void *dest, void *src)
+{
+	byte *to = dest, *from = src;
+
+	to[0] = from[3];
+	to[1] = from[2];
+	to[2] = from[1];
+	to[3] = from[0];
+}
+
 short   ShortSwap (short l)
 {
 	byte    b1,b2;



More information about the quake3-commits mailing list