Commit 6f393457 authored by Gustavo A. R. Silva's avatar Gustavo A. R. Silva Committed by David S. Miller

net: ipv6: Use struct_size() helper and kcalloc()

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes. Also, remove unnecessary
function ipv6_rpl_srh_alloc_size() and replace kzalloc() with kcalloc(),
which has a 2-factor argument form for multiplication.

This code was detected with the help of Coccinelle and, audited and
fixed manually.
Signed-off-by: default avatarGustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: default avatarDavid S. Miller <davem@davemloft.net>
parent 5777cbba
...@@ -26,12 +26,6 @@ static inline void rpl_exit(void) {} ...@@ -26,12 +26,6 @@ static inline void rpl_exit(void) {}
/* Worst decompression memory usage ipv6 address (16) + pad 7 */ /* Worst decompression memory usage ipv6 address (16) + pad 7 */
#define IPV6_RPL_SRH_WORST_SWAP_SIZE (sizeof(struct in6_addr) + 7) #define IPV6_RPL_SRH_WORST_SWAP_SIZE (sizeof(struct in6_addr) + 7)
static inline size_t ipv6_rpl_srh_alloc_size(unsigned char n)
{
return sizeof(struct ipv6_rpl_sr_hdr) +
((n + 1) * sizeof(struct in6_addr));
}
size_t ipv6_rpl_srh_size(unsigned char n, unsigned char cmpri, size_t ipv6_rpl_srh_size(unsigned char n, unsigned char cmpri,
unsigned char cmpre); unsigned char cmpre);
......
...@@ -580,7 +580,7 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb) ...@@ -580,7 +580,7 @@ static int ipv6_rpl_srh_rcv(struct sk_buff *skb)
hdr->segments_left--; hdr->segments_left--;
i = n - hdr->segments_left; i = n - hdr->segments_left;
buf = kzalloc(ipv6_rpl_srh_alloc_size(n + 1) * 2, GFP_ATOMIC); buf = kcalloc(struct_size(hdr, segments.addr, n + 2), 2, GFP_ATOMIC);
if (unlikely(!buf)) { if (unlikely(!buf)) {
kfree_skb(skb); kfree_skb(skb);
return -1; return -1;
......
...@@ -136,8 +136,7 @@ static int rpl_do_srh_inline(struct sk_buff *skb, const struct rpl_lwt *rlwt, ...@@ -136,8 +136,7 @@ static int rpl_do_srh_inline(struct sk_buff *skb, const struct rpl_lwt *rlwt,
oldhdr = ipv6_hdr(skb); oldhdr = ipv6_hdr(skb);
buf = kzalloc(ipv6_rpl_srh_alloc_size(srh->segments_left - 1) * 2, buf = kcalloc(struct_size(srh, segments.addr, srh->segments_left), 2, GFP_ATOMIC);
GFP_ATOMIC);
if (!buf) if (!buf)
return -ENOMEM; return -ENOMEM;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment