From 7ea680e187be8db7d5a3133e049a1fd6a7800d3d Mon Sep 17 00:00:00 2001 From: Alex Hultman Date: Mon, 15 Apr 2019 22:15:45 +0200 Subject: [PATCH] Fix misaligned websocket reads --- src/WebSocketProtocol.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/WebSocketProtocol.h b/src/WebSocketProtocol.h index d911fb0..e47c945 100644 --- a/src/WebSocketProtocol.h +++ b/src/WebSocketProtocol.h @@ -72,6 +72,13 @@ public: namespace protocol { +template +T bit_cast(char *c) { + T val; + memcpy(&val, c, sizeof(T)); + return val; +} + /* Byte swap for little-endian systems */ template T cond_byte_swap(T value) { @@ -399,12 +406,12 @@ public: } else if (payloadLength(src) == 126) { if (length < MEDIUM_MESSAGE_HEADER) { break; - } else if(consumeMessage(protocol::cond_byte_swap(*(uint16_t *) &src[2]), src, length, wState, user)) { + } else if(consumeMessage(protocol::cond_byte_swap(protocol::bit_cast(src + 2)), src, length, wState, user)) { return; } } else if (length < LONG_MESSAGE_HEADER) { break; - } else if (consumeMessage(protocol::cond_byte_swap(*(uint64_t *) &src[2]), src, length, wState, user)) { + } else if (consumeMessage(protocol::cond_byte_swap(protocol::bit_cast(src + 2)), src, length, wState, user)) { return; } }