Fix misaligned read in physical_socket_server
which causes errors under ASAN/UBSAN
BUG=None
Change-Id: I36bf4ff706339b6e3a8fd3764551e98fce111ad7
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/365249
Reviewed-by: Danil Chapovalov <danilchap@webrtc.org>
Reviewed-by: Per Kjellander <perkj@webrtc.org>
Reviewed-by: Harald Alvestrand <hta@webrtc.org>
Commit-Queue: Philipp Hancke <phancke@meta.com>
Cr-Commit-Position: refs/heads/main@{#43243}
diff --git a/rtc_base/physical_socket_server.cc b/rtc_base/physical_socket_server.cc
index 71da31e..af133b8 100644
--- a/rtc_base/physical_socket_server.cc
+++ b/rtc_base/physical_socket_server.cc
@@ -10,6 +10,7 @@
#include "rtc_base/physical_socket_server.h"
#include <cstdint>
+#include <cstring>
#include <utility>
#if defined(_MSC_VER) && _MSC_VER < 1300
@@ -549,10 +550,11 @@
if (cmsg->cmsg_level != SOL_SOCKET)
continue;
if (timestamp && cmsg->cmsg_type == SCM_TIMESTAMP) {
- timeval* ts = reinterpret_cast<timeval*>(CMSG_DATA(cmsg));
+ timeval ts;
+ std::memcpy(static_cast<void*>(&ts), CMSG_DATA(cmsg), sizeof(ts));
*timestamp =
- rtc::kNumMicrosecsPerSec * static_cast<int64_t>(ts->tv_sec) +
- static_cast<int64_t>(ts->tv_usec);
+ rtc::kNumMicrosecsPerSec * static_cast<int64_t>(ts.tv_sec) +
+ static_cast<int64_t>(ts.tv_usec);
}
}
}