Skip to content

Commit e527093

Browse files
committed
Fix: Prevent unaligned access in ping function
Previously we were doing some UB stuff -- using reinterpret cast on a byte buffer. Instead we use std::memcpy to copy a quint32 to the buffer -- this is portable and not UB.
1 parent 06c4ac9 commit e527093

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

WebSocket.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343

4444
#include <algorithm>
4545
#include <cassert>
46+
#include <cstddef>
4647
#include <chrono>
4748
#include <cstring>
4849
#include <limits>
@@ -1023,7 +1024,11 @@ namespace WebSocket
10231024
return;
10241025
}
10251026
QByteArray data(sizeof(quint32), Qt::Uninitialized);
1026-
*reinterpret_cast<quint32 *>(data.data()) = QRandomGenerator::global()->generate();
1027+
{
1028+
const quint32 randVal = QRandomGenerator::global()->generate();
1029+
// prevent unaligned access
1030+
std::memcpy(data.data(), reinterpret_cast<const std::byte *>(&randVal), sizeof(randVal));
1031+
}
10271032
sendPing(data);
10281033
if (getTime() - lastPongRecvd >= pingTimer->interval()*2) {
10291034
qDebug() << "Ping timeout for" << QString::asprintf("%s:%hu", peerAddress().toString().toUtf8().constData(), peerPort());

0 commit comments

Comments
 (0)