]> git.proxmox.com Git - mirror_ubuntu-focal-kernel.git/commit
staging: iio: tsl2x7x: clean up limit checks
authorDan Carpenter <dan.carpenter@oracle.com>
Fri, 8 Sep 2017 10:53:43 +0000 (13:53 +0300)
committerJonathan Cameron <Jonathan.Cameron@huawei.com>
Sun, 24 Sep 2017 11:34:56 +0000 (12:34 +0100)
commit30f130d274c9de9b89d837674accab0f85cbf5a5
treed041c03b3046b6af14a132611e6c57d4eca80129
parent9c84c9101b9b4604c183f75a43fe7875ecce8bcd
staging: iio: tsl2x7x: clean up limit checks

The background of this code is that we can either use the default
tables or load our own table with sysfs.  The default tables are three
element arrays of struct tsl2x7x_lux.  If we load the table with sysfs
then we can have as many as nine elements.  Which ever way we do it, the
last element is always zeroed out.

The most interesting part of this patch is in the
in_illuminance0_lux_table_show() function.  We were using the wrong
limit, "TSL2X7X_MAX_LUX_TABLE_SIZE * 3", when it should have been just
"TSL2X7X_MAX_LUX_TABLE_SIZE".  This creates a static checker warning
that we are going of bounds.  However, since the last element is
always zeroed out, that means we hit the break statement and the code
works correctly despite the wrong limit check.

I made several related readability changes.  The most notable that I
changed the MAX_DEFAULT_TABLE_BYTES define which was:

I renamed the define to TSL2X7X_DEFAULT_TABLE_BYTES because it's not the
max size, it's the only size.  Also the size should really be expressed
as sizeof(struct tsl2x7x_lux) * 3.  In other words, 12 * 3 instead of
4 * 9.  It's 36 bytes either way, so this doesn't change the behavior.

Finally, I created the TSL2X7X_DEF_LUX_TABLE_SZ define instead of using
the magic number 3.  I declared the default tables using that define
to hopefully signal to future programmers that if they want to use a
different size they have to update all the related code.

Signed-off-by: Dan Carpenter <dan.carpenter@oracle.com>
Acked-by: Brian Masney <masneyb@onstation.org>
Signed-off-by: Jonathan Cameron <Jonathan.Cameron@huawei.com>
drivers/staging/iio/light/tsl2x7x.c
drivers/staging/iio/light/tsl2x7x.h