* [PATCH] ubifs: super: Use struct_size() helper
@ 2019-08-29 0:50 Gustavo A. R. Silva
2019-09-15 21:58 ` Richard Weinberger
0 siblings, 1 reply; 2+ messages in thread
From: Gustavo A. R. Silva @ 2019-08-29 0:50 UTC (permalink / raw)
To: Richard Weinberger, Artem Bityutskiy, Adrian Hunter
Cc: linux-mtd, 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 ubifs_znode {
...
struct ubifs_zbranch zbranch[];
};
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 ubifs_znode) + c->fanout * sizeof(struct ubifs_zbranch)
with:
struct_size(c->cnext, zbranch, c->fanout)
This code was detected with the help of Coccinelle.
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
---
fs/ubifs/super.c | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index 2706f13e3eb9..ca86489048c8 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -661,8 +661,7 @@ static int init_constants_sb(struct ubifs_info *c)
long long tmp64;
c->main_bytes = (long long)c->main_lebs * c->leb_size;
- c->max_znode_sz = sizeof(struct ubifs_znode) +
- c->fanout * sizeof(struct ubifs_zbranch);
+ c->max_znode_sz = struct_size(c->cnext, zbranch, c->fanout);
tmp = ubifs_idx_node_sz(c, 1);
c->ranges[UBIFS_IDX_NODE].min_len = tmp;
--
2.23.0
^ permalink raw reply related [flat|nested] 2+ messages in thread
* Re: [PATCH] ubifs: super: Use struct_size() helper
2019-08-29 0:50 [PATCH] ubifs: super: Use struct_size() helper Gustavo A. R. Silva
@ 2019-09-15 21:58 ` Richard Weinberger
0 siblings, 0 replies; 2+ messages in thread
From: Richard Weinberger @ 2019-09-15 21:58 UTC (permalink / raw)
To: Gustavo A. R. Silva
Cc: Richard Weinberger, Artem Bityutskiy, Adrian Hunter, linux-mtd, LKML
On Thu, Aug 29, 2019 at 2:50 AM Gustavo A. R. Silva
<gustavo@embeddedor.com> 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 ubifs_znode {
> ...
> struct ubifs_zbranch zbranch[];
> };
>
> 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 ubifs_znode) + c->fanout * sizeof(struct ubifs_zbranch)
>
> with:
>
> struct_size(c->cnext, zbranch, c->fanout)
>
> This code was detected with the help of Coccinelle.
>
> Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
> ---
> fs/ubifs/super.c | 3 +--
> 1 file changed, 1 insertion(+), 2 deletions(-)
>
> diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
> index 2706f13e3eb9..ca86489048c8 100644
> --- a/fs/ubifs/super.c
> +++ b/fs/ubifs/super.c
> @@ -661,8 +661,7 @@ static int init_constants_sb(struct ubifs_info *c)
> long long tmp64;
>
> c->main_bytes = (long long)c->main_lebs * c->leb_size;
> - c->max_znode_sz = sizeof(struct ubifs_znode) +
> - c->fanout * sizeof(struct ubifs_zbranch);
> + c->max_znode_sz = struct_size(c->cnext, zbranch, c->fanout);
First of all, c->fanout is bound checked.
I had to lookup how struct_size() works to understand this
single line of code and why you use the completely unrelated c->cnext here.
Sorry this change does not make the code any better just harder to read.
--
Thanks,
//richard
^ permalink raw reply [flat|nested] 2+ messages in thread
end of thread, other threads:[~2019-09-15 21:58 UTC | newest]
Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-08-29 0:50 [PATCH] ubifs: super: Use struct_size() helper Gustavo A. R. Silva
2019-09-15 21:58 ` Richard Weinberger
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).