netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH][next] nfp: flower: use struct_size() helper
@ 2019-06-05 20:38 Gustavo A. R. Silva
  2019-06-05 21:18 ` [oss-drivers] " Jakub Kicinski
  2019-06-05 23:55 ` David Miller
  0 siblings, 2 replies; 3+ messages in thread
From: Gustavo A. R. Silva @ 2019-06-05 20:38 UTC (permalink / raw)
  To: Jakub Kicinski, David S. Miller
  Cc: oss-drivers, netdev, linux-kernel, Gustavo A. R. Silva

One of the more common cases of allocation size calculations is finding
the size of a structure that has a zero-sized array at the end, along
with memory for some number of elements for that array. For example:

struct nfp_tun_active_tuns {
	...
        struct route_ip_info {
                __be32 ipv4;
                __be32 egress_port;
                __be32 extra[2];
        } tun_info[];
};

Make use of the struct_size() helper instead of an open-coded version
in order to avoid any potential type mistakes.

So, replace the following form:

sizeof(struct nfp_tun_active_tuns) + sizeof(struct route_ip_info) * count

with:

struct_size(payload, tun_info, count)

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
 drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c b/drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c
index 8c67505865a4..a7a80f4b722a 100644
--- a/drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c
+++ b/drivers/net/ethernet/netronome/nfp/flower/tunnel_conf.c
@@ -162,8 +162,7 @@ void nfp_tunnel_keep_alive(struct nfp_app *app, struct sk_buff *skb)
 	}
 
 	pay_len = nfp_flower_cmsg_get_data_len(skb);
-	if (pay_len != sizeof(struct nfp_tun_active_tuns) +
-	    sizeof(struct route_ip_info) * count) {
+	if (pay_len != struct_size(payload, tun_info, count)) {
 		nfp_flower_cmsg_warn(app, "Corruption in tunnel keep-alive message.\n");
 		return;
 	}
-- 
2.21.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [oss-drivers] [PATCH][next] nfp: flower: use struct_size() helper
  2019-06-05 20:38 [PATCH][next] nfp: flower: use struct_size() helper Gustavo A. R. Silva
@ 2019-06-05 21:18 ` Jakub Kicinski
  2019-06-05 23:55 ` David Miller
  1 sibling, 0 replies; 3+ messages in thread
From: Jakub Kicinski @ 2019-06-05 21:18 UTC (permalink / raw)
  To: Gustavo A. R. Silva; +Cc: David S. Miller, oss-drivers, netdev, linux-kernel

On Wed, 5 Jun 2019 15:38:27 -0500, Gustavo A. R. Silva wrote:
> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
> 
> struct nfp_tun_active_tuns {
> 	...
>         struct route_ip_info {
>                 __be32 ipv4;
>                 __be32 egress_port;
>                 __be32 extra[2];
>         } tun_info[];
> };
> 
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
> 
> So, replace the following form:
> 
> sizeof(struct nfp_tun_active_tuns) + sizeof(struct route_ip_info) * count
> 
> with:
> 
> struct_size(payload, tun_info, count)
> 
> This code was detected with the help of Coccinelle.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>

Acked-by: Jakub Kicinski <jakub.kicinski@netronome.com>

Thanks!

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [PATCH][next] nfp: flower: use struct_size() helper
  2019-06-05 20:38 [PATCH][next] nfp: flower: use struct_size() helper Gustavo A. R. Silva
  2019-06-05 21:18 ` [oss-drivers] " Jakub Kicinski
@ 2019-06-05 23:55 ` David Miller
  1 sibling, 0 replies; 3+ messages in thread
From: David Miller @ 2019-06-05 23:55 UTC (permalink / raw)
  To: gustavo; +Cc: jakub.kicinski, oss-drivers, netdev, linux-kernel

From: "Gustavo A. R. Silva" <gustavo@embeddedor.com>
Date: Wed, 5 Jun 2019 15:38:27 -0500

> One of the more common cases of allocation size calculations is finding
> the size of a structure that has a zero-sized array at the end, along
> with memory for some number of elements for that array. For example:
> 
> struct nfp_tun_active_tuns {
> 	...
>         struct route_ip_info {
>                 __be32 ipv4;
>                 __be32 egress_port;
>                 __be32 extra[2];
>         } tun_info[];
> };
> 
> Make use of the struct_size() helper instead of an open-coded version
> in order to avoid any potential type mistakes.
> 
> So, replace the following form:
> 
> sizeof(struct nfp_tun_active_tuns) + sizeof(struct route_ip_info) * count
> 
> with:
> 
> struct_size(payload, tun_info, count)
> 
> This code was detected with the help of Coccinelle.
> 
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>

Applied.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2019-06-05 23:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-06-05 20:38 [PATCH][next] nfp: flower: use struct_size() helper Gustavo A. R. Silva
2019-06-05 21:18 ` [oss-drivers] " Jakub Kicinski
2019-06-05 23:55 ` David Miller

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).