Skip to content

Commit

Permalink
CWebSocketFrame: Fix alignment issues
Browse files Browse the repository at this point in the history
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<bool>) const xbmc/threads/Thread.cpp:152:18
    #8 0x5635fca212d6 in void std::__invoke_impl<void, CThread::Create(bool)::$_0, CThread*, std::promise<bool>>(std::__invoke_other, CThread::Create(bool)::$_0&&, CThread*&&, std::promise<bool>&&) /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<CThread::Create(bool)::$_0, CThread*, std::promise<bool>>::type std::__invoke<CThread::Create(bool)::$_0, CThread*, std::promise<bool>>(CThread::Create(bool)::$_0&&, CThread*&&, std::promise<bool>&&) /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<std::tuple<CThread::Create(bool)::$_0, CThread*, std::promise<bool>>>::_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<std::tuple<CThread::Create(bool)::$_0, CThread*, std::promise<bool>>>::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<std::thread::_Invoker<std::tuple<CThread::Create(bool)::$_0, CThread*, std::promise<bool>>>>::_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
  • Loading branch information
neo1973 committed Aug 20, 2023
1 parent cb02c86 commit f6e6155
Showing 1 changed file with 7 additions and 3 deletions.
10 changes: 7 additions & 3 deletions xbmc/network/websocket/WebSocket.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include "utils/StringUtils.h"
#include "utils/log.h"

#include <cstdint>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -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;
}

Expand All @@ -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;
}

Expand Down

0 comments on commit f6e6155

Please sign in to comment.