Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

i2c: mux: pinctrl: use flexible-array member and struct_size() helper

Update the code to use a flexible array member instead of a pointer in
structure i2c_mux_pinctrl and use the struct_size() helper.

Also, make use of the struct_size() helper instead of an open-coded
version in order to avoid any potential type mistakes, in particular
in the context in which this code is being used.

So, replace the following form:

sizeof(*mux) + num_names * sizeof(*mux->states)

with:

struct_size(mux, states, num_names)

This code was detected with the help of Coccinelle.

Signed-off-by: Gustavo A. R. Silva <gustavo@embeddedor.com>
Signed-off-by: Peter Rosin <peda@axentia.se>

authored by

Gustavo A. R. Silva and committed by
Peter Rosin
90af2731 d9a183bf

+2 -3
+2 -3
drivers/i2c/muxes/i2c-mux-pinctrl.c
··· 27 27 28 28 struct i2c_mux_pinctrl { 29 29 struct pinctrl *pinctrl; 30 - struct pinctrl_state **states; 30 + struct pinctrl_state *states[]; 31 31 }; 32 32 33 33 static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan) ··· 104 104 return PTR_ERR(parent); 105 105 106 106 muxc = i2c_mux_alloc(parent, dev, num_names, 107 - sizeof(*mux) + num_names * sizeof(*mux->states), 107 + struct_size(mux, states, num_names), 108 108 0, i2c_mux_pinctrl_select, NULL); 109 109 if (!muxc) { 110 110 ret = -ENOMEM; 111 111 goto err_put_parent; 112 112 } 113 113 mux = i2c_mux_priv(muxc); 114 - mux->states = (struct pinctrl_state **)(mux + 1); 115 114 116 115 platform_set_drvdata(pdev, muxc); 117 116