From 8ece83bb230a2ddc242139f2cb0f017fbf81ed86 Mon Sep 17 00:00:00 2001 From: Heikki Linnakangas Date: Wed, 2 Dec 2015 16:23:56 +0200 Subject: [PATCH] Fix alignment in FileRep_CalculateParity. This was causing a segfault when compiling with -O3 on Centos 7 (gcc (GCC) 4.8.3 20140911 (Red Hat 4.8.3-9)). I'm not 100% if that was actually a compiler bug, but unaligned access would definitely be wrong on architectures that are strict on alignment anyway. --- src/backend/cdb/cdbfilerep.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/backend/cdb/cdbfilerep.c b/src/backend/cdb/cdbfilerep.c index 76256356d9..68854ac623 100644 --- a/src/backend/cdb/cdbfilerep.c +++ b/src/backend/cdb/cdbfilerep.c @@ -2787,7 +2787,21 @@ FileRep_CalculateParity(unsigned char *buf, int len) /* Get 64-bits at a time, xor into temp */ for (n = 0; n < words; n++) - temp ^= ((unsigned long long*)buf)[n]; + { + unsigned long long w; + + /* + * Use memcpy because 'buf' might not be aligned. (Even on x86, which + * does not usually require alignment for memory accesses, the + * compiler might decide to optimize this with MMX instructions - + * which do require 16-bit alignment. We've seen this happen with gcc + * -O3 with some compiler versions. OTOH, as long as the target + * architecture doesn't require alignment, this will be optimized + * away and not cost anything.) + */ + memcpy(&w, &buf[n * 8], 8); + temp ^= w; + } /* Combine each of the 8 bytes from temp into parity, so we get the same answer as byte-by-byte loop */ /* -- GitLab