i2c: mux: demux-pinctrl: use struct_size() in devm_kzalloc()
PATCH 1/1 i2c: mux: demux-pinctrl: use struct_size() in devm_kzalloc()
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 = devm_kzalloc(dev, 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 = devm_kzalloc(dev, struct_size(instance, entry, count), GFP_KERNEL);
This code was detected with the help of Coccinelle.
--- drivers/i2c/muxes/i2c-demux-pinctrl.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
drivers/i2c/muxes/i2c-demux-pinctrl.c +2 -2
@@ -219,8 +219,8 @@ static int i2c_demux_pinctrl_probe(struct platform_device *pdev)return -EINVAL ; } - priv =devm_kzalloc (&pdev->dev,sizeof (*priv) - + num_chan *sizeof (struct i2c_demux_pinctrl_chan ),GFP_KERNEL ); + priv =devm_kzalloc (&pdev->dev,struct_size (priv, chan, num_chan), +GFP_KERNEL ); props =devm_kcalloc (&pdev->dev, num_chan,sizeof (*props),GFP_KERNEL );
Signature · 1 line
2.20.1
Quoted 3 lines
Instead of leaving these open-coded and prone to type mistakes, we can now use the new
struct_size()helper:
I am okay with this patch in general. However, I think the name of the helper 'struct_size' is a bit unfortunate. I really wondered on the first read why it needs 3 arguments to find out a struct size. I only understood on the second read. I think 'trailing_array_struct_size' would be way more clearer what it does.
Quoted 9 lines · Wolfram Sang
Quoted 3 lines
Instead of leaving these open-coded and prone to type mistakes, we can now use the new
struct_size()helper:I am okay with this patch in general. However, I think the name of the helper '
struct_size' is a bit unfortunate. I really wondered on the first read why it needs 3 arguments to find out a struct size. I only understood on the second read. I think 'trailing_array_struct_size' would be way more clearer what it does.
I read that as an Acked-by... :-)
Cheers,
Peter
Quoted 5 lines · Peter Rosin
Quoted 4 lines · Wolfram Sang
Quoted 3 lines
Instead of leaving these open-coded and prone to type mistakes, we can now use the new
struct_size()helper:
Quoted 7 lines
Quoted 5 lines
I am okay with this patch in general. However, I think the name of the helper '
struct_size' is a bit unfortunate. I really wondered on the first read why it needs 3 arguments to find out a struct size. I only understood on the second read. I think 'trailing_array_struct_size' would be way more clearer what it does.I read that as an Acked-by... :-)
For this patch, yes. Thanks!