Skip to contentSkip to conversation

ubifs: super: Use struct_size() helper

PATCH In review Gustavo A. R. Silva 2 participants 2 messages Aug 29, 2019 – Sep 15, 2019

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 ubifs: super: Use struct_size() helper

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.

---
 fs/ubifs/super.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/ubifs/super.c +1 -2
index 2706f13e3eb9..ca86489048c8 100644
@@ -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;
Signature · 1 line
2.23.0
#1 lore raw
Quoted 33 lines · 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.

---
 fs/ubifs/super.c | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)
fs/ubifs/super.c +0 -0
index 2706f13e3eb9..ca86489048c8 100644

fs/ubifs/super.c static int init_constants_sb(struct ubifs_info *c) · line 664

Quoted 6 lines
fs/ubifs/super.c +1 -2
@@ -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.

Signature · 2 lines
Thanks,
//richard