Skip to content

Commit

Permalink
Fix unaligned reads and writes of the checksum (fixes #237) (#238)
Browse files Browse the repository at this point in the history
Co-authored-by: Arvid Norlander <[email protected]>
  • Loading branch information
arvid-norlander and arvid-norlander authored Mar 5, 2024
1 parent 2a85cd6 commit c44b7d0
Showing 1 changed file with 11 additions and 5 deletions.
16 changes: 11 additions & 5 deletions protocol.c
Original file line number Diff line number Diff line change
Expand Up @@ -1072,11 +1072,14 @@ enet_protocol_handle_incoming_commands (ENetHost * host, ENetEvent * event)

if (host -> checksum != NULL)
{
enet_uint32 * checksum = (enet_uint32 *) & host -> receivedData [headerSize - sizeof (enet_uint32)],
desiredChecksum = * checksum;
enet_uint32 * checksum = (enet_uint32 *) & host -> receivedData [headerSize - sizeof (enet_uint32)];
enet_uint32 desiredChecksum, newChecksum;
ENetBuffer buffer;
/* Checksum may be an unaligned pointer, use memcpy to avoid undefined behaviour. */
memcpy (& desiredChecksum, checksum, sizeof (enet_uint32));

* checksum = peer != NULL ? peer -> connectID : 0;
newChecksum = peer != NULL ? peer -> connectID : 0;
memcpy (checksum, & newChecksum, sizeof (enet_uint32));

buffer.data = host -> receivedData;
buffer.dataLength = host -> receivedDataLength;
Expand Down Expand Up @@ -1704,9 +1707,12 @@ enet_protocol_send_outgoing_commands (ENetHost * host, ENetEvent * event, int ch
if (host -> checksum != NULL)
{
enet_uint32 * checksum = (enet_uint32 *) & headerData [host -> buffers -> dataLength];
* checksum = currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID ? currentPeer -> connectID : 0;
enet_uint32 newChecksum = currentPeer -> outgoingPeerID < ENET_PROTOCOL_MAXIMUM_PEER_ID ? currentPeer -> connectID : 0;
/* Checksum may be unaligned, use memcpy to avoid undefined behaviour. */
memcpy(checksum, & newChecksum, sizeof (enet_uint32));
host -> buffers -> dataLength += sizeof (enet_uint32);
* checksum = host -> checksum (host -> buffers, host -> bufferCount);
newChecksum = host -> checksum (host -> buffers, host -> bufferCount);
memcpy(checksum, & newChecksum, sizeof (enet_uint32));
}

if (shouldCompress > 0)
Expand Down

0 comments on commit c44b7d0

Please sign in to comment.