diff mbox series

[v7,1/2] iio: sx9310: Fix access to variable DT array

Message ID 20210326184603.251683-2-gwendal@chromium.org (mailing list archive)
State New, archived
Headers show
Series iio: sx9310: Support ACPI properties | expand

Commit Message

Gwendal Grignou March 26, 2021, 6:46 p.m. UTC
With the current code, we want to read 4 entries from DT array
"semtech,combined-sensors". If there are less, we silently fail as
of_property_read_u32_array() returns -EOVERFLOW.

First count the number of entries and if between 1 and 4, collect the
content of the array.

Fixes: 5b19ca2c78a0 ("iio: sx9310: Set various settings from DT")
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>
---
 No changes in v7.

 Changes in v6:
 Fix error in of_property_count_elems_of_size() argumnent:
 Used ARRAY_SIZE(combined) [4] instead of sizeof(u32).

 Changes in v5:
 new, split fixes from changes needed for ACPI support.

 drivers/iio/proximity/sx9310.c | 40 ++++++++++++++++++++++++----------
 1 file changed, 28 insertions(+), 12 deletions(-)

Comments

Jonathan Cameron March 29, 2021, 12:18 p.m. UTC | #1
On Fri, 26 Mar 2021 11:46:02 -0700
Gwendal Grignou <gwendal@chromium.org> wrote:

> With the current code, we want to read 4 entries from DT array
> "semtech,combined-sensors". If there are less, we silently fail as
> of_property_read_u32_array() returns -EOVERFLOW.
> 
> First count the number of entries and if between 1 and 4, collect the
> content of the array.
> 
> Fixes: 5b19ca2c78a0 ("iio: sx9310: Set various settings from DT")
> Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
> Reviewed-by: Andy Shevchenko <andy.shevchenko@gmail.com>

Applying this one to the fixes-togreg branch of iio.git.

As the second patch isn't a fix it might take a while to get applied.
Gwendal, feel free to poke me if I seem to have lost it after the first
patch reaches my togreg branch.

Thanks,

Jonathan

> ---
>  No changes in v7.
> 
>  Changes in v6:
>  Fix error in of_property_count_elems_of_size() argumnent:
>  Used ARRAY_SIZE(combined) [4] instead of sizeof(u32).
> 
>  Changes in v5:
>  new, split fixes from changes needed for ACPI support.
> 
>  drivers/iio/proximity/sx9310.c | 40 ++++++++++++++++++++++++----------
>  1 file changed, 28 insertions(+), 12 deletions(-)
> 
> diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
> index 394c2afe0f233..289c76bb3b024 100644
> --- a/drivers/iio/proximity/sx9310.c
> +++ b/drivers/iio/proximity/sx9310.c
> @@ -1213,17 +1213,17 @@ static int sx9310_init_compensation(struct iio_dev *indio_dev)
>  }
>  
>  static const struct sx9310_reg_default *
> -sx9310_get_default_reg(struct sx9310_data *data, int i,
> +sx9310_get_default_reg(struct sx9310_data *data, int idx,
>  		       struct sx9310_reg_default *reg_def)
>  {
> -	int ret;
>  	const struct device_node *np = data->client->dev.of_node;
> -	u32 combined[SX9310_NUM_CHANNELS] = { 4, 4, 4, 4 };
> +	u32 combined[SX9310_NUM_CHANNELS];
> +	u32 start = 0, raw = 0, pos = 0;
>  	unsigned long comb_mask = 0;
> +	int ret, i, count;
>  	const char *res;
> -	u32 start = 0, raw = 0, pos = 0;
>  
> -	memcpy(reg_def, &sx9310_default_regs[i], sizeof(*reg_def));
> +	memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def));
>  	if (!np)
>  		return reg_def;
>  
> @@ -1234,15 +1234,31 @@ sx9310_get_default_reg(struct sx9310_data *data, int i,
>  			reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
>  		}
>  
> -		reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
> -		of_property_read_u32_array(np, "semtech,combined-sensors",
> -					   combined, ARRAY_SIZE(combined));
> -		for (i = 0; i < ARRAY_SIZE(combined); i++) {
> -			if (combined[i] <= SX9310_NUM_CHANNELS)
> -				comb_mask |= BIT(combined[i]);
> +		count = of_property_count_elems_of_size(np, "semtech,combined-sensors",
> +							sizeof(u32));
> +		if (count > 0 && count <= ARRAY_SIZE(combined)) {
> +			ret = of_property_read_u32_array(np, "semtech,combined-sensors",
> +							 combined, count);
> +			if (ret)
> +				break;
> +		} else {
> +			/*
> +			 * Either the property does not exist in the DT or the
> +			 * number of entries is incorrect.
> +			 */
> +			break;
> +		}
> +		for (i = 0; i < count; i++) {
> +			if (combined[i] >= SX9310_NUM_CHANNELS) {
> +				/* Invalid sensor (invalid DT). */
> +				break;
> +			}
> +			comb_mask |= BIT(combined[i]);
>  		}
> +		if (i < count)
> +			break;
>  
> -		comb_mask &= 0xf;
> +		reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
>  		if (comb_mask == (BIT(3) | BIT(2) | BIT(1) | BIT(0)))
>  			reg_def->def |= SX9310_REG_PROX_CTRL2_COMBMODE_CS0_CS1_CS2_CS3;
>  		else if (comb_mask == (BIT(1) | BIT(2)))
diff mbox series

Patch

diff --git a/drivers/iio/proximity/sx9310.c b/drivers/iio/proximity/sx9310.c
index 394c2afe0f233..289c76bb3b024 100644
--- a/drivers/iio/proximity/sx9310.c
+++ b/drivers/iio/proximity/sx9310.c
@@ -1213,17 +1213,17 @@  static int sx9310_init_compensation(struct iio_dev *indio_dev)
 }
 
 static const struct sx9310_reg_default *
