Fix pointer dereferencing to unaligned data. Many platforms do not support it, ARM...
authorfatbob313 <martin@fatbob.nu>
Thu, 14 Jan 2010 21:20:29 +0000 (21:20 +0000)
committerfatbob313 <martin@fatbob.nu>
Thu, 14 Jan 2010 21:20:29 +0000 (21:20 +0000)
Also fix potential bug in receive logic.

src/client.c
src/messages.c

index 2294b3aa23c18ee546a7ca463a37eebce2abe3c2..cb7452e3dafd710993dbfa81b6612e44a3e1c4e2 100644 (file)
@@ -300,7 +300,7 @@ int Client_read(client_t *client)
        do {
                errno = 0;
                if (!client->msgsize) 
-                       rc = SSL_read(client->ssl, client->rxbuf, 6 - client->rxcount);
+                       rc = SSL_read(client->ssl, &client->rxbuf[client->rxcount], 6 - client->rxcount);
                else if (client->drainleft > 0)
                        rc = SSL_read(client->ssl, client->rxbuf, client->drainleft > BUFSIZE ? BUFSIZE : client->drainleft);
                else
@@ -312,8 +312,9 @@ int Client_read(client_t *client)
                        else {
                                client->rxcount += rc;
                                if (!client->msgsize && client->rxcount >= 6) {
-                                       uint32_t *msgLen = (uint32_t *) &client->rxbuf[2];
-                                       client->msgsize = ntohl(*msgLen);
+                                       uint32_t msgLen;
+                                       memcpy(&msgLen, &client->rxbuf[2], sizeof(uint32_t));
+                                       client->msgsize = ntohl(msgLen);
                                }
                                if (client->msgsize > BUFSIZE - 6 && client->drainleft == 0) {
                                        Log_warn("Too big message received (%d). Discarding.", client->msgsize);
index ee32f7fcc481cf3fc86a082ec2630fdc3c8d3860..b290257c1e4d24c35580761f63343e34b1a5800d 100644 (file)
@@ -45,20 +45,27 @@ static message_t *Msg_create_nopayload(messageType_t messageType);
 
 void Msg_addPreamble(uint8_t *buffer, uint16_t type, uint32_t len)
 {
-       uint16_t *msgType = (uint16_t *) &buffer[0];
-       uint32_t *msgLen = (uint32_t *) &buffer[2];
+       type = htons(type);
+       len = htonl(len);
        
-       *msgType = htons(type);
-       *msgLen = htonl(len);
+       buffer[0] = (type) & 0xff;
+       buffer[1] = (type >> 8) & 0xff;
+       
+       buffer[2] = (len) & 0xff;
+       buffer[3] = (len >> 8) & 0xff;
+       buffer[4] = (len >> 16) & 0xff;
+       buffer[5] = (len >> 24) & 0xff; 
 }
 
 static void Msg_getPreamble(uint8_t *buffer, int *type, int *len)
 {
-       uint16_t *msgType = (uint16_t *) &buffer[0];
-       uint32_t *msgLen = (uint32_t *) &buffer[2];
+       uint16_t msgType;
+       uint32_t msgLen;
        
-       *type = (int)ntohs(*msgType);
-       *len = (int)ntohl(*msgLen);
+       msgType = buffer[0] | (buffer[1] << 8);
+       msgLen = buffer[2] | (buffer[3] << 8) | (buffer[4] << 16) | (buffer[5] << 24);
+       *type = (int)ntohs(msgType);
+       *len = (int)ntohl(msgLen);
 }
 
 #define MAX_MSGSIZE (BUFSIZE - 6)