From f6e615584446f09e0b297cc74301e474d99b3849 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Markus=20H=C3=A4rer?= Date: Sun, 20 Aug 2023 21:19:44 +0200 Subject: [PATCH] CWebSocketFrame: Fix alignment issues UBSAN error: xbmc/network/websocket/WebSocket.cpp:107:14: runtime error: load of misaligned address 0x63100021c802 for type 'const uint32_t' (aka 'const unsigned int'), which requires 4 byte alignment 0x63100021c802: note: pointer points here 00 00 88 82 cf d3 5c c3 cc 3a 00 be be be be be be be be be be be be be be be be be be be be be ^ #0 0x56360048bf64 in CWebSocketFrame::CWebSocketFrame(char const*, unsigned long) xbmc/network/websocket/WebSocket.cpp:107:14 #1 0x5636004a6905 in CWebSocketV8::GetFrame(char const*, unsigned long) xbmc/network/websocket/WebSocketV8.cpp:145:14 #2 0x563600491ec9 in CWebSocket::Handle(char const*&, unsigned long&, bool&) xbmc/network/websocket/WebSocket.cpp:298:34 #3 0x5636005b05dd in JSONRPC::CTCPServer::CWebSocketClient::PushBuffer(JSONRPC::CTCPServer*, char const*, int) xbmc/network/TCPServer.cpp:716:29 #4 0x5636005a3760 in JSONRPC::CTCPServer::Process() xbmc/network/TCPServer.cpp:171:33 #5 0x5636005a6858 in non-virtual thunk to JSONRPC::CTCPServer::Process() xbmc/network/TCPServer.cpp #6 0x5635fca1fe32 in CThread::Action() xbmc/threads/Thread.cpp:283:5 #7 0x5635fca225f6 in CThread::Create(bool)::$_0::operator()(CThread*, std::promise) const xbmc/threads/Thread.cpp:152:18 #8 0x5635fca212d6 in void std::__invoke_impl>(std::__invoke_other, CThread::Create(bool)::$_0&&, CThread*&&, std::promise&&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/invoke.h:61:14 #9 0x5635fca20f06 in std::__invoke_result>::type std::__invoke>(CThread::Create(bool)::$_0&&, CThread*&&, std::promise&&) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/invoke.h:96:14 #10 0x5635fca20e3f in void std::thread::_Invoker>>::_M_invoke<0ul, 1ul, 2ul>(std::_Index_tuple<0ul, 1ul, 2ul>) /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/std_thread.h:292:13 #11 0x5635fca20cb8 in std::thread::_Invoker>>::operator()() /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/std_thread.h:299:11 #12 0x5635fca20888 in std::thread::_State_impl>>>::_M_run() /usr/bin/../lib64/gcc/x86_64-pc-linux-gnu/13.2.1/../../../../include/c++/13.2.1/bits/std_thread.h:244:13 #13 0x7f03890e1942 in execute_native_thread_routine /usr/src/debug/gcc/gcc/libstdc++-v3/src/c++11/thread.cc:104:18 #14 0x7f038a88c9ea (/usr/lib/libc.so.6+0x8c9ea) (BuildId: 316d0d3666387f0e8fb98773f51aa1801027c5ab) #15 0x7f038a910dfb (/usr/lib/libc.so.6+0x110dfb) (BuildId: 316d0d3666387f0e8fb98773f51aa1801027c5ab) SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior xbmc/network/websocket/WebSocket.cpp:107:14 in --- xbmc/network/websocket/WebSocket.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/xbmc/network/websocket/WebSocket.cpp b/xbmc/network/websocket/WebSocket.cpp index bbf4a017f66aa..ecec4efeee9a5 100644 --- a/xbmc/network/websocket/WebSocket.cpp +++ b/xbmc/network/websocket/WebSocket.cpp @@ -13,6 +13,7 @@ #include "utils/StringUtils.h" #include "utils/log.h" +#include #include #include @@ -85,12 +86,15 @@ CWebSocketFrame::CWebSocketFrame(const char* data, uint64_t length) int offset = 0; if (m_length == 126) { - m_length = (uint64_t)Endian_SwapBE16(*(const uint16_t *)(m_data + 2)); + uint16_t length; + std::memcpy(&length, m_data + 2, 2); + m_length = Endian_SwapBE16(length); offset = 2; } else if (m_length == 127) { - m_length = Endian_SwapBE64(*(const uint64_t *)(m_data + 2)); + std::memcpy(&m_length, m_data + 2, 8); + m_length = Endian_SwapBE64(m_length); offset = 8; } @@ -104,7 +108,7 @@ CWebSocketFrame::CWebSocketFrame(const char* data, uint64_t length) // Get the mask if (m_masked) { - m_mask = *(const uint32_t *)(m_data + LENGTH_MIN + offset); + std::memcpy(&m_mask, m_data + LENGTH_MIN + offset, 4); offset += 4; }