Skip to content

Commit 017b451

Browse files
resistordavidchisnall
authored andcommitted
Switch to using an unaligned read helper that does the reads via memcpy.
1 parent 4b7d558 commit 017b451

File tree

3 files changed

+22
-13
lines changed

3 files changed

+22
-13
lines changed

include/endianness.hh

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright SCI Semiconductor and CHERIoT Contributors.
22
// SPDX-License-Identifier: MIT
33

4+
#include "cdefs.h"
45
#include <stdint.h>
56

67
uint16_t constexpr ntohs(uint16_t value)
@@ -24,3 +25,11 @@ uint16_t constexpr htons(uint16_t value)
2425
#endif
2526
;
2627
}
28+
29+
template<class T>
30+
__always_inline T read_unaligned(const void *p)
31+
{
32+
T val;
33+
__builtin_memcpy(&val, p, sizeof(T));
34+
return val;
35+
}

lib/dns/dns.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -808,12 +808,12 @@ namespace
808808
if (isIPv6)
809809
{
810810
queryResult.kind = NetworkAddress::AddressKindIPv6;
811-
uint16_t *ipv6 = reinterpret_cast<uint16_t *>(&queryResult.ipv6[0]);
811+
uint8_t *ipv6 = queryResult.ipv6;
812812
// Enforce machine byte order by block of 2 byte.
813813
for (int i = 0; i < 8; i++)
814814
{
815-
*ipv6++ = ntohs(
816-
*reinterpret_cast<uint16_t *>(dnsPacket + currentOffset));
815+
*ipv6++ =
816+
ntohs(read_unaligned<uint16_t>(dnsPacket + currentOffset));
817817
currentOffset += 2;
818818
}
819819
}
@@ -1110,15 +1110,15 @@ __cheri_compartment("DNS") int network_host_resolve(Timeout *timeout,
11101110
{
11111111
Debug::log("Resolved {} -> {}:{}:{}:{}:{}:{}:{}:{}",
11121112
hostname,
1113-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[0]),
1114-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[2]),
1115-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[4]),
1116-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[6]),
1117-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[8]),
1118-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[10]),
1119-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[12]),
1120-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[14]),
1121-
*reinterpret_cast<uint16_t *>(&queryResult.ipv6[16]));
1113+
read_unaligned<uint16_t>(&queryResult.ipv6[0]),
1114+
read_unaligned<uint16_t>(&queryResult.ipv6[2]),
1115+
read_unaligned<uint16_t>(&queryResult.ipv6[4]),
1116+
read_unaligned<uint16_t>(&queryResult.ipv6[6]),
1117+
read_unaligned<uint16_t>(&queryResult.ipv6[8]),
1118+
read_unaligned<uint16_t>(&queryResult.ipv6[10]),
1119+
read_unaligned<uint16_t>(&queryResult.ipv6[12]),
1120+
read_unaligned<uint16_t>(&queryResult.ipv6[14]),
1121+
read_unaligned<uint16_t>(&queryResult.ipv6[16]));
11221122
}
11231123

11241124
// We are now good to process the next lookup.

lib/dns/protocol-headers.hh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ uint16_t compute_ipv4_checksum(const uint8_t *header, uint16_t length)
6868

6969
while (length > 1)
7070
{
71-
sum += *reinterpret_cast<const uint16_t *>(header);
71+
sum += read_unaligned<uint16_t>(header);
7272
header += 2;
7373
length -= 2;
7474
}

0 commit comments

Comments
 (0)