From 6fabf34630f8bc111da6a86aafca4553f2357d58 Mon Sep 17 00:00:00 2001 From: Sven Almgren Date: Thu, 15 Mar 2012 15:15:19 +0100 Subject: [PATCH 1/2] Removed some nonstrict-alias code --- src/processors/hybi.hpp | 6 ++---- src/processors/hybi_legacy.hpp | 13 ++++++------- src/websocket_frame.hpp | 7 +------ 3 files changed, 9 insertions(+), 17 deletions(-) diff --git a/src/processors/hybi.hpp b/src/processors/hybi.hpp index 01659469cf..4cc46d6cba 100644 --- a/src/processors/hybi.hpp +++ b/src/processors/hybi.hpp @@ -550,11 +550,9 @@ public: } // set close payload - char val[3]; - *reinterpret_cast(&val[0]) = htons(code); - val[2] = 0x00; + const uint16_t payload = htons(code); - msg->set_payload(std::string(val)); + msg->set_payload(std::string(reinterpret_cast(&payload), 2)); msg->append_payload(reason); // prepare rest of frame diff --git a/src/processors/hybi_legacy.hpp b/src/processors/hybi_legacy.hpp index 0fcba1c784..b7cf6c6e41 100644 --- a/src/processors/hybi_legacy.hpp +++ b/src/processors/hybi_legacy.hpp @@ -62,12 +62,10 @@ public: char key_final[16]; // copy key1 into final key - *reinterpret_cast(&key_final[0]) = - decode_client_key(request.header("Sec-WebSocket-Key1")); + decode_client_key(request.header("Sec-WebSocket-Key1"), &key_final[0]); // copy key2 into final key - *reinterpret_cast(&key_final[4]) = - decode_client_key(request.header("Sec-WebSocket-Key2")); + decode_client_key(request.header("Sec-WebSocket-Key2"), &key_final[4]); // copy key3 into final key memcpy(&key_final[8],request.header("Sec-WebSocket-Key3").c_str(),8); @@ -292,7 +290,7 @@ public: } private: - uint32_t decode_client_key(const std::string& key) { + void decode_client_key(const std::string& key, char* result) { int spaces = 0; std::string digits = ""; uint32_t num; @@ -308,9 +306,10 @@ private: num = atoi(digits.c_str()); if (spaces > 0 && num > 0) { - return htonl(num/spaces); + num = htonl(num/spaces); + memcpy(result, reinterpret_cast(&num), 4); } else { - return 0; + memset(result, 0, 4); } } diff --git a/src/websocket_frame.hpp b/src/websocket_frame.hpp index 3c39f620ca..c4bc0d68b0 100644 --- a/src/websocket_frame.hpp +++ b/src/websocket_frame.hpp @@ -334,12 +334,7 @@ public: if (get_payload_size() == 0) { return close::status::NO_STATUS; } else if (get_payload_size() >= 2) { - char val[2]; - - val[0] = m_payload[0]; - val[1] = m_payload[1]; - - uint16_t code = ntohs(*(reinterpret_cast(&val[0]))); + uint16_t code = ntohs(reinterpret_cast(m_payload[1]) | reinterpret_cast(m_payload[0]) << 8); return close::status::value(code); } else { From 76296fe2dd49c55d85f818005ea9bb6901807c79 Mon Sep 17 00:00:00 2001 From: Sven Almgren Date: Thu, 15 Mar 2012 16:15:41 +0100 Subject: [PATCH 2/2] Fixed broken conversion --- src/websocket_frame.hpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/websocket_frame.hpp b/src/websocket_frame.hpp index c4bc0d68b0..f41d7623b4 100644 --- a/src/websocket_frame.hpp +++ b/src/websocket_frame.hpp @@ -334,7 +334,11 @@ public: if (get_payload_size() == 0) { return close::status::NO_STATUS; } else if (get_payload_size() >= 2) { - uint16_t code = ntohs(reinterpret_cast(m_payload[1]) | reinterpret_cast(m_payload[0]) << 8); + char val[2] = { m_payload[0], m_payload[1] }; + uint16_t code; + + memcpy(&code, val, sizeof(code)); + code = ntohs(code); return close::status::value(code); } else {