From: cgel.zte@gmail.com
To: davem@davemloft.net
Cc: kuba@kernel.org, netdev@vger.kernel.org,
linux-kernel@vger.kernel.org,
Minghao Chi <chi.minghao@zte.com.cn>,
Zeal Robot <zealci@zte.com.cn>
Subject: [PATCH] net/802: use struct_size over open coded arithmetic
Date: Fri, 28 Jan 2022 08:05:41 +0000 [thread overview]
Message-ID: <20220128080541.1211668-1-chi.minghao@zte.com.cn> (raw)
From: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
Replace zero-length array with flexible-array member and make use
of the struct_size() helper in kmalloc(). For example:
struct garp_attr {
struct rb_node node;
enum garp_applicant_state state;
u8 type;
u8 dlen;
unsigned char data[];
};
Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.
Reported-by: Zeal Robot <zealci@zte.com.cn>
Signed-off-by: Minghao Chi (CGEL ZTE) <chi.minghao@zte.com.cn>
---
net/802/garp.c | 2 +-
net/802/mrp.c | 2 +-
2 files changed, 2 insertions(+), 2 deletions(-)
diff --git a/net/802/garp.c b/net/802/garp.c
index f6012f8e59f0..746763a76f83 100644
--- a/net/802/garp.c
+++ b/net/802/garp.c
@@ -184,7 +184,7 @@ static struct garp_attr *garp_attr_create(struct garp_applicant *app,
return attr;
}
}
- attr = kmalloc(sizeof(*attr) + len, GFP_ATOMIC);
+ attr = kmalloc(struct_size(*attr, data, len), GFP_ATOMIC);
if (!attr)
return attr;
attr->state = GARP_APPLICANT_VO;
diff --git a/net/802/mrp.c b/net/802/mrp.c
index 35e04cc5390c..ce3f1b610a3f 100644
--- a/net/802/mrp.c
+++ b/net/802/mrp.c
@@ -273,7 +273,7 @@ static struct mrp_attr *mrp_attr_create(struct mrp_applicant *app,
return attr;
}
}
- attr = kmalloc(sizeof(*attr) + len, GFP_ATOMIC);
+ attr = kmalloc(struct_size(*attr, value, len), GFP_ATOMIC);
if (!attr)
return attr;
attr->state = MRP_APPLICANT_VO;
--
2.25.1
next reply other threads:[~2022-01-28 8:08 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2022-01-28 8:05 cgel.zte [this message]
2022-01-28 12:40 ` kernel test robot
2022-01-28 12:51 ` kernel test robot
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20220128080541.1211668-1-chi.minghao@zte.com.cn \
--to=cgel.zte@gmail.com \
--cc=chi.minghao@zte.com.cn \
--cc=davem@davemloft.net \
--cc=kuba@kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=zealci@zte.com.cn \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®