-sx9310_get_default_reg(struct sx9310_data *data, int i,
+sx9310_get_default_reg(struct sx9310_data *data, int idx,
 		       struct sx9310_reg_default *reg_def)
 {
-	int ret;
 	const struct device_node *np = data->client->dev.of_node;
-	u32 combined[SX9310_NUM_CHANNELS] = { 4, 4, 4, 4 };
+	u32 combined[SX9310_NUM_CHANNELS];
+	u32 start = 0, raw = 0, pos = 0;
 	unsigned long comb_mask = 0;
+	int ret, i, count;
 	const char *res;
-	u32 start = 0, raw = 0, pos = 0;
 
-	memcpy(reg_def, &sx9310_default_regs[i], sizeof(*reg_def));
+	memcpy(reg_def, &sx9310_default_regs[idx], sizeof(*reg_def));
 	if (!np)
 		return reg_def;
 
@@ -1234,15 +1234,31 @@  sx9310_get_default_reg(struct sx9310_data *data, int i,
 			reg_def->def |= SX9310_REG_PROX_CTRL2_SHIELDEN_GROUND;
 		}
 
-		reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
-		of_property_read_u32_array(np, "semtech,combined-sensors",
-					   combined, ARRAY_SIZE(combined));
-		for (i = 0; i < ARRAY_SIZE(combined); i++) {
-			if (combined[i] <= SX9310_NUM_CHANNELS)
-				comb_mask |= BIT(combined[i]);
+		count = of_property_count_elems_of_size(np, "semtech,combined-sensors",
+							sizeof(u32));
+		if (count > 0 && count <= ARRAY_SIZE(combined)) {
+			ret = of_property_read_u32_array(np, "semtech,combined-sensors",
+							 combined, count);
+			if (ret)
+				break;
+		} else {
+			/*
+			 * Either the property does not exist in the DT or the
+			 * number of entries is incorrect.
+			 */
+			break;
+		}
+		for (i = 0; i < count; i++) {
+			if (combined[i] >= SX9310_NUM_CHANNELS) {
+				/* Invalid sensor (invalid DT). */
+				break;
+			}
+			comb_mask |= BIT(combined[i]);
 		}
+		if (i < count)
+			break;
 
-		comb_mask &= 0xf;
+		reg_def->def &= ~SX9310_REG_PROX_CTRL2_COMBMODE_MASK;
 		if (comb_mask == (BIT(3) | BIT(2) | BIT(1) | BIT(0)))
 			reg_def->def |= SX9310_REG_PROX_CTRL2_COMBMODE_CS0_CS1_CS2_CS3;
 		else if (comb_mask == (BIT(1) | BIT(2)))