Skip to content

Commit

Permalink
batman-adv: Calculate sizeof using variable insead of types
Browse files Browse the repository at this point in the history
Documentation/CodingStyle recommends to use the form

	p = kmalloc(sizeof(*p), ...);

to calculate the size of a struct and not the version where the struct
name is spelled out to prevent bugs when the type of p changes. This
also seems appropriate for manipulation of buffers when they are
directly associated with p.

Signed-off-by: Sven Eckelmann <sven@narfation.org>
  • Loading branch information
Sven Eckelmann committed May 30, 2011
1 parent 958ca59 commit 704509b
Show file tree
Hide file tree
Showing 14 changed files with 65 additions and 72 deletions.
2 changes: 1 addition & 1 deletion net/batman-adv/aggregation.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ static void new_aggregated_packet(const unsigned char *packet_buff,
}
}

forw_packet_aggr = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
forw_packet_aggr = kmalloc(sizeof(*forw_packet_aggr), GFP_ATOMIC);
if (!forw_packet_aggr) {
if (!own_packet)
atomic_inc(&bat_priv->batman_queue_left);
Expand Down
2 changes: 1 addition & 1 deletion net/batman-adv/bat_debugfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ static int debug_log_setup(struct bat_priv *bat_priv)
if (!bat_priv->debug_dir)
goto err;

bat_priv->debug_log = kzalloc(sizeof(struct debug_log), GFP_ATOMIC);
bat_priv->debug_log = kzalloc(sizeof(*bat_priv->debug_log), GFP_ATOMIC);
if (!bat_priv->debug_log)
goto err;

Expand Down
12 changes: 6 additions & 6 deletions net/batman-adv/gateway_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ static void gw_node_add(struct bat_priv *bat_priv,
struct gw_node *gw_node;
int down, up;

gw_node = kzalloc(sizeof(struct gw_node), GFP_ATOMIC);
gw_node = kzalloc(sizeof(*gw_node), GFP_ATOMIC);
if (!gw_node)
return;

Expand Down Expand Up @@ -508,7 +508,7 @@ int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
/* check for ip header */
switch (ntohs(ethhdr->h_proto)) {
case ETH_P_IP:
if (!pskb_may_pull(skb, header_len + sizeof(struct iphdr)))
if (!pskb_may_pull(skb, header_len + sizeof(*iphdr)))
return 0;
iphdr = (struct iphdr *)(skb->data + header_len);
header_len += iphdr->ihl * 4;
Expand All @@ -519,10 +519,10 @@ int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)

break;
case ETH_P_IPV6:
if (!pskb_may_pull(skb, header_len + sizeof(struct ipv6hdr)))
if (!pskb_may_pull(skb, header_len + sizeof(*ipv6hdr)))
return 0;
ipv6hdr = (struct ipv6hdr *)(skb->data + header_len);
header_len += sizeof(struct ipv6hdr);
header_len += sizeof(*ipv6hdr);

/* check for udp header */
if (ipv6hdr->nexthdr != IPPROTO_UDP)
Expand All @@ -533,10 +533,10 @@ int gw_is_target(struct bat_priv *bat_priv, struct sk_buff *skb)
return 0;
}

if (!pskb_may_pull(skb, header_len + sizeof(struct udphdr)))
if (!pskb_may_pull(skb, header_len + sizeof(*udphdr)))
return 0;
udphdr = (struct udphdr *)(skb->data + header_len);
header_len += sizeof(struct udphdr);
header_len += sizeof(*udphdr);

/* check for bootp port */
if ((ntohs(ethhdr->h_proto) == ETH_P_IP) &&
Expand Down
2 changes: 1 addition & 1 deletion net/batman-adv/hard-interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,7 +459,7 @@ static struct hard_iface *hardif_add_interface(struct net_device *net_dev)

dev_hold(net_dev);

hard_iface = kmalloc(sizeof(struct hard_iface), GFP_ATOMIC);
hard_iface = kmalloc(sizeof(*hard_iface), GFP_ATOMIC);
if (!hard_iface) {
pr_err("Can't add interface (%s): out of memory\n",
net_dev->name);
Expand Down
7 changes: 4 additions & 3 deletions net/batman-adv/hash.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,16 @@ struct hashtable_t *hash_new(int size)
{
struct hashtable_t *hash;

hash = kmalloc(sizeof(struct hashtable_t), GFP_ATOMIC);
hash = kmalloc(sizeof(*hash), GFP_ATOMIC);
if (!hash)
return NULL;

hash->table = kmalloc(sizeof(struct element_t *) * size, GFP_ATOMIC);
hash->table = kmalloc(sizeof(*hash->table) * size, GFP_ATOMIC);
if (!hash->table)
goto free_hash;

hash->list_locks = kmalloc(sizeof(spinlock_t) * size, GFP_ATOMIC);
hash->list_locks = kmalloc(sizeof(*hash->list_locks) * size,
GFP_ATOMIC);
if (!hash->list_locks)
goto free_table;

Expand Down
4 changes: 2 additions & 2 deletions net/batman-adv/icmp_socket.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static int bat_socket_open(struct inode *inode, struct file *file)

nonseekable_open(inode, file);

socket_client = kmalloc(sizeof(struct socket_client), GFP_KERNEL);
socket_client = kmalloc(sizeof(*socket_client), GFP_KERNEL);

if (!socket_client)
return -ENOMEM;
Expand Down Expand Up @@ -310,7 +310,7 @@ static void bat_socket_add_packet(struct socket_client *socket_client,
{
struct socket_packet *socket_packet;

socket_packet = kmalloc(sizeof(struct socket_packet), GFP_ATOMIC);
socket_packet = kmalloc(sizeof(*socket_packet), GFP_ATOMIC);

if (!socket_packet)
return;
Expand Down
4 changes: 2 additions & 2 deletions net/batman-adv/originator.c
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ struct neigh_node *create_neighbor(struct orig_node *orig_node,
bat_dbg(DBG_BATMAN, bat_priv,
"Creating new last-hop neighbor of originator\n");

neigh_node = kzalloc(sizeof(struct neigh_node), GFP_ATOMIC);
neigh_node = kzalloc(sizeof(*neigh_node), GFP_ATOMIC);
if (!neigh_node)
return NULL;

Expand Down Expand Up @@ -196,7 +196,7 @@ struct orig_node *get_orig_node(struct bat_priv *bat_priv, const uint8_t *addr)
bat_dbg(DBG_BATMAN, bat_priv,
"Creating new originator: %pM\n", addr);

orig_node = kzalloc(sizeof(struct orig_node), GFP_ATOMIC);
orig_node = kzalloc(sizeof(*orig_node), GFP_ATOMIC);
if (!orig_node)
return NULL;

Expand Down
8 changes: 4 additions & 4 deletions net/batman-adv/routing.c
Original file line number Diff line number Diff line change
Expand Up @@ -1357,7 +1357,7 @@ int route_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
int recv_unicast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
{
struct unicast_packet *unicast_packet;
int hdr_size = sizeof(struct unicast_packet);
int hdr_size = sizeof(*unicast_packet);

if (check_unicast_packet(skb, hdr_size) < 0)
return NET_RX_DROP;
Expand All @@ -1377,7 +1377,7 @@ int recv_ucast_frag_packet(struct sk_buff *skb, struct hard_iface *recv_if)
{
struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
struct unicast_frag_packet *unicast_packet;
int hdr_size = sizeof(struct unicast_frag_packet);
int hdr_size = sizeof(*unicast_packet);
struct sk_buff *new_skb = NULL;
int ret;

Expand Down Expand Up @@ -1413,7 +1413,7 @@ int recv_bcast_packet(struct sk_buff *skb, struct hard_iface *recv_if)
struct orig_node *orig_node = NULL;
struct bcast_packet *bcast_packet;
struct ethhdr *ethhdr;
int hdr_size = sizeof(struct bcast_packet);
int hdr_size = sizeof(*bcast_packet);
int ret = NET_RX_DROP;
int32_t seq_diff;

Expand Down Expand Up @@ -1491,7 +1491,7 @@ int recv_vis_packet(struct sk_buff *skb, struct hard_iface *recv_if)
struct vis_packet *vis_packet;
struct ethhdr *ethhdr;
struct bat_priv *bat_priv = netdev_priv(recv_if->soft_iface);
int hdr_size = sizeof(struct vis_packet);
int hdr_size = sizeof(*vis_packet);

/* keep skb linear */
if (skb_linearize(skb) < 0)
Expand Down
17 changes: 8 additions & 9 deletions net/batman-adv/send.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ int send_skb_packet(struct sk_buff *skb, struct hard_iface *hard_iface,
}

/* push to the ethernet header. */
if (my_skb_head_push(skb, sizeof(struct ethhdr)) < 0)
if (my_skb_head_push(skb, sizeof(*ethhdr)) < 0)
goto send_skb_err;

skb_reset_mac_header(skb);
Expand Down Expand Up @@ -144,7 +144,7 @@ static void send_packet_to_if(struct forw_packet *forw_packet,
hard_iface->net_dev->name,
hard_iface->net_dev->dev_addr);

buff_pos += sizeof(struct batman_packet) +
buff_pos += sizeof(*batman_packet) +
(batman_packet->num_tt * ETH_ALEN);
packet_num++;
batman_packet = (struct batman_packet *)
Expand Down Expand Up @@ -220,19 +220,18 @@ static void rebuild_batman_packet(struct bat_priv *bat_priv,
unsigned char *new_buff;
struct batman_packet *batman_packet;

new_len = sizeof(struct batman_packet) +
(bat_priv->num_local_tt * ETH_ALEN);
new_len = sizeof(*batman_packet) + (bat_priv->num_local_tt * ETH_ALEN);
new_buff = kmalloc(new_len, GFP_ATOMIC);

/* keep old buffer if kmalloc should fail */
if (new_buff) {
memcpy(new_buff, hard_iface->packet_buff,
sizeof(struct batman_packet));
sizeof(*batman_packet));
batman_packet = (struct batman_packet *)new_buff;

batman_packet->num_tt = tt_local_fill_buffer(bat_priv,
new_buff + sizeof(struct batman_packet),
new_len - sizeof(struct batman_packet));
new_buff + sizeof(*batman_packet),
new_len - sizeof(*batman_packet));

kfree(hard_iface->packet_buff);
hard_iface->packet_buff = new_buff;
Expand Down Expand Up @@ -368,7 +367,7 @@ void schedule_forward_packet(struct orig_node *orig_node,
send_time = forward_send_time();
add_bat_packet_to_list(bat_priv,
(unsigned char *)batman_packet,
sizeof(struct batman_packet) + tt_buff_len,
sizeof(*batman_packet) + tt_buff_len,
if_incoming, 0, send_time);
}

Expand Down Expand Up @@ -424,7 +423,7 @@ int add_bcast_packet_to_list(struct bat_priv *bat_priv,
if (!primary_if)
goto out_and_inc;

forw_packet = kmalloc(sizeof(struct forw_packet), GFP_ATOMIC);
forw_packet = kmalloc(sizeof(*forw_packet), GFP_ATOMIC);

if (!forw_packet)
goto out_and_inc;
Expand Down
12 changes: 5 additions & 7 deletions net/batman-adv/soft-interface.c
Original file line number Diff line number Diff line change
Expand Up @@ -123,8 +123,7 @@ static struct softif_neigh_vid *softif_neigh_vid_get(struct bat_priv *bat_priv,
goto out;
}

softif_neigh_vid = kzalloc(sizeof(struct softif_neigh_vid),
GFP_ATOMIC);
softif_neigh_vid = kzalloc(sizeof(*softif_neigh_vid), GFP_ATOMIC);
if (!softif_neigh_vid)
goto out;

Expand Down Expand Up @@ -170,7 +169,7 @@ static struct softif_neigh *softif_neigh_get(struct bat_priv *bat_priv,
goto unlock;
}

softif_neigh = kzalloc(sizeof(struct softif_neigh), GFP_ATOMIC);
softif_neigh = kzalloc(sizeof(*softif_neigh), GFP_ATOMIC);
if (!softif_neigh)
goto unlock;

Expand Down Expand Up @@ -611,7 +610,7 @@ int interface_tx(struct sk_buff *skb, struct net_device *soft_iface)
if (!primary_if)
goto dropped;

if (my_skb_head_push(skb, sizeof(struct bcast_packet)) < 0)
if (my_skb_head_push(skb, sizeof(*bcast_packet)) < 0)
goto dropped;

bcast_packet = (struct bcast_packet *)skb->data;
Expand Down Expand Up @@ -790,7 +789,7 @@ static void interface_setup(struct net_device *dev)

SET_ETHTOOL_OPS(dev, &bat_ethtool_ops);

memset(priv, 0, sizeof(struct bat_priv));
memset(priv, 0, sizeof(*priv));
}

struct net_device *softif_create(const char *name)
Expand All @@ -799,8 +798,7 @@ struct net_device *softif_create(const char *name)
struct bat_priv *bat_priv;
int ret;

soft_iface = alloc_netdev(sizeof(struct bat_priv) , name,
interface_setup);
soft_iface = alloc_netdev(sizeof(*bat_priv), name, interface_setup);

if (!soft_iface) {
pr_err("Unable to allocate the batman interface: %s\n", name);
Expand Down
7 changes: 3 additions & 4 deletions net/batman-adv/translation-table.c
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ void tt_local_add(struct net_device *soft_iface, const uint8_t *addr)
bat_dbg(DBG_ROUTES, bat_priv,
"Creating new local tt entry: %pM\n", addr);

tt_local_entry = kmalloc(sizeof(struct tt_local_entry), GFP_ATOMIC);
tt_local_entry = kmalloc(sizeof(*tt_local_entry), GFP_ATOMIC);
if (!tt_local_entry)
return;

Expand Down Expand Up @@ -427,9 +427,8 @@ void tt_global_add_orig(struct bat_priv *bat_priv,
if (!tt_global_entry) {
spin_unlock_bh(&bat_priv->tt_ghash_lock);

tt_global_entry =
kmalloc(sizeof(struct tt_global_entry),
GFP_ATOMIC);
tt_global_entry = kmalloc(sizeof(*tt_global_entry),
GFP_ATOMIC);

if (!tt_global_entry)
break;
Expand Down
21 changes: 10 additions & 11 deletions net/batman-adv/unicast.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ static struct sk_buff *frag_merge_packet(struct list_head *head,
(struct unicast_frag_packet *)skb->data;
struct sk_buff *tmp_skb;
struct unicast_packet *unicast_packet;
int hdr_len = sizeof(struct unicast_packet);
int uni_diff = sizeof(struct unicast_frag_packet) - hdr_len;
int hdr_len = sizeof(*unicast_packet);
int uni_diff = sizeof(*up) - hdr_len;

/* set skb to the first part and tmp_skb to the second part */
if (up->flags & UNI_FRAG_HEAD) {
Expand All @@ -53,7 +53,7 @@ static struct sk_buff *frag_merge_packet(struct list_head *head,
if (skb_linearize(skb) < 0 || skb_linearize(tmp_skb) < 0)
goto err;

skb_pull(tmp_skb, sizeof(struct unicast_frag_packet));
skb_pull(tmp_skb, sizeof(*up));
if (pskb_expand_head(skb, 0, tmp_skb->len, GFP_ATOMIC) < 0)
goto err;

Expand Down Expand Up @@ -99,8 +99,7 @@ static int frag_create_buffer(struct list_head *head)
struct frag_packet_list_entry *tfp;

for (i = 0; i < FRAG_BUFFER_SIZE; i++) {
tfp = kmalloc(sizeof(struct frag_packet_list_entry),
GFP_ATOMIC);
tfp = kmalloc(sizeof(*tfp), GFP_ATOMIC);
if (!tfp) {
frag_list_free(head);
return -ENOMEM;
Expand Down Expand Up @@ -224,8 +223,8 @@ int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
struct hard_iface *primary_if;
struct sk_buff *frag_skb;
struct unicast_frag_packet *frag1, *frag2;
int uc_hdr_len = sizeof(struct unicast_packet);
int ucf_hdr_len = sizeof(struct unicast_frag_packet);
int uc_hdr_len = sizeof(*unicast_packet);
int ucf_hdr_len = sizeof(*frag1);
int data_len = skb->len - uc_hdr_len;
int large_tail = 0, ret = NET_RX_DROP;
uint16_t seqno;
Expand All @@ -250,14 +249,14 @@ int frag_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv,
frag1 = (struct unicast_frag_packet *)skb->data;
frag2 = (struct unicast_frag_packet *)frag_skb->data;

memcpy(frag1, &tmp_uc, sizeof(struct unicast_packet));
memcpy(frag1, &tmp_uc, sizeof(tmp_uc));

frag1->ttl--;
frag1->version = COMPAT_VERSION;
frag1->packet_type = BAT_UNICAST_FRAG;

memcpy(frag1->orig, primary_if->net_dev->dev_addr, ETH_ALEN);
memcpy(frag2, frag1, sizeof(struct unicast_frag_packet));
memcpy(frag2, frag1, sizeof(*frag2));

if (data_len & 1)
large_tail = UNI_FRAG_LARGETAIL;
Expand Down Expand Up @@ -314,7 +313,7 @@ int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
if (!neigh_node)
goto out;

if (my_skb_head_push(skb, sizeof(struct unicast_packet)) < 0)
if (my_skb_head_push(skb, sizeof(*unicast_packet)) < 0)
goto out;

unicast_packet = (struct unicast_packet *)skb->data;
Expand All @@ -328,7 +327,7 @@ int unicast_send_skb(struct sk_buff *skb, struct bat_priv *bat_priv)
memcpy(unicast_packet->dest, orig_node->orig, ETH_ALEN);

if (atomic_read(&bat_priv->fragmentation) &&
data_len + sizeof(struct unicast_packet) >
data_len + sizeof(*unicast_packet) >
neigh_node->if_incoming->net_dev->mtu) {
/* send frag skb decreases ttl */
unicast_packet->ttl++;
Expand Down
2 changes: 1 addition & 1 deletion net/batman-adv/unicast.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ static inline int frag_can_reassemble(const struct sk_buff *skb, int mtu)
uneven_correction = -1;
}

merged_size = (skb->len - sizeof(struct unicast_frag_packet)) * 2;
merged_size = (skb->len - sizeof(*unicast_packet)) * 2;
merged_size += sizeof(struct unicast_packet) + uneven_correction;

return merged_size <= mtu;
Expand Down
Loading

0 comments on commit 704509b

Please sign in to comment.