Skip to content

Commit

Permalink
Fix a lot of "cast increases required alignment of target data type"
Browse files Browse the repository at this point in the history
which is particularly bad in ARM32 (aka armhf). Straighten the code
where possible, drop in (void *) to break alignment "chain" in other
cases. This hopefully will make ARM32 build for core warning-free.
  • Loading branch information
sobomax committed Nov 28, 2020
1 parent 22f8efa commit fff57ac
Show file tree
Hide file tree
Showing 19 changed files with 95 additions and 71 deletions.
17 changes: 10 additions & 7 deletions bin_interface.c
Expand Up @@ -33,7 +33,7 @@ static int bin_extend(bin_packet_t *packet, int size);
static struct packet_cb_list *reg_cbs;

void set_len(bin_packet_t *packet) {
*(unsigned int *)(packet->buffer.s + BIN_PACKET_MARKER_SIZE) = packet->buffer.len;
memcpy(packet->buffer.s + BIN_PACKET_MARKER_SIZE, &packet->buffer.len, sizeof(unsigned int));
}

/**
Expand Down Expand Up @@ -96,7 +96,7 @@ int bin_init(bin_packet_t *packet, str *capability, int packet_type,

void bin_get_capability(bin_packet_t *packet, str *capability)
{
capability->len = *(unsigned short *)(packet->buffer.s + HEADER_SIZE);
memcpy(&capability->len, packet->buffer.s + HEADER_SIZE, sizeof(unsigned short));
capability->s = packet->buffer.s + HEADER_SIZE + LEN_FIELD_SIZE;
}

Expand All @@ -115,7 +115,7 @@ void bin_init_buffer(bin_packet_t *packet, char *buffer, int length)

bin_get_capability(packet, &capability);

packet->type = *(int *)(capability.s + capability.len);
memcpy(&packet->type, capability.s + capability.len, sizeof(int));
packet->front_pointer = capability.s + capability.len + CMD_FIELD_SIZE;
LM_DBG("init buffer length %d\n", length);
}
Expand Down Expand Up @@ -431,7 +431,7 @@ void call_callbacks(char* buffer, struct receive_info *rcv)
bin_packet_t packet;
str capability;

pkg_len = *(unsigned int*)(buffer + BIN_PACKET_MARKER_SIZE);
memcpy(&pkg_len, buffer + BIN_PACKET_MARKER_SIZE, sizeof(unsigned int));
//add extra size so a realloc wont trigger after small altering of the packet
packet.buffer.s = pkg_malloc(pkg_len + 50);
if (!packet.buffer.s) {
Expand All @@ -446,7 +446,7 @@ void call_callbacks(char* buffer, struct receive_info *rcv)
bin_get_capability(&packet, &capability);

packet.front_pointer = capability.s + capability.len + CMD_FIELD_SIZE;
packet.type = *(int *)(capability.s + capability.len);
memcpy(&packet.type, capability.s + capability.len, sizeof(int));
packet.next = NULL;

/* packet will be now processed for a specific capability */
Expand Down Expand Up @@ -514,11 +514,14 @@ int bin_get_buffer(bin_packet_t *packet, str *buffer)

int bin_get_content_start(bin_packet_t *packet, str *buf)
{
unsigned short clen;

if (!buf)
return -1;

memcpy(&clen, packet->buffer.s + HEADER_SIZE, sizeof(clen));
buf->s = packet->buffer.s + HEADER_SIZE + LEN_FIELD_SIZE +
*(unsigned short *)(packet->buffer.s + HEADER_SIZE) + sizeof(int);
clen + sizeof(int);
buf->len = packet->buffer.len - (buf->s - packet->buffer.s);

return buf->len;
Expand All @@ -542,7 +545,7 @@ int bin_reset_back_pointer(bin_packet_t *packet)
if (!packet->buffer.s || !packet->size)
return -1;

cap_len = *(unsigned short*)(packet->buffer.s + HEADER_SIZE);
memcpy(&cap_len, packet->buffer.s + HEADER_SIZE, sizeof(unsigned short));

packet->buffer.len = HEADER_SIZE + LEN_FIELD_SIZE + CMD_FIELD_SIZE + cap_len;

Expand Down
11 changes: 7 additions & 4 deletions bin_interface.h
Expand Up @@ -81,15 +81,18 @@ struct packet_cb_list {
/* returns the version of the bin protocol from the given message */
static inline short get_bin_pkg_version(bin_packet_t *packet)
{
return *(short *)(packet->buffer.s + BIN_PACKET_MARKER_SIZE
+ PKG_LEN_FIELD_SIZE);
short rval;

memcpy(&rval, packet->buffer.s + BIN_PACKET_MARKER_SIZE
+ PKG_LEN_FIELD_SIZE, sizeof(rval));
return rval;
}

/* overrides the version of the bin protocol from the given message */
static inline void set_bin_pkg_version(bin_packet_t *packet, short new_version)
{
*(short *)(packet->buffer.s + BIN_PACKET_MARKER_SIZE
+ PKG_LEN_FIELD_SIZE) = new_version;
memcpy(packet->buffer.s + BIN_PACKET_MARKER_SIZE
+ PKG_LEN_FIELD_SIZE, &new_version, sizeof(new_version));
}

/*
Expand Down
9 changes: 4 additions & 5 deletions context.h
Expand Up @@ -135,8 +135,7 @@ static inline void context_put_str(enum osips_context type, context_p ctx,
abort();
}
#endif

((str *)((char *)ctx + type_offsets[type][CONTEXT_STR_TYPE]))[pos] = *data;
((str *)(void *)((char *)ctx + type_offsets[type][CONTEXT_STR_TYPE]))[pos] = *data;
}

static inline void context_put_ptr(enum osips_context type, context_p ctx,
Expand All @@ -149,7 +148,7 @@ static inline void context_put_ptr(enum osips_context type, context_p ctx,
}
#endif

((void **)((char *)ctx + type_offsets[type][CONTEXT_PTR_TYPE]))[pos] = data;
((void **)(void *)((char *)ctx + type_offsets[type][CONTEXT_PTR_TYPE]))[pos] = data;
}

/****************************** GETTERS ********************************/
Expand Down Expand Up @@ -177,7 +176,7 @@ static inline str *context_get_str(enum osips_context type,
}
#endif

return &((str *)((char *)ctx + type_offsets[type][CONTEXT_STR_TYPE]))[pos];
return &((str *)(void *)((char *)ctx + type_offsets[type][CONTEXT_STR_TYPE]))[pos];
}

static inline void *context_get_ptr(enum osips_context type,
Expand All @@ -190,7 +189,7 @@ static inline void *context_get_ptr(enum osips_context type,
}
#endif

return ((void **)((char *)ctx +
return ((void **)(void *)((char *)ctx +
type_offsets[type][CONTEXT_PTR_TYPE]))[pos];
}

Expand Down
9 changes: 4 additions & 5 deletions db/db_insertq.c
Expand Up @@ -373,23 +373,22 @@ query_list_t *ql_init(db_con_t *con,db_key_t *cols,int col_no)
memcpy(entry->table.s,con->table->s,con->table->len);

/* deal with the columns */
entry->cols = (db_key_t *)((char *)entry+sizeof(query_list_t)+
entry->cols = (db_key_t *)(void *)((char *)entry+sizeof(query_list_t)+
con->table->len);
entry->col_no = col_no;

pos = (char *)(entry->cols + col_no) + col_no * sizeof(str);
for (i=0;i<col_no;i++)
{
entry->cols[i] = (str *)((char *)(entry->cols + col_no) +
i * sizeof(str));
entry->cols[i] = (str *)(entry->cols + col_no) + i;
entry->cols[i]->len = cols[i]->len;
entry->cols[i]->s = pos;
memcpy(pos,cols[i]->s,cols[i]->len);
pos += cols[i]->len;
}

/* deal with the rows */
entry->rows = (db_val_t **)((char *)entry + sizeof(query_list_t) +
entry->rows = (db_val_t **)(void *)((char *)(entry + 1) +
con->table->len + key_size);

/* save url for later use by timer */
Expand All @@ -399,7 +398,7 @@ query_list_t *ql_init(db_con_t *con,db_key_t *cols,int col_no)
memcpy(entry->url.s,con->url.s,con->url.len);

/* build array of connections per process */
entry->conn = (db_con_t**)((char *)entry + sizeof(query_list_t) +
entry->conn = (db_con_t**)(void *)((char *)(entry + 1) +
con->table->len + key_size + row_q_size + con->url.len);

LM_DBG("initialized query list for table [%.*s]\n",entry->table.len,entry->table.s);
Expand Down
4 changes: 2 additions & 2 deletions db/db_res.c
Expand Up @@ -168,7 +168,7 @@ int db_allocate_rows(db_res_t* _res, const unsigned int rows)
for( i=0 ; i<rows ; i++ )
/* the values of the row i */
ROW_VALUES( &(RES_ROWS(_res)[i]) ) =
((db_val_t*)(RES_ROWS(_res)+rows)) + RES_COL_N(_res)*i;
((db_val_t*)(void *)(RES_ROWS(_res)+rows)) + RES_COL_N(_res)*i;

return 0;
}
Expand Down Expand Up @@ -209,7 +209,7 @@ int db_realloc_rows(db_res_t *_res, const unsigned int old_rows,
for( i=0 ; i<rows ; i++ )
/* the values of the row i */
ROW_VALUES( &(RES_ROWS(_res)[i]) ) =
((db_val_t*)(RES_ROWS(_res)+rows)) + RES_COL_N(_res)*i;
((db_val_t*)(void *)(RES_ROWS(_res)+rows)) + RES_COL_N(_res)*i;

return 0;
}
2 changes: 1 addition & 1 deletion ip_addr.c
Expand Up @@ -212,7 +212,7 @@ int is_mcast(struct ip_addr* ip)
if (ip->af==AF_INET){
return IN_MULTICAST(htonl(ip->u.addr32[0]));
} else if (ip->af==AF_INET6){
return IN6_IS_ADDR_MULTICAST((struct in6_addr *)ip->u.addr);
return IN6_IS_ADDR_MULTICAST((struct in6_addr *)(void *)ip->u.addr);
} else {
LM_ERR("unsupported protocol family\n");
return -1;
Expand Down
10 changes: 7 additions & 3 deletions ip_addr.h
Expand Up @@ -153,7 +153,7 @@ struct socket_id {
struct ip_addr __ip; \
sockaddr2ip_addr( &__ip, (struct sockaddr*)_su ); \
_ip_char = ip_addr2a(&__ip); \
_port_no = su_getport( (union sockaddr_union*)_su); \
_port_no = su_getport( (union sockaddr_union*)(void *)_su); \
} while(0)


Expand Down Expand Up @@ -208,22 +208,26 @@ inline static int matchnet(struct ip_addr* ip, struct net* net)
/*! \brief inits an ip_addr pointer from a sockaddr structure*/
static inline void sockaddr2ip_addr(struct ip_addr* ip, struct sockaddr* sa)
{
void *copyfrom;

switch(sa->sa_family){
case AF_INET:
ip->af=AF_INET;
ip->len=4;
memcpy(ip->u.addr, &((struct sockaddr_in*)sa)->sin_addr, 4);
copyfrom = &((struct sockaddr_in*)(void *)sa)->sin_addr;
break;
case AF_INET6:
ip->af=AF_INET6;
ip->len=16;
memcpy(ip->u.addr, &((struct sockaddr_in6*)sa)->sin6_addr, 16);
copyfrom = &((struct sockaddr_in6*)(void *)sa)->sin6_addr;
break;
default:
LM_CRIT("unknown address family %d\n", sa->sa_family);
/* clear the structure to prevent uninitialized warnings */
ip->af=sa->sa_family;
return;
}
memcpy(ip->u.addr, copyfrom, ip->len);
}


Expand Down
2 changes: 1 addition & 1 deletion lib/container.h
Expand Up @@ -32,6 +32,6 @@
*/
#define container_of(ptr, type, member) ({ \
const typeof( ((type *)0)->member ) *__mptr = (ptr); \
(type *)( (char *)__mptr - offsetof(type,member) );})
(type *)(void *)( (char *)__mptr - offsetof(type,member) );})

#endif /* __OSS_CONTAINER_H */
8 changes: 4 additions & 4 deletions mem/f_malloc.c
Expand Up @@ -41,7 +41,7 @@
#define frag_is_free(_f) ((_f)->prev)

#define FRAG_NEXT(f) \
((struct fm_frag *)((char *)(f) + sizeof(struct fm_frag) + (f)->size))
((struct fm_frag *)(void *)((char *)(f) + sizeof(struct fm_frag) + (f)->size))

#define max(a,b) ( (a)>(b)?(a):(b))

Expand Down Expand Up @@ -208,7 +208,7 @@ struct fm_block *fm_malloc_init(char *address, unsigned long size, char *name)
return 0;
}
end=start+size;
fm=(struct fm_block *)start;
fm=(struct fm_block *)(void *)start;
memset(fm, 0, sizeof(struct fm_block));
fm->name = name;
fm->size=size;
Expand All @@ -220,8 +220,8 @@ struct fm_block *fm_malloc_init(char *address, unsigned long size, char *name)
fm->fragments = 0;
#endif

fm->first_frag=(struct fm_frag *)(start+ROUNDUP(sizeof(struct fm_block)));
fm->last_frag=(struct fm_frag *)(end-sizeof(struct fm_frag));
fm->first_frag=(struct fm_frag *)(void *)(start+ROUNDUP(sizeof(struct fm_block)));
fm->last_frag=(struct fm_frag *)(void *)end - 1;
/* init initial fragment*/
fm->first_frag->size=size-init_overhead;
fm->last_frag->size=0;
Expand Down
2 changes: 1 addition & 1 deletion mem/f_malloc.h
Expand Up @@ -55,7 +55,7 @@

/* get the fragment which corresponds to a pointer */
#define FM_FRAG(p) \
((struct fm_frag *)((char *)(p) - sizeof(struct fm_frag)))
((struct fm_frag *)(p) - 1)

/* hash structure:
* 0 .... F_MALLOC_OPTIMIZE/ROUNDTO - small buckets, size increases with
Expand Down
8 changes: 4 additions & 4 deletions mem/hp_malloc.c
Expand Up @@ -46,7 +46,7 @@
#define MIN_SHM_SPLIT_SIZE 4096
#define MIN_PKG_SPLIT_SIZE 256

#define FRAG_NEXT(f) ((struct hp_frag *)((char *)((f) + 1) + (f)->size))
#define FRAG_NEXT(f) ((struct hp_frag *)(void *)((char *)((f) + 1) + (f)->size))

#define FRAG_OVERHEAD HP_FRAG_OVERHEAD
#define frag_is_free(_f) ((_f)->prev)
Expand Down Expand Up @@ -552,7 +552,7 @@ static struct hp_block *hp_malloc_init(char *address, unsigned long size,
}

end = start + size;
hpb = (struct hp_block *)start;
hpb = (struct hp_block *)(void *)start;
memset(hpb, 0, sizeof(struct hp_block));
hpb->name = name;
hpb->size = size;
Expand All @@ -563,8 +563,8 @@ static struct hp_block *hp_malloc_init(char *address, unsigned long size,
hpb->total_fragments = 2;
gettimeofday(&hpb->last_updated, NULL);

hpb->first_frag = (struct hp_frag *)(start + ROUNDUP(sizeof(struct hp_block)));
hpb->last_frag = (struct hp_frag *)(end - sizeof *hpb->last_frag);
hpb->first_frag = (struct hp_frag *)(void *)(start + ROUNDUP(sizeof(struct hp_block)));
hpb->last_frag = (struct hp_frag *)(void *)end - 1;
hpb->last_frag->size = 0;

/* init initial fragment */
Expand Down
2 changes: 1 addition & 1 deletion mem/hp_malloc.h
Expand Up @@ -73,7 +73,7 @@ extern stat_var *rpm_frags;

/* get the fragment which corresponds to a pointer */
#define HP_FRAG(p) \
((struct hp_frag *)((char *)(p) - sizeof(struct hp_frag)))
((struct hp_frag *)(p) - 1)

#define UN_HASH(h) (((unsigned long)(h) <= (HP_MALLOC_OPTIMIZE/ROUNDTO)) ?\
(unsigned long)(h)*ROUNDTO: \
Expand Down
17 changes: 8 additions & 9 deletions mem/q_malloc.c
Expand Up @@ -35,20 +35,19 @@

/*useful macros*/
#define FRAG_END(f) \
((struct qm_frag_end*)((char*)(f)+sizeof(struct qm_frag)+ \
((struct qm_frag_end*)(void *)((char*)(f)+sizeof(struct qm_frag)+ \
(f)->size))

#define FRAG_NEXT(f) \
((struct qm_frag*)((char*)(f)+sizeof(struct qm_frag)+(f)->size+ \
((struct qm_frag*)(void *)((char*)(f)+sizeof(struct qm_frag)+(f)->size+ \
sizeof(struct qm_frag_end)))

#define FRAG_PREV(f) \
( (struct qm_frag*) ( ((char*)(f)-sizeof(struct qm_frag_end))- \
((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))->size- \
sizeof(struct qm_frag) ) )
({struct qm_frag_end *ep = (struct qm_frag_end*)(f) - 1; \
(struct qm_frag*)(void *)((char*)ep - ep->size) - 1;})

#define PREV_FRAG_END(f) \
((struct qm_frag_end*)((char*)(f)-sizeof(struct qm_frag_end)))
((struct qm_frag_end*)(f)-1)

#define MIN_FRAG_SIZE QM_ROUNDTO
#define FRAG_OVERHEAD (sizeof(struct qm_frag)+sizeof(struct qm_frag_end))
Expand Down Expand Up @@ -205,7 +204,7 @@ struct qm_block *qm_malloc_init(char *address, unsigned long size, char *name)
return 0;
}
end=start+size;
qm=(struct qm_block*)start;
qm=(struct qm_block*)(void *)start;
memset(qm, 0, sizeof(struct qm_block));
qm->name=name;
qm->size=size;
Expand All @@ -216,8 +215,8 @@ struct qm_block *qm_malloc_init(char *address, unsigned long size, char *name)
qm->max_real_used = 0;
size-=init_overhead;

qm->first_frag=(struct qm_frag*)(start+ROUNDUP(sizeof(struct qm_block)));
qm->last_frag_end=(struct qm_frag_end*)(end-sizeof(struct qm_frag_end));
qm->first_frag=(struct qm_frag*)(void *)(start+ROUNDUP(sizeof(struct qm_block)));
qm->last_frag_end=(struct qm_frag_end*)(void *)end-1;
/* init initial fragment*/
qm->first_frag->size=size;
qm->last_frag_end->size=size;
Expand Down
2 changes: 1 addition & 1 deletion mem/q_malloc.h
Expand Up @@ -58,7 +58,7 @@
(sizeof(long)*8 - Q_MALLOC_OPTIMIZE_FACTOR) + 1))

#define QM_FRAG(p) \
((struct qm_frag *)((char *)(p) - sizeof(struct qm_frag)))
((struct qm_frag *)(p) - 1)

/* hash structure:
* 0 .... Q_MALLOC_OPTIMIZE/QM_ROUNDTO - small buckets, size increases with
Expand Down
2 changes: 1 addition & 1 deletion mem/q_malloc_dyn.h
Expand Up @@ -51,7 +51,7 @@ int qm_split_frag(struct qm_block *qm, struct qm_frag *f,
/*split the fragment*/
end=FRAG_END(f);
end->size=new_size;
n=(struct qm_frag*)((char*)end+sizeof(struct qm_frag_end));
n=(struct qm_frag*)(end+1);
n->size=rest-FRAG_OVERHEAD;
FRAG_END(n)->size=n->size;
FRAG_CLEAR_USED(n); /* never used */
Expand Down

0 comments on commit fff57ac

Please sign in to comment.