aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorAlex Wang <alexw@nicira.com>2013-07-22 15:47:19 -0700
committerBen Pfaff <blp@nicira.com>2013-07-23 12:34:41 -0700
commitdb5a101931c5393d5f9df600559882418d536878 (patch)
tree3e2820c844a3f20209736adad31ddfaa1b0f5e13 /lib
parent550f0db4e0fb3bf9952e8a88b71242491b9dde2a (diff)
clang: Fix the alignment warning.
This commit fixes the warning issued by 'clang' when pointer is casted to one with greater alignment. Signed-off-by: Alex Wang <alexw@nicira.com> Signed-off-by: Ben Pfaff <blp@nicira.com>
Diffstat (limited to 'lib')
-rw-r--r--lib/hash.c6
-rw-r--r--lib/jhash.c10
-rw-r--r--lib/mac-learning.c4
-rw-r--r--lib/netdev-linux.c3
-rw-r--r--lib/netlink.c6
-rw-r--r--lib/nx-match.c11
-rw-r--r--lib/nx-match.h2
-rw-r--r--lib/odp-util.c3
-rw-r--r--lib/ofp-actions.c27
-rw-r--r--lib/ofp-actions.h2
-rw-r--r--lib/ofp-util.c7
-rw-r--r--lib/ovs-atomic-gcc4+.h4
-rw-r--r--lib/packets.c8
-rw-r--r--lib/route-table.c2
-rw-r--r--lib/rtnetlink-link.c3
-rw-r--r--lib/socket-util.c5
-rw-r--r--lib/stream-tcp.c3
-rw-r--r--lib/util.h4
18 files changed, 60 insertions, 50 deletions
diff --git a/lib/hash.c b/lib/hash.c
index e954d786..8f34493b 100644
--- a/lib/hash.c
+++ b/lib/hash.c
@@ -29,15 +29,15 @@ hash_3words(uint32_t a, uint32_t b, uint32_t c)
uint32_t
hash_bytes(const void *p_, size_t n, uint32_t basis)
{
- const uint8_t *p = p_;
+ const uint32_t *p = p_;
size_t orig_n = n;
uint32_t hash;
hash = basis;
while (n >= 4) {
- hash = mhash_add(hash, get_unaligned_u32((const uint32_t *) p));
+ hash = mhash_add(hash, get_unaligned_u32(p));
n -= 4;
- p += 4;
+ p += 1;
}
if (n) {
diff --git a/lib/jhash.c b/lib/jhash.c
index 4ec2871d..c08c3689 100644
--- a/lib/jhash.c
+++ b/lib/jhash.c
@@ -96,18 +96,18 @@ jhash_words(const uint32_t *p, size_t n, uint32_t basis)
uint32_t
jhash_bytes(const void *p_, size_t n, uint32_t basis)
{
- const uint8_t *p = p_;
+ const uint32_t *p = p_;
uint32_t a, b, c;
a = b = c = 0xdeadbeef + n + basis;
while (n >= 12) {
- a += get_unaligned_u32((uint32_t *) p);
- b += get_unaligned_u32((uint32_t *) (p + 4));
- c += get_unaligned_u32((uint32_t *) (p + 8));
+ a += get_unaligned_u32(p);
+ b += get_unaligned_u32(p + 1);
+ c += get_unaligned_u32(p + 2);
jhash_mix(&a, &b, &c);
n -= 12;
- p += 12;
+ p += 3;
}
if (n) {
diff --git a/lib/mac-learning.c b/lib/mac-learning.c
index ca0ccb8e..e2ca02b4 100644
--- a/lib/mac-learning.c
+++ b/lib/mac-learning.c
@@ -49,8 +49,8 @@ static uint32_t
mac_table_hash(const struct mac_learning *ml, const uint8_t mac[ETH_ADDR_LEN],
uint16_t vlan)
{
- unsigned int mac1 = get_unaligned_u32((uint32_t *) mac);
- unsigned int mac2 = get_unaligned_u16((uint16_t *) (mac + 4));
+ unsigned int mac1 = get_unaligned_u32(ALIGNED_CAST(uint32_t *, mac));
+ unsigned int mac2 = get_unaligned_u16(ALIGNED_CAST(uint16_t *, mac + 4));
return hash_3words(mac1, mac2 | (vlan << 16), ml->secret);
}
diff --git a/lib/netdev-linux.c b/lib/netdev-linux.c
index e1ad4c86..ab4feaeb 100644
--- a/lib/netdev-linux.c
+++ b/lib/netdev-linux.c
@@ -4550,7 +4550,8 @@ netdev_linux_get_ipv4(const struct netdev *netdev, struct in_addr *ip,
ifr.ifr_addr.sa_family = AF_INET;
error = netdev_linux_do_ioctl(netdev_get_name(netdev), &ifr, cmd, cmd_name);
if (!error) {
- const struct sockaddr_in *sin = (struct sockaddr_in *) &ifr.ifr_addr;
+ const struct sockaddr_in *sin = ALIGNED_CAST(struct sockaddr_in *,
+ &ifr.ifr_addr);
*ip = sin->sin_addr;
}
return error;
diff --git a/lib/netlink.c b/lib/netlink.c
index a6867847..50444abd 100644
--- a/lib/netlink.c
+++ b/lib/netlink.c
@@ -711,8 +711,7 @@ nl_policy_parse(const struct ofpbuf *msg, size_t nla_offset,
return false;
}
- NL_ATTR_FOR_EACH (nla, left,
- (struct nlattr *) ((char *) msg->data + nla_offset),
+ NL_ATTR_FOR_EACH (nla, left, ofpbuf_at(msg, nla_offset, 0),
msg->size - nla_offset)
{
uint16_t type = nl_attr_type(nla);
@@ -777,8 +776,7 @@ nl_attr_find__(const struct nlattr *attrs, size_t size, uint16_t type)
const struct nlattr *
nl_attr_find(const struct ofpbuf *buf, size_t hdr_len, uint16_t type)
{
- const uint8_t *start = (const uint8_t *) buf->data + hdr_len;
- return nl_attr_find__((const struct nlattr *) start, buf->size - hdr_len,
+ return nl_attr_find__(ofpbuf_at(buf, hdr_len, 0), buf->size - hdr_len,
type);
}
diff --git a/lib/nx-match.c b/lib/nx-match.c
index 3a6d7cc2..bdb3a2ba 100644
--- a/lib/nx-match.c
+++ b/lib/nx-match.c
@@ -744,7 +744,7 @@ oxm_put_match(struct ofpbuf *b, const struct match *match)
match_len = nx_put_raw(b, true, match, cookie, cookie_mask) + sizeof *omh;
ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
- omh = (struct ofp11_match_header *)((char *)b->data + start_len);
+ omh = ofpbuf_at(b, start_len, sizeof *omh);
omh->type = htons(OFPMT_OXM);
omh->length = htons(match_len);
@@ -807,9 +807,9 @@ nx_match_to_string(const uint8_t *p, unsigned int match_len)
}
char *
-oxm_match_to_string(const uint8_t *p, unsigned int match_len)
+oxm_match_to_string(const struct ofpbuf *p, unsigned int match_len)
{
- const struct ofp11_match_header *omh = (struct ofp11_match_header *)p;
+ const struct ofp11_match_header *omh = p->data;
uint16_t match_len_;
struct ds s;
@@ -837,7 +837,8 @@ oxm_match_to_string(const uint8_t *p, unsigned int match_len)
goto err;
}
- return nx_match_to_string(p + sizeof *omh, match_len - sizeof *omh);
+ return nx_match_to_string(ofpbuf_at(p, sizeof *omh, 0),
+ match_len - sizeof *omh);
err:
return ds_steal_cstr(&s);
@@ -997,7 +998,7 @@ oxm_match_from_string(const char *s, struct ofpbuf *b)
match_len = nx_match_from_string_raw(s, b) + sizeof *omh;
ofpbuf_put_zeros(b, ROUND_UP(match_len, 8) - match_len);
- omh = (struct ofp11_match_header *)((char *)b->data + start_len);
+ omh = ofpbuf_at(b, start_len, sizeof *omh);
omh->type = htons(OFPMT_OXM);
omh->length = htons(match_len);
diff --git a/lib/nx-match.h b/lib/nx-match.h
index b03688b2..a6b7c523 100644
--- a/lib/nx-match.h
+++ b/lib/nx-match.h
@@ -54,7 +54,7 @@ int nx_put_match(struct ofpbuf *, const struct match *,
int oxm_put_match(struct ofpbuf *, const struct match *);
char *nx_match_to_string(const uint8_t *, unsigned int match_len);
-char *oxm_match_to_string(const uint8_t *, unsigned int match_len);
+char *oxm_match_to_string(const struct ofpbuf *, unsigned int match_len);
int nx_match_from_string(const char *, struct ofpbuf *);
int oxm_match_from_string(const char *, struct ofpbuf *);
diff --git a/lib/odp-util.c b/lib/odp-util.c
index 5a322217..3c3063df 100644
--- a/lib/odp-util.c
+++ b/lib/odp-util.c
@@ -2512,7 +2512,8 @@ uint32_t
odp_flow_key_hash(const struct nlattr *key, size_t key_len)
{
BUILD_ASSERT_DECL(!(NLA_ALIGNTO % sizeof(uint32_t)));
- return hash_words((const uint32_t *) key, key_len / sizeof(uint32_t), 0);
+ return hash_words(ALIGNED_CAST(const uint32_t *, key),
+ key_len / sizeof(uint32_t), 0);
}
static void
diff --git a/lib/ofp-actions.c b/lib/ofp-actions.c
index 899928a7..61e28542 100644
--- a/lib/ofp-actions.c
+++ b/lib/ofp-actions.c
@@ -336,7 +336,7 @@ ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
break;
case OFPUTIL_NXAST_WRITE_METADATA:
- nawm = (const struct nx_action_write_metadata *) a;
+ nawm = ALIGNED_CAST(const struct nx_action_write_metadata *, a);
error = metadata_from_nxast(nawm, out);
break;
@@ -356,7 +356,7 @@ ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
case OFPUTIL_NXAST_REG_LOAD:
error = nxm_reg_load_from_openflow(
- (const struct nx_action_reg_load *) a, out);
+ ALIGNED_CAST(const struct nx_action_reg_load *, a), out);
break;
case OFPUTIL_NXAST_STACK_PUSH:
@@ -375,7 +375,7 @@ ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
break;
case OFPUTIL_NXAST_SET_TUNNEL64:
- nast64 = (const struct nx_action_set_tunnel64 *) a;
+ nast64 = ALIGNED_CAST(const struct nx_action_set_tunnel64 *, a);
tunnel = ofpact_put_SET_TUNNEL(out);
tunnel->ofpact.compat = code;
tunnel->tun_id = ntohll(nast64->tun_id);
@@ -402,7 +402,8 @@ ofpact_from_nxast(const union ofp_action *a, enum ofputil_action_code code,
break;
case OFPUTIL_NXAST_LEARN:
- error = learn_from_openflow((const struct nx_action_learn *) a, out);
+ error = learn_from_openflow(
+ ALIGNED_CAST(const struct nx_action_learn *, a), out);
break;
case OFPUTIL_NXAST_EXIT:
@@ -881,7 +882,7 @@ ofpacts_from_openflow11(const union ofp_action *in, size_t n_in,
instruction_get_##ENUM(const struct ofp11_instruction *inst)\
{ \
ovs_assert(inst->type == htons(ENUM)); \
- return (struct STRUCT *)inst; \
+ return ALIGNED_CAST(struct STRUCT *, inst); \
} \
\
static inline void \
@@ -1070,10 +1071,10 @@ decode_openflow11_instructions(const struct ofp11_instruction insts[],
static void
get_actions_from_instruction(const struct ofp11_instruction *inst,
- const union ofp_action **actions,
- size_t *n_actions)
+ const union ofp_action **actions,
+ size_t *n_actions)
{
- *actions = (const union ofp_action *) (inst + 1);
+ *actions = ALIGNED_CAST(const union ofp_action *, inst + 1);
*n_actions = (ntohs(inst->len) - sizeof *inst) / OFP11_INSTRUCTION_ALIGN;
}
@@ -1140,8 +1141,8 @@ ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
const struct ofp13_instruction_meter *oim;
struct ofpact_meter *om;
- oim = (const struct ofp13_instruction_meter *)
- insts[OVSINST_OFPIT13_METER];
+ oim = ALIGNED_CAST(const struct ofp13_instruction_meter *,
+ insts[OVSINST_OFPIT13_METER]);
om = ofpact_put_METER(ofpacts);
om->meter_id = ntohl(oim->meter_id);
@@ -1167,8 +1168,8 @@ ofpacts_pull_openflow11_instructions(struct ofpbuf *openflow,
const struct ofp11_instruction_write_metadata *oiwm;
struct ofpact_metadata *om;
- oiwm = (const struct ofp11_instruction_write_metadata *)
- insts[OVSINST_OFPIT11_WRITE_METADATA];
+ oiwm = ALIGNED_CAST(const struct ofp11_instruction_write_metadata *,
+ insts[OVSINST_OFPIT11_WRITE_METADATA]);
om = ofpact_put_WRITE_METADATA(ofpacts);
om->metadata = oiwm->metadata;
@@ -1436,7 +1437,7 @@ ofpact_note_to_nxast(const struct ofpact_note *note, struct ofpbuf *out)
if (remainder) {
ofpbuf_put_zeros(out, OFP_ACTION_ALIGN - remainder);
}
- nan = (struct nx_action_note *)((char *)out->data + start_ofs);
+ nan = ofpbuf_at(out, start_ofs, sizeof *nan);
nan->len = htons(out->size - start_ofs);
}
diff --git a/lib/ofp-actions.h b/lib/ofp-actions.h
index b97afd0b..ca33ca89 100644
--- a/lib/ofp-actions.h
+++ b/lib/ofp-actions.h
@@ -582,7 +582,7 @@ void *ofpact_put(struct ofpbuf *, enum ofpact_type, size_t len);
ofpact_get_##ENUM(const struct ofpact *ofpact) \
{ \
ovs_assert(ofpact->type == OFPACT_##ENUM); \
- return (struct STRUCT *) ofpact; \
+ return ALIGNED_CAST(struct STRUCT *, ofpact); \
} \
\
static inline struct STRUCT * \
diff --git a/lib/ofp-util.c b/lib/ofp-util.c
index bc85797a..7725253b 100644
--- a/lib/ofp-util.c
+++ b/lib/ofp-util.c
@@ -1293,7 +1293,7 @@ ofputil_decode_hello_bitmap(const struct ofp_hello_elem_header *oheh,
uint32_t *allowed_versionsp)
{
uint16_t bitmap_len = ntohs(oheh->length) - sizeof *oheh;
- const ovs_be32 *bitmap = (const ovs_be32 *) (oheh + 1);
+ const ovs_be32 *bitmap = ALIGNED_CAST(const ovs_be32 *, oheh + 1);
uint32_t allowed_versions;
if (!bitmap_len || bitmap_len % sizeof *bitmap) {
@@ -1397,7 +1397,7 @@ ofputil_encode_hello(uint32_t allowed_versions)
oheh = ofpbuf_put_zeros(msg, ROUND_UP(map_len + sizeof *oheh, 8));
oheh->type = htons(OFPHET_VERSIONBITMAP);
oheh->length = htons(map_len + sizeof *oheh);
- *(ovs_be32 *)(oheh + 1) = htonl(allowed_versions);
+ *ALIGNED_CAST(ovs_be32 *, oheh + 1) = htonl(allowed_versions);
ofpmsg_update_length(msg);
}
@@ -1750,7 +1750,8 @@ ofputil_pull_bands(struct ofpbuf *msg, size_t len, uint16_t *n_bands,
((struct ofp13_meter_band_dscp_remark *)ombh)->prec_level : 0;
n++;
len -= ombh_len;
- ombh = (struct ofp13_meter_band_header *)(((char *)ombh) + ombh_len);
+ ombh = ALIGNED_CAST(struct ofp13_meter_band_header *,
+ (char *) ombh + ombh_len);
}
if (len) {
return OFPERR_OFPBRC_BAD_LEN;
diff --git a/lib/ovs-atomic-gcc4+.h b/lib/ovs-atomic-gcc4+.h
index b8649ede..4476162b 100644
--- a/lib/ovs-atomic-gcc4+.h
+++ b/lib/ovs-atomic-gcc4+.h
@@ -117,7 +117,7 @@ typedef enum {
__builtin_choose_expr( \
__builtin_types_compatible_p(typeof(OBJECT), struct locked_uint64), \
(THEN), (ELSE))
-#define AS_LOCKED_UINT64(OBJECT) ((struct locked_uint64 *) (OBJECT))
+#define AS_LOCKED_UINT64(OBJECT) ((struct locked_uint64 *) (void *) (OBJECT))
#define AS_UINT64(OBJECT) ((uint64_t *) (OBJECT))
struct locked_uint64 {
uint64_t value;
@@ -135,7 +135,7 @@ uint64_t locked_uint64_and(struct locked_uint64 *, uint64_t arg);
__builtin_choose_expr( \
__builtin_types_compatible_p(typeof(OBJECT), struct locked_int64), \
(THEN), (ELSE))
-#define AS_LOCKED_INT64(OBJECT) ((struct locked_int64 *) (OBJECT))
+#define AS_LOCKED_INT64(OBJECT) ((struct locked_int64 *) (void *) (OBJECT))
#define AS_INT64(OBJECT) ((int64_t *) (OBJECT))
struct locked_int64 {
int64_t value;
diff --git a/lib/packets.c b/lib/packets.c
index 9c30b95c..b95e1e01 100644
--- a/lib/packets.c
+++ b/lib/packets.c
@@ -250,7 +250,8 @@ set_ethertype(struct ofpbuf *packet, ovs_be16 eth_type)
if (eh->eth_type == htons(ETH_TYPE_VLAN)) {
ovs_be16 *p;
- p = (ovs_be16 *)((char *)(packet->l2_5 ? packet->l2_5 : packet->l3) - 2);
+ p = ALIGNED_CAST(ovs_be16 *,
+ (char *)(packet->l2_5 ? packet->l2_5 : packet->l3) - 2);
*p = eth_type;
} else {
eh->eth_type = eth_type;
@@ -670,7 +671,7 @@ packet_rh_present(struct ofpbuf *packet)
if (remaining < sizeof *nh) {
return false;
}
- nh = (struct ip6_hdr *)data;
+ nh = ALIGNED_CAST(struct ip6_hdr *, data);
data += sizeof *nh;
remaining -= sizeof *nh;
nexthdr = nh->ip6_nxt;
@@ -706,7 +707,8 @@ packet_rh_present(struct ofpbuf *packet)
nexthdr = ext_hdr->ip6e_nxt;
len = (ext_hdr->ip6e_len + 2) * 4;
} else if (nexthdr == IPPROTO_FRAGMENT) {
- const struct ip6_frag *frag_hdr = (struct ip6_frag *)data;
+ const struct ip6_frag *frag_hdr = ALIGNED_CAST(struct ip6_frag *,
+ data);
nexthdr = frag_hdr->ip6f_nxt;
len = sizeof *frag_hdr;
diff --git a/lib/route-table.c b/lib/route-table.c
index d572e8ce..1afc01d0 100644
--- a/lib/route-table.c
+++ b/lib/route-table.c
@@ -270,7 +270,7 @@ route_table_parse(struct ofpbuf *buf, struct route_table_msg *change)
const struct nlmsghdr *nlmsg;
nlmsg = buf->data;
- rtm = (const struct rtmsg *) ((const char *) buf->data + NLMSG_HDRLEN);
+ rtm = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *rtm);
if (rtm->rtm_family != AF_INET) {
VLOG_DBG_RL(&rl, "received non AF_INET rtnetlink route message");
diff --git a/lib/rtnetlink-link.c b/lib/rtnetlink-link.c
index 459e4855..308338fc 100644
--- a/lib/rtnetlink-link.c
+++ b/lib/rtnetlink-link.c
@@ -59,8 +59,7 @@ rtnetlink_link_parse(struct ofpbuf *buf,
const struct ifinfomsg *ifinfo;
nlmsg = buf->data;
- ifinfo = ((const struct ifinfomsg *)
- ((const char *) buf->data + NLMSG_HDRLEN));
+ ifinfo = ofpbuf_at(buf, NLMSG_HDRLEN, sizeof *ifinfo);
change->nlmsg_type = nlmsg->nlmsg_type;
change->ifi_index = ifinfo->ifi_index;
diff --git a/lib/socket-util.c b/lib/socket-util.c
index 2ba0fd4a..1d0cede8 100644
--- a/lib/socket-util.c
+++ b/lib/socket-util.c
@@ -201,7 +201,8 @@ lookup_hostname(const char *host_name, struct in_addr *addr)
switch (getaddrinfo(host_name, NULL, &hints, &result)) {
case 0:
- *addr = ((struct sockaddr_in *) result->ai_addr)->sin_addr;
+ *addr = ALIGNED_CAST(struct sockaddr_in *,
+ result->ai_addr)->sin_addr;
freeaddrinfo(result);
return 0;
@@ -1326,7 +1327,7 @@ recv_data_and_fds(int sock,
goto error;
} else {
size_t n_fds = (p->cmsg_len - CMSG_LEN(0)) / sizeof *fds;
- const int *fds_data = (const int *) CMSG_DATA(p);
+ const int *fds_data = ALIGNED_CAST(const int *, CMSG_DATA(p));
ovs_assert(n_fds > 0);
if (n_fds > SOUTIL_MAX_FDS) {
diff --git a/lib/stream-tcp.c b/lib/stream-tcp.c
index d507208e..a4cdf45a 100644
--- a/lib/stream-tcp.c
+++ b/lib/stream-tcp.c
@@ -130,7 +130,8 @@ static int
ptcp_accept(int fd, const struct sockaddr *sa, size_t sa_len,
struct stream **streamp)
{
- const struct sockaddr_in *sin = (const struct sockaddr_in *) sa;
+ const struct sockaddr_in *sin = ALIGNED_CAST(const struct sockaddr_in *,
+ sa);
char name[128];
if (sa_len == sizeof(struct sockaddr_in) && sin->sin_family == AF_INET) {
diff --git a/lib/util.h b/lib/util.h
index 911ad321..0db41be9 100644
--- a/lib/util.h
+++ b/lib/util.h
@@ -193,6 +193,10 @@ is_pow2(uintmax_t x)
#define ASSIGN_CONTAINER(OBJECT, POINTER, MEMBER) \
((OBJECT) = OBJECT_CONTAINING(POINTER, OBJECT, MEMBER), (void) 0)
+/* Given ATTR, and TYPE, cast the ATTR to TYPE by first casting ATTR to
+ * (void *). This is to suppress the alignment warning issued by clang. */
+#define ALIGNED_CAST(TYPE, ATTR) ((TYPE) (void *) (ATTR))
+
#ifdef __cplusplus
extern "C" {
#endif