diff options
| author | Gustavo A. R. Silva <gustavo@embeddedor.com> | 2019-01-08 10:02:33 -0600 |
|---|---|---|
| committer | Mike Snitzer <snitzer@redhat.com> | 2019-02-21 10:37:54 -0500 |
| commit | 042f96ad4c5a369307df2a840fd943a4b080c3d1 (patch) | |
| tree | 68e7dad02bdfac01b080e7244350f01521fcabe7 | |
| parent | 8812b7bfa600fee13324b04a3c34f50188cfbd29 (diff) | |
| download | linux-dm-042f96ad4c5a369307df2a840fd943a4b080c3d1.tar.gz | |
dm switch: use struct_size() in kzalloc()
Notice: this object is not reachable from any branch.
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;
void *entry[];
};
instance = kzalloc(sizeof(struct foo) + sizeof(void *) * count, 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);
This code was detected with the help of Coccinelle.
Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Mike Snitzer <snitzer@redhat.com>
Notice: this object is not reachable from any branch.
| -rw-r--r-- | drivers/md/dm-switch.c | 3 |
1 files changed, 1 insertions, 2 deletions
diff --git a/drivers/md/dm-switch.c b/drivers/md/dm-switch.c index fae35caf36720..8a0f057b81220 100644 --- a/drivers/md/dm-switch.c +++ b/drivers/md/dm-switch.c @@ -61,8 +61,7 @@ static struct switch_ctx *alloc_switch_ctx(struct dm_target *ti, unsigned nr_pat { struct switch_ctx *sctx; - sctx = kzalloc(sizeof(struct switch_ctx) + nr_paths * sizeof(struct switch_path), - GFP_KERNEL); + sctx = kzalloc(struct_size(sctx, path_list, nr_paths), GFP_KERNEL); if (!sctx) return NULL; |
