tipc: Use struct_size() helper
authorGustavo A. R. Silva <gustavoars@kernel.org>
Thu, 18 Jun 2020 13:35:00 +0000 (08:35 -0500)
committerDavid S. Miller <davem@davemloft.net>
Sat, 20 Jun 2020 03:15:25 +0000 (20:15 -0700)
Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

This code was detected with the help of Coccinelle and, audited and
fixed manually.

Signed-off-by: Gustavo A. R. Silva <gustavoars@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
net/tipc/link.c
net/tipc/msg.h

index eac89a3..1c57935 100644 (file)
@@ -1385,12 +1385,12 @@ u16 tipc_get_gap_ack_blks(struct tipc_gap_ack_blks **ga, struct tipc_link *l,
                p = (struct tipc_gap_ack_blks *)msg_data(hdr);
                sz = ntohs(p->len);
                /* Sanity check */
-               if (sz == tipc_gap_ack_blks_sz(p->ugack_cnt + p->bgack_cnt)) {
+               if (sz == struct_size(p, gacks, p->ugack_cnt + p->bgack_cnt)) {
                        /* Good, check if the desired type exists */
                        if ((uc && p->ugack_cnt) || (!uc && p->bgack_cnt))
                                goto ok;
                /* Backward compatible: peer might not support bc, but uc? */
-               } else if (uc && sz == tipc_gap_ack_blks_sz(p->ugack_cnt)) {
+               } else if (uc && sz == struct_size(p, gacks, p->ugack_cnt)) {
                        if (p->ugack_cnt) {
                                p->bgack_cnt = 0;
                                goto ok;
@@ -1472,7 +1472,7 @@ static u16 tipc_build_gap_ack_blks(struct tipc_link *l, struct tipc_msg *hdr)
                        __tipc_build_gap_ack_blks(ga, l, ga->bgack_cnt) : 0;
 
        /* Total len */
-       len = tipc_gap_ack_blks_sz(ga->bgack_cnt + ga->ugack_cnt);
+       len = struct_size(ga, gacks, ga->bgack_cnt + ga->ugack_cnt);
        ga->len = htons(len);
        return len;
 }
@@ -1521,7 +1521,7 @@ static int tipc_link_advance_transmq(struct tipc_link *l, struct tipc_link *r,
                gacks = &ga->gacks[ga->bgack_cnt];
        } else if (ga) {
                /* Copy the Gap ACKs, bc part, for later renewal if needed */
-               this_ga = kmemdup(ga, tipc_gap_ack_blks_sz(ga->bgack_cnt),
+               this_ga = kmemdup(ga, struct_size(ga, gacks, ga->bgack_cnt),
                                  GFP_ATOMIC);
                if (likely(this_ga)) {
                        this_ga->start_index = 0;
index 65119e8..1016e96 100644 (file)
@@ -189,11 +189,9 @@ struct tipc_gap_ack_blks {
        struct tipc_gap_ack gacks[];
 };
 
-#define tipc_gap_ack_blks_sz(n) (sizeof(struct tipc_gap_ack_blks) + \
-                                sizeof(struct tipc_gap_ack) * (n))
-
 #define MAX_GAP_ACK_BLKS       128
-#define MAX_GAP_ACK_BLKS_SZ    tipc_gap_ack_blks_sz(MAX_GAP_ACK_BLKS)
+#define MAX_GAP_ACK_BLKS_SZ    (sizeof(struct tipc_gap_ack_blks) + \
+                                sizeof(struct tipc_gap_ack) * MAX_GAP_ACK_BLKS)
 
 static inline struct tipc_msg *buf_msg(struct sk_buff *skb)
 {