2014-12-18 17:15:32

by Daniel Baluta

[permalink] [raw]
Subject: [RFC PATCH] iio: ak8975: Make sure chipset is always initialized

When using ACPI, if acpi_match_device fails then chipset enum will be
uninitialized and &ak_def_array[chipset] will point to some bad address.

This fixes the following compilation warning:

drivers/iio/magnetometer/ak8975.c: In function ‘ak8975_probe’:
drivers/iio/magnetometer/ak8975.c:788:14: warning: ‘chipset’ may be used
uninitialized in this function [-Wmaybe-uninitialized]
data->def = &ak_def_array[chipset];

Reported-by: Octavian Purdila <[email protected]>
Signed-off-by: Daniel Baluta <[email protected]>
---
This is a RFC because while I'm pretty sure that chipset should be initialized
with AK_MAX_TYPE in ak8975_match_acpi_device, I am not sure if we can live with
a NULL return value of ak8975_match_acpi_device. Current implementation ignores
return value of ak8975_match_acpi_device.

The same situation is for kxcjk-1013, bmc150-accel, bmg160 and possible other
drivers.

drivers/iio/magnetometer/ak8975.c | 1 +
1 file changed, 1 insertion(+)

diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c
index 0d10a4b..cdf9e77 100644
--- a/drivers/iio/magnetometer/ak8975.c
+++ b/drivers/iio/magnetometer/ak8975.c
@@ -716,6 +716,7 @@ static const char *ak8975_match_acpi_device(struct device *dev,
{
const struct acpi_device_id *id;

+ *chipset = AK_MAX_TYPE;
id = acpi_match_device(dev->driver->acpi_match_table, dev);
if (!id)
return NULL;
--
1.9.1


2014-12-19 22:17:07

by Hartmut Knaack

[permalink] [raw]
Subject: Re: [RFC PATCH] iio: ak8975: Make sure chipset is always initialized

Daniel Baluta schrieb am 18.12.2014 um 18:16:
> When using ACPI, if acpi_match_device fails then chipset enum will be
> uninitialized and &ak_def_array[chipset] will point to some bad address.
>
> This fixes the following compilation warning:
>
> drivers/iio/magnetometer/ak8975.c: In function ‘ak8975_probe’:
> drivers/iio/magnetometer/ak8975.c:788:14: warning: ‘chipset’ may be used
> uninitialized in this function [-Wmaybe-uninitialized]
> data->def =ak_def_array[chipset];
>
> Reported-by: Octavian Purdila <[email protected]>
> Signed-off-by: Daniel Baluta <[email protected]>
> ---
> This is a RFC because while I'm pretty sure that chipset should be initialized
> with AK_MAX_TYPE in ak8975_match_acpi_device, I am not sure if we can live with
> a NULL return value of ak8975_match_acpi_device. Current implementation ignores
> return value of ak8975_match_acpi_device.
This seems to be the actual problem: these _match_acpi_device functions return
NULL on failure, and this should be checked for.

>
> The same situation is for kxcjk-1013, bmc150-accel, bmg160 and possible other
> drivers.
>
> drivers/iio/magnetometer/ak8975.c | 1 +
> 1 file changed, 1 insertion(+)
>
> diff --git a/drivers/iio/magnetometer/ak8975.c b/drivers/iio/magnetometer/ak8975.c
> index 0d10a4b..cdf9e77 100644
> --- a/drivers/iio/magnetometer/ak8975.c
> +++ b/drivers/iio/magnetometer/ak8975.c
> @@ -716,6 +716,7 @@ static const char *ak8975_match_acpi_device(struct device *dev,
> {
> const struct acpi_device_id *id;
>
> + *chipset =K_MAX_TYPE;
> id =cpi_match_device(dev->driver->acpi_match_table, dev);
> if (!id)
> return NULL;
>

2014-12-19 22:25:35

by Daniel Baluta

[permalink] [raw]
Subject: Re: [RFC PATCH] iio: ak8975: Make sure chipset is always initialized

On Sat, Dec 20, 2014 at 12:16 AM, Hartmut Knaack <[email protected]> wrote:
> Daniel Baluta schrieb am 18.12.2014 um 18:16:
>> When using ACPI, if acpi_match_device fails then chipset enum will be
>> uninitialized and &ak_def_array[chipset] will point to some bad address.
>>
>> This fixes the following compilation warning:
>>
>> drivers/iio/magnetometer/ak8975.c: In function ‘ak8975_probe’:
>> drivers/iio/magnetometer/ak8975.c:788:14: warning: ‘chipset’ may be used
>> uninitialized in this function [-Wmaybe-uninitialized]
>> data->def =ak_def_array[chipset];
>>
>> Reported-by: Octavian Purdila <[email protected]>
>> Signed-off-by: Daniel Baluta <[email protected]>
>> ---
>> This is a RFC because while I'm pretty sure that chipset should be initialized
>> with AK_MAX_TYPE in ak8975_match_acpi_device, I am not sure if we can live with
>> a NULL return value of ak8975_match_acpi_device. Current implementation ignores
>> return value of ak8975_match_acpi_device.
> This seems to be the actual problem: these _match_acpi_device functions return
> NULL on failure, and this should be checked for.

Ok, so this would acceptable?

diff --git a/drivers/iio/magnetometer/ak8975.c
b/drivers/iio/magnetometer/ak8975.c
index 0d10a4b..68d99e9 100644
--- a/drivers/iio/magnetometer/ak8975.c
+++ b/drivers/iio/magnetometer/ak8975.c
@@ -776,8 +776,9 @@ static int ak8975_probe(struct i2c_client *client,
name = id->name;
} else if (ACPI_HANDLE(&client->dev))
name = ak8975_match_acpi_device(&client->dev, &chipset);
- else
- return -ENOSYS;
+
+ if (!name)
+ return -ENODEV;


I still have some doubts about return code in case of error.

For ak8975 we use -ENOSYS, but for kxcjk-1013 we use -ENODEV.

I will send a patch after we clear this out.

thanks,
Daniel.

2014-12-20 21:26:40

by srinivas pandruvada

[permalink] [raw]
Subject: Re: [RFC PATCH] iio: ak8975: Make sure chipset is always initialized

On Sat, 2014-12-20 at 00:25 +0200, Daniel Baluta wrote:
> On Sat, Dec 20, 2014 at 12:16 AM, Hartmut Knaack <[email protected]> wrote:
> > Daniel Baluta schrieb am 18.12.2014 um 18:16:
> >> When using ACPI, if acpi_match_device fails then chipset enum will be
> >> uninitialized and &ak_def_array[chipset] will point to some bad address.
> >>
I am missing something. You are enumerated over i2c device, which was
created from ACPI PNP resource. There is a valid handle or and the
device has an ACPI companion at the least. If this failing, I have to
check the code for acpi i2c.
Can you check why this check failed? We may have bug in i2c handling.

Thanks,
Srinivas

> >> This fixes the following compilation warning:
> >>
> >> drivers/iio/magnetometer/ak8975.c: In function ‘ak8975_probe’:
> >> drivers/iio/magnetometer/ak8975.c:788:14: warning: ‘chipset’ may be used
> >> uninitialized in this function [-Wmaybe-uninitialized]
> >> data->def =ak_def_array[chipset];
> >>
> >> Reported-by: Octavian Purdila <[email protected]>
> >> Signed-off-by: Daniel Baluta <[email protected]>
> >> ---
> >> This is a RFC because while I'm pretty sure that chipset should be initialized
> >> with AK_MAX_TYPE in ak8975_match_acpi_device, I am not sure if we can live with
> >> a NULL return value of ak8975_match_acpi_device. Current implementation ignores
> >> return value of ak8975_match_acpi_device.
> > This seems to be the actual problem: these _match_acpi_device functions return
> > NULL on failure, and this should be checked for.
>
> Ok, so this would acceptable?
>
> diff --git a/drivers/iio/magnetometer/ak8975.c
> b/drivers/iio/magnetometer/ak8975.c
> index 0d10a4b..68d99e9 100644
> --- a/drivers/iio/magnetometer/ak8975.c
> +++ b/drivers/iio/magnetometer/ak8975.c
> @@ -776,8 +776,9 @@ static int ak8975_probe(struct i2c_client *client,
> name = id->name;
> } else if (ACPI_HANDLE(&client->dev))
> name = ak8975_match_acpi_device(&client->dev, &chipset);
> - else
> - return -ENOSYS;
> +
> + if (!name)
> + return -ENODEV;
>
>
> I still have some doubts about return code in case of error.
>
> For ak8975 we use -ENOSYS, but for kxcjk-1013 we use -ENODEV.
>
> I will send a patch after we clear this out.
>
> thanks,
> Daniel.

2014-12-20 21:29:16

by Pandruvada, Srinivas

[permalink] [raw]
Subject: Re: [RFC PATCH] iio: ak8975: Make sure chipset is always initialized

+Mika

On Sat, 2014-12-20 at 13:26 -0800, Srinivas Pandruvada wrote:
> On Sat, 2014-12-20 at 00:25 +0200, Daniel Baluta wrote:
> > On Sat, Dec 20, 2014 at 12:16 AM, Hartmut Knaack <[email protected]> wrote:
> > > Daniel Baluta schrieb am 18.12.2014 um 18:16:
> > >> When using ACPI, if acpi_match_device fails then chipset enum will be
> > >> uninitialized and &ak_def_array[chipset] will point to some bad address.
> > >>
> I am missing something. You are enumerated over i2c device, which was
> created from ACPI PNP resource. There is a valid handle or and the
> device has an ACPI companion at the least. If this failing, I have to
> check the code for acpi i2c.
> Can you check why this check failed? We may have bug in i2c handling.
>
> Thanks,
> Srinivas
>
> > >> This fixes the following compilation warning:
> > >>
> > >> drivers/iio/magnetometer/ak8975.c: In function ‘ak8975_probe’:
> > >> drivers/iio/magnetometer/ak8975.c:788:14: warning: ‘chipset’ may be used
> > >> uninitialized in this function [-Wmaybe-uninitialized]
> > >> data->def =ak_def_array[chipset];
> > >>
> > >> Reported-by: Octavian Purdila <[email protected]>
> > >> Signed-off-by: Daniel Baluta <[email protected]>
> > >> ---
> > >> This is a RFC because while I'm pretty sure that chipset should be initialized
> > >> with AK_MAX_TYPE in ak8975_match_acpi_device, I am not sure if we can live with
> > >> a NULL return value of ak8975_match_acpi_device. Current implementation ignores
> > >> return value of ak8975_match_acpi_device.
> > > This seems to be the actual problem: these _match_acpi_device functions return
> > > NULL on failure, and this should be checked for.
> >
> > Ok, so this would acceptable?
> >
> > diff --git a/drivers/iio/magnetometer/ak8975.c
> > b/drivers/iio/magnetometer/ak8975.c
> > index 0d10a4b..68d99e9 100644
> > --- a/drivers/iio/magnetometer/ak8975.c
> > +++ b/drivers/iio/magnetometer/ak8975.c
> > @@ -776,8 +776,9 @@ static int ak8975_probe(struct i2c_client *client,
> > name = id->name;
> > } else if (ACPI_HANDLE(&client->dev))
> > name = ak8975_match_acpi_device(&client->dev, &chipset);
> > - else
> > - return -ENOSYS;
> > +
> > + if (!name)
> > + return -ENODEV;
> >
> >
> > I still have some doubts about return code in case of error.
> >
> > For ak8975 we use -ENOSYS, but for kxcjk-1013 we use -ENODEV.
> >
> > I will send a patch after we clear this out.
> >
> > thanks,
> > Daniel.
>

????{.n?+???????+%?????ݶ??w??{.n?+????{??G?????{ay?ʇڙ?,j??f???h?????????z_??(?階?ݢj"???m??????G????????????&???~???iO???z??v?^?m???? ????????I?

2014-12-20 21:40:22

by Daniel Baluta

[permalink] [raw]
Subject: Re: [RFC PATCH] iio: ak8975: Make sure chipset is always initialized

I will have closer look on why acpi_match_device could fail. This patch
was only based on code reading when trying to fix the compiler warning
mentioned in the commit message.

[Sorry for top posting]

On Sat, Dec 20, 2014 at 11:29 PM, Pandruvada, Srinivas
<[email protected]> wrote:
> +Mika
>
> On Sat, 2014-12-20 at 13:26 -0800, Srinivas Pandruvada wrote:
>> On Sat, 2014-12-20 at 00:25 +0200, Daniel Baluta wrote:
>> > On Sat, Dec 20, 2014 at 12:16 AM, Hartmut Knaack <[email protected]> wrote:
>> > > Daniel Baluta schrieb am 18.12.2014 um 18:16:
>> > >> When using ACPI, if acpi_match_device fails then chipset enum will be
>> > >> uninitialized and &ak_def_array[chipset] will point to some bad address.
>> > >>
>> I am missing something. You are enumerated over i2c device, which was
>> created from ACPI PNP resource. There is a valid handle or and the
>> device has an ACPI companion at the least. If this failing, I have to
>> check the code for acpi i2c.
>> Can you check why this check failed? We may have bug in i2c handling.
>>
>> Thanks,
>> Srinivas
>>
>> > >> This fixes the following compilation warning:
>> > >>
>> > >> drivers/iio/magnetometer/ak8975.c: In function ‘ak8975_probe’:
>> > >> drivers/iio/magnetometer/ak8975.c:788:14: warning: ‘chipset’ may be used
>> > >> uninitialized in this function [-Wmaybe-uninitialized]
>> > >> data->def =ak_def_array[chipset];
>> > >>
>> > >> Reported-by: Octavian Purdila <[email protected]>
>> > >> Signed-off-by: Daniel Baluta <[email protected]>
>> > >> ---
>> > >> This is a RFC because while I'm pretty sure that chipset should be initialized
>> > >> with AK_MAX_TYPE in ak8975_match_acpi_device, I am not sure if we can live with
>> > >> a NULL return value of ak8975_match_acpi_device. Current implementation ignores
>> > >> return value of ak8975_match_acpi_device.
>> > > This seems to be the actual problem: these _match_acpi_device functions return
>> > > NULL on failure, and this should be checked for.
>> >
>> > Ok, so this would acceptable?
>> >
>> > diff --git a/drivers/iio/magnetometer/ak8975.c
>> > b/drivers/iio/magnetometer/ak8975.c
>> > index 0d10a4b..68d99e9 100644
>> > --- a/drivers/iio/magnetometer/ak8975.c
>> > +++ b/drivers/iio/magnetometer/ak8975.c
>> > @@ -776,8 +776,9 @@ static int ak8975_probe(struct i2c_client *client,
>> > name = id->name;
>> > } else if (ACPI_HANDLE(&client->dev))
>> > name = ak8975_match_acpi_device(&client->dev, &chipset);
>> > - else
>> > - return -ENOSYS;
>> > +
>> > + if (!name)
>> > + return -ENODEV;
>> >
>> >
>> > I still have some doubts about return code in case of error.
>> >
>> > For ak8975 we use -ENOSYS, but for kxcjk-1013 we use -ENODEV.
>> >
>> > I will send a patch after we clear this out.
>> >
>> > thanks,
>> > Daniel.
>>
>