From: Steinar H. Gunderson Date: Sun, 18 Aug 2013 00:48:13 +0000 (+0200) Subject: Use memcpy instead of incurring potential unaligned reads when reading the Metacube... X-Git-Tag: 1.0.0~8 X-Git-Url: https://git.sesse.net/?p=cubemap;a=commitdiff_plain;h=22cd7233eabd1d012f29e890ec67c6b70a0d6753 Use memcpy instead of incurring potential unaligned reads when reading the Metacube header. This would probably be a portability problem for many other platforms than x86. --- diff --git a/httpinput.cpp b/httpinput.cpp index 80daa1a..c923145 100644 --- a/httpinput.cpp +++ b/httpinput.cpp @@ -535,16 +535,17 @@ void HTTPInput::process_data(char *ptr, size_t bytes) } // Now it's safe to read the header. - metacube2_block_header *hdr = reinterpret_cast(pending_data.data()); - assert(memcmp(hdr->sync, METACUBE2_SYNC, sizeof(hdr->sync)) == 0); - uint32_t size = ntohl(hdr->size); - uint16_t flags = ntohs(hdr->flags); - uint16_t expected_csum = metacube2_compute_crc(hdr); - - if (expected_csum != ntohs(hdr->csum)) { + metacube2_block_header hdr; + memcpy(&hdr, pending_data.data(), sizeof(hdr)); + assert(memcmp(hdr.sync, METACUBE2_SYNC, sizeof(hdr.sync)) == 0); + uint32_t size = ntohl(hdr.size); + uint16_t flags = ntohs(hdr.flags); + uint16_t expected_csum = metacube2_compute_crc(&hdr); + + if (expected_csum != ntohs(hdr.csum)) { log(WARNING, "[%s] Metacube checksum failed (expected 0x%x, got 0x%x), " "not reading block claiming to be %d bytes (flags=%x).", - url.c_str(), expected_csum, ntohs(hdr->csum), + url.c_str(), expected_csum, ntohs(hdr.csum), size, flags); // Drop only the first byte, and let the rest of the code handle resync.