]> asedeno.scripts.mit.edu Git - linux.git/commitdiff
cfg80211: Use struct_size() in kzalloc()
authorGustavo A. R. Silva <gustavo@embeddedor.com>
Wed, 3 Apr 2019 15:31:51 +0000 (10:31 -0500)
committerJohannes Berg <johannes.berg@intel.com>
Fri, 26 Apr 2019 11:02:11 +0000 (13:02 +0200)
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 foo {
    int stuff;
    struct boo entry[];
};

size = sizeof(struct foo) + count * sizeof(struct boo);
instance = kzalloc(size, GFP_KERNEL)

Instead of leaving these open-coded and prone to type mistakes, we can
now use the new struct_size() helper:

instance = kzalloc(struct_size(instance, entry, count), GFP_KERNEL)

Notice that, in this case, variable size_of_regd is not necessary,
hence it is removed.

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Johannes Berg <johannes.berg@intel.com>
net/wireless/reg.c

index 2f1bf91eb2265a26bcebeeb3589735e77a3a9daa..0d5b11d7c6ed4955ccbe7b73bc9b52602f58b3b0 100644 (file)
@@ -427,14 +427,10 @@ static const struct ieee80211_regdomain *
 reg_copy_regd(const struct ieee80211_regdomain *src_regd)
 {
        struct ieee80211_regdomain *regd;
-       int size_of_regd;
        unsigned int i;
 
-       size_of_regd =
-               sizeof(struct ieee80211_regdomain) +
-               src_regd->n_reg_rules * sizeof(struct ieee80211_reg_rule);
-
-       regd = kzalloc(size_of_regd, GFP_KERNEL);
+       regd = kzalloc(struct_size(regd, reg_rules, src_regd->n_reg_rules),
+                      GFP_KERNEL);
        if (!regd)
                return ERR_PTR(-ENOMEM);
 
@@ -948,12 +944,10 @@ static int regdb_query_country(const struct fwdb_header *db,
        unsigned int ptr = be16_to_cpu(country->coll_ptr) << 2;
        struct fwdb_collection *coll = (void *)((u8 *)db + ptr);
        struct ieee80211_regdomain *regdom;
-       unsigned int size_of_regd, i;
-
-       size_of_regd = sizeof(struct ieee80211_regdomain) +
-               coll->n_rules * sizeof(struct ieee80211_reg_rule);
+       unsigned int i;
 
-       regdom = kzalloc(size_of_regd, GFP_KERNEL);
+       regdom = kzalloc(struct_size(regdom, reg_rules, coll->n_rules),
+                        GFP_KERNEL);
        if (!regdom)
                return -ENOMEM;
 
@@ -1450,7 +1444,7 @@ static struct ieee80211_regdomain *
 regdom_intersect(const struct ieee80211_regdomain *rd1,
                 const struct ieee80211_regdomain *rd2)
 {
-       int r, size_of_regd;
+       int r;
        unsigned int x, y;
        unsigned int num_rules = 0;
        const struct ieee80211_reg_rule *rule1, *rule2;
@@ -1481,10 +1475,7 @@ regdom_intersect(const struct ieee80211_regdomain *rd1,
        if (!num_rules)
                return NULL;
 
-       size_of_regd = sizeof(struct ieee80211_regdomain) +
-                      num_rules * sizeof(struct ieee80211_reg_rule);
-
-       rd = kzalloc(size_of_regd, GFP_KERNEL);
+       rd = kzalloc(struct_size(rd, reg_rules, num_rules), GFP_KERNEL);
        if (!rd)
                return NULL;