talloc: Fix alignment issues for casting pointers
authorAndreas Schneider <asn@samba.org>
Fri, 12 Oct 2018 09:58:26 +0000 (11:58 +0200)
committerAndreas Schneider <asn@cryptomilk.org>
Tue, 19 Mar 2019 12:38:50 +0000 (12:38 +0000)
warning: cast from 'char *' to 'struct talloc_chunk *' increases required
alignment from 1 to 8

Signed-off-by: Andreas Schneider <asn@samba.org>
Reviewed-by: Volker Lendecke <vl@samba.org>
Autobuild-User(master): Andreas Schneider <asn@cryptomilk.org>
Autobuild-Date(master): Tue Mar 19 12:38:50 UTC 2019 on sn-devel-144

lib/talloc/talloc.c

index 073a3e50d4b327d0c444e248e8ed7f325badaf7c..518ffbdbfdf54e42372ca8c1109dcc220cce5e52 100644 (file)
@@ -317,6 +317,11 @@ struct talloc_chunk {
        struct talloc_pool_hdr *pool;
 };
 
+union talloc_chunk_cast_u {
+       uint8_t *ptr;
+       struct talloc_chunk *chunk;
+};
+
 /* 16 byte alignment seems to keep everyone happy */
 #define TC_ALIGN16(s) (((s)+15)&~15)
 #define TC_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_chunk))
@@ -611,16 +616,25 @@ struct talloc_pool_hdr {
        size_t poolsize;
 };
 
+union talloc_pool_hdr_cast_u {
+       uint8_t *ptr;
+       struct talloc_pool_hdr *hdr;
+};
+
 #define TP_HDR_SIZE TC_ALIGN16(sizeof(struct talloc_pool_hdr))
 
 static inline struct talloc_pool_hdr *talloc_pool_from_chunk(struct talloc_chunk *c)
 {
-       return (struct talloc_pool_hdr *)((char *)c - TP_HDR_SIZE);
+       union talloc_chunk_cast_u tcc = { .chunk = c };
+       union talloc_pool_hdr_cast_u tphc = { tcc.ptr - TP_HDR_SIZE };
+       return tphc.hdr;
 }
 
 static inline struct talloc_chunk *talloc_chunk_from_pool(struct talloc_pool_hdr *h)
 {
-       return (struct talloc_chunk *)((char *)h + TP_HDR_SIZE);
+       union talloc_pool_hdr_cast_u tphc = { .hdr = h };
+       union talloc_chunk_cast_u tcc = { .ptr = tphc.ptr + TP_HDR_SIZE };
+       return tcc.chunk;
 }
 
 static inline void *tc_pool_end(struct talloc_pool_hdr *pool_hdr)
@@ -668,6 +682,7 @@ static inline struct talloc_chunk *tc_alloc_pool(struct talloc_chunk *parent,
                                                     size_t size, size_t prefix_len)
 {
        struct talloc_pool_hdr *pool_hdr = NULL;
+       union talloc_chunk_cast_u tcc;
        size_t space_left;
        struct talloc_chunk *result;
        size_t chunk_size;
@@ -698,7 +713,10 @@ static inline struct talloc_chunk *tc_alloc_pool(struct talloc_chunk *parent,
                return NULL;
        }
 
-       result = (struct talloc_chunk *)((char *)pool_hdr->end + prefix_len);
+       tcc = (union talloc_chunk_cast_u) {
+               .ptr = ((uint8_t *)pool_hdr->end) + prefix_len
+       };
+       result = tcc.chunk;
 
 #if defined(DEVELOPER) && defined(VALGRIND_MAKE_MEM_UNDEFINED)
        VALGRIND_MAKE_MEM_UNDEFINED(pool_hdr->end, chunk_size);
@@ -750,7 +768,8 @@ static inline void *__talloc_with_prefix(const void *context,
        }
 
        if (tc == NULL) {
-               char *ptr;
+               uint8_t *ptr = NULL;
+               union talloc_chunk_cast_u tcc;
 
                /*
                 * Only do the memlimit check/update on actual allocation.
@@ -764,7 +783,8 @@ static inline void *__talloc_with_prefix(const void *context,
                if (unlikely(ptr == NULL)) {
                        return NULL;
                }
-               tc = (struct talloc_chunk *)(ptr + prefix_len);
+               tcc = (union talloc_chunk_cast_u) { .ptr = ptr + prefix_len };
+               tc = tcc.chunk;
                tc->flags = talloc_magic;
                tc->pool  = NULL;