1
0
mirror of https://github.com/lammertb/libhttp.git synced 2025-12-22 04:02:04 +03:00

Cast pointer conversions through void

We are taking a char * and casting it to uint32_t or uint16_t. This
increases the required alignment to 4 and 2 respectively. On most
platforms alignment of 16bits is enough and we are writing to offsets
that a divisable by 2 so we shouldn't have any segmentation faults.
This commit is contained in:
Matt Clarkson
2015-07-15 22:22:23 +01:00
parent 857d27f918
commit b13178f0ac

View File

@@ -7136,8 +7136,8 @@ static void read_websocket(struct mg_connection *conn,
data_len = ((((size_t)buf[2]) << 8) + buf[3]); data_len = ((((size_t)buf[2]) << 8) + buf[3]);
} else if (body_len >= 10 + mask_len) { } else if (body_len >= 10 + mask_len) {
header_len = 10 + mask_len; header_len = 10 + mask_len;
data_len = (((uint64_t)ntohl(*(uint32_t *)&buf[2])) << 32) + data_len = (((uint64_t)ntohl(*(uint32_t *)(void *)&buf[2])) << 32) +
ntohl(*(uint32_t *)&buf[6]); ntohl(*(uint32_t *)(void *)&buf[6]);
} }
} }
@@ -7266,13 +7266,13 @@ int mg_websocket_write(struct mg_connection *conn,
} else if (dataLen <= 0xFFFF) { } else if (dataLen <= 0xFFFF) {
/* 16-bit length field */ /* 16-bit length field */
header[1] = 126; header[1] = 126;
*(uint16_t *)(header + 2) = htons((uint16_t)dataLen); *(uint16_t *)(void *)(header + 2) = htons((uint16_t)dataLen);
headerLen = 4; headerLen = 4;
} else { } else {
/* 64-bit length field */ /* 64-bit length field */
header[1] = 127; header[1] = 127;
*(uint32_t *)(header + 2) = htonl((uint64_t)dataLen >> 32); *(uint32_t *)(void *)(header + 2) = htonl((uint64_t)dataLen >> 32);
*(uint32_t *)(header + 6) = htonl(dataLen & 0xFFFFFFFF); *(uint32_t *)(void *)(header + 6) = htonl(dataLen & 0xFFFFFFFF);
headerLen = 10; headerLen = 10;
} }