]> arthur.barton.de Git - bup.git/commitdiff
_helpers.c: be careful about memory alignment in bloom operations.
authorJulien Cristau <jcristau@debian.org>
Thu, 8 Aug 2013 22:30:26 +0000 (18:30 -0400)
committerRob Browning <rlb@defaultvalue.org>
Sun, 11 Aug 2013 20:04:24 +0000 (15:04 -0500)
Debian (Bug#641488) discovered via the bloom test that bup crashes on
sparc because it's trying to access unaligned memory.  Fix that.

Signed-off-by: Julien Cristau <jcristau@debian.org>
[rlb@defaultvalue.org: adjust commit message.]
Reviewed-by: Rob Browning <rlb@defaultvalue.org>
lib/bup/_helpers.c

index afc59a36adb5f4b0803924c9247b3929d6277382..a8e38ba935fa7073a9d71dc51fe6e1fb51604d2a 100644 (file)
@@ -170,59 +170,58 @@ static PyObject *firstword(PyObject *self, PyObject *args)
 
 #define BLOOM2_HEADERLEN 16
 
-typedef struct {
-    uint32_t high;
-    unsigned char low;
-} bits40_t;
-
-static void to_bloom_address_bitmask4(const bits40_t *buf,
+static void to_bloom_address_bitmask4(const unsigned char *buf,
        const int nbits, uint64_t *v, unsigned char *bitmask)
 {
     int bit;
+    uint32_t high;
     uint64_t raw, mask;
 
+    memcpy(&high, buf, 4);
     mask = (1<<nbits) - 1;
-    raw = (((uint64_t)ntohl(buf->high)) << 8) | buf->low;
+    raw = (((uint64_t)ntohl(high) << 8) | buf[4]);
     bit = (raw >> (37-nbits)) & 0x7;
     *v = (raw >> (40-nbits)) & mask;
     *bitmask = 1 << bit;
 }
 
-static void to_bloom_address_bitmask5(const uint32_t *buf,
+static void to_bloom_address_bitmask5(const unsigned char *buf,
        const int nbits, uint32_t *v, unsigned char *bitmask)
 {
     int bit;
+    uint32_t high;
     uint32_t raw, mask;
 
+    memcpy(&high, buf, 4);
     mask = (1<<nbits) - 1;
-    raw = ntohl(*buf);
+    raw = ntohl(high);
     bit = (raw >> (29-nbits)) & 0x7;
     *v = (raw >> (32-nbits)) & mask;
     *bitmask = 1 << bit;
 }
 
-#define BLOOM_SET_BIT(name, address, itype, otype) \
-static void name(unsigned char *bloom, const void *buf, const int nbits)\
+#define BLOOM_SET_BIT(name, address, otype) \
+static void name(unsigned char *bloom, const unsigned char *buf, const int nbits)\
 {\
     unsigned char bitmask;\
     otype v;\
-    address((itype *)buf, nbits, &v, &bitmask);\
+    address(buf, nbits, &v, &bitmask);\
     bloom[BLOOM2_HEADERLEN+v] |= bitmask;\
 }
-BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
-BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
+BLOOM_SET_BIT(bloom_set_bit4, to_bloom_address_bitmask4, uint64_t)
+BLOOM_SET_BIT(bloom_set_bit5, to_bloom_address_bitmask5, uint32_t)
 
 
-#define BLOOM_GET_BIT(name, address, itype, otype) \
-static int name(const unsigned char *bloom, const void *buf, const int nbits)\
+#define BLOOM_GET_BIT(name, address, otype) \
+static int name(const unsigned char *bloom, const unsigned char *buf, const int nbits)\
 {\
     unsigned char bitmask;\
     otype v;\
-    address((itype *)buf, nbits, &v, &bitmask);\
+    address(buf, nbits, &v, &bitmask);\
     return bloom[BLOOM2_HEADERLEN+v] & bitmask;\
 }
-BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, bits40_t, uint64_t)
-BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t, uint32_t)
+BLOOM_GET_BIT(bloom_get_bit4, to_bloom_address_bitmask4, uint64_t)
+BLOOM_GET_BIT(bloom_get_bit5, to_bloom_address_bitmask5, uint32_t)
 
 
 static PyObject *bloom_add(PyObject *self, PyObject *args)