Skip to contentSkip to conversation

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

PATCH In review Gustavo A. R. Silva 2 participants 2 messages Jun 3, 2019 – Jun 11, 2019

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

PATCH 1/1 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.

---
 drivers/i2c/muxes/i2c-mux-pinctrl.c | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/i2c/muxes/i2c-mux-pinctrl.c +2 -3
index cc6818aabab5..ff3e8c9d4dd8 100644
@@ -27,7 +27,7 @@
 
 struct i2c_mux_pinctrl {
 	struct pinctrl *pinctrl;
-	struct pinctrl_state **states;
+	struct pinctrl_state *states[];
 };
 
 static int i2c_mux_pinctrl_select(struct i2c_mux_core *muxc, u32 chan)
@@ -104,14 +104,13 @@ static int i2c_mux_pinctrl_probe(struct platform_device *pdev)
 		return PTR_ERR(parent);
 
 	muxc = i2c_mux_alloc(parent, dev, num_names,
-			     sizeof(*mux) + num_names * sizeof(*mux->states),
+			     struct_size(mux, states, num_names),
 			     0, i2c_mux_pinctrl_select, NULL);
 	if (!muxc) {
 		ret = -ENOMEM;
 		goto err_put_parent;
 	}
 	mux = i2c_mux_priv(muxc);
-	mux->states = (struct pinctrl_state **)(mux + 1);
 
 	platform_set_drvdata(pdev, muxc);
 
Signature · 1 line
2.21.0
#1 lore raw
Quoted 11 lines · Gustavo A. R. Silva

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)

Quoted 7 lines

with:

struct_size(mux, states, num_names)

This code was detected with the help of Coccinelle.

Thanks, patch applied to i2c-mux/for-next

Cheers,
Peter