Skip to content

Commit 82f9962

Browse files
Ray ChungRay Chung
Ray Chung
authored and
Ray Chung
committed
Fix misaligned dereference in H264 Parser
Use memcpy instead of direct dereference to avoid misaligned access.
1 parent fbd8f23 commit 82f9962

File tree

2 files changed

+7
-3
lines changed

2 files changed

+7
-3
lines changed

examples/streamer/h264fileparser.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "rtc/rtc.hpp"
1212

1313
#include <fstream>
14+
#include <cstring>
1415

1516
#ifdef _WIN32
1617
#include <winsock2.h>
@@ -29,7 +30,9 @@ void H264FileParser::loadNextSample() {
2930
while (i < sample.size()) {
3031
assert(i + 4 < sample.size());
3132
auto lengthPtr = (uint32_t *) (sample.data() + i);
32-
uint32_t length = ntohl(*lengthPtr);
33+
uint32_t length;
34+
std::memcpy(&length, lengthPtr, sizeof(uint32_t));
35+
length = ntohl(length);
3336
auto naluStartIndex = i + 4;
3437
auto naluEndIndex = naluStartIndex + length;
3538
assert(naluEndIndex <= sample.size());

src/h264rtppacketizer.cpp

+3-2
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,9 @@ shared_ptr<NalUnits> H264RtpPacketizer::splitMessage(binary_ptr message) {
3232
LOG_WARNING << "Invalid NAL Unit data (incomplete length), ignoring!";
3333
break;
3434
}
35-
auto lengthPtr = (uint32_t *)(message->data() + index);
36-
uint32_t length = ntohl(*lengthPtr);
35+
uint32_t length;
36+
std::memcpy(&length, message->data() + index, sizeof(uint32_t));
37+
length = ntohl(length);
3738
auto naluStartIndex = index + 4;
3839
auto naluEndIndex = naluStartIndex + length;
3940

0 commit comments

Comments
 (0)