Avoid `reinterpret_cast<uint16_t*>` when computing checksum
Correct a potential alignment problem by replacing access to a `uint16_t` through a `reinterpret_cast` with access through a `memcpy`. This generates identical code on x86_64, ARMv7, and ARMv8, but it’s safe and portable to platforms that don’t support unaligned access.
PiperOrigin-RevId: 451467992
diff --git a/quiche/quic/qbone/platform/internet_checksum.cc b/quiche/quic/qbone/platform/internet_checksum.cc
index e819f37..f9901e5 100644
--- a/quiche/quic/qbone/platform/internet_checksum.cc
+++ b/quiche/quic/qbone/platform/internet_checksum.cc
@@ -4,15 +4,20 @@
#include "quiche/quic/qbone/platform/internet_checksum.h"
+#include <stdint.h>
+#include <string.h>
+
namespace quic {
void InternetChecksum::Update(const char* data, size_t size) {
const char* current;
for (current = data; current + 1 < data + size; current += 2) {
- accumulator_ += *reinterpret_cast<const uint16_t*>(current);
+ uint16_t v;
+ memcpy(&v, current, sizeof(v));
+ accumulator_ += v;
}
if (current < data + size) {
- accumulator_ += *reinterpret_cast<const uint8_t*>(current);
+ accumulator_ += *reinterpret_cast<const unsigned char*>(current);
}
}