Re: [PATCH 4/5] iio: pressure: bmp280: Allow multiple chips id per family of devices

From: Andy Shevchenko
Date: Mon Oct 16 2023 - 03:53:48 EST


On Sun, Oct 15, 2023 at 05:16:26PM +0200, Angel Iglesias wrote:
> Improve device detection in certain chip families known to have various
> chip ids.

...

> +#include <linux/overflow.h>

Probably you don't need this, see below.

...

> ret = regmap_read(regmap, data->chip_info->id_reg, &chip_id);
> if (ret < 0)
> return ret;

> + if (i == data->chip_info->num_chip_id) {
> + size_t nbuf;
> + char *buf;
> +
> + // 0x<id>, so four chars per number plus one space + ENDL
> + if (check_mul_overflow(data->chip_info->num_chip_id, 5, &nbuf))
> + return ret;

First of all, it _implicitly_ returns 0 here...

> + buf = kmalloc_array(data->chip_info->num_chip_id, 5, GFP_KERNEL);
> + if (!buf)
> + return ret;

...and here.

Second, kmalloc_array() has that check inside.

Third, define this magic 5 either as strlen(), or a constant (in latter case
with the comment of its meaning).

> + for (i = 0; i < data->chip_info->num_chip_id; i++)
> + snprintf(&buf[i*5], nbuf - i*5, "0x%x ", data->chip_info->chip_id[i]);

Fourth, use incremental position, i.e. use retuned value from snprintf().

> + dev_err(dev, "bad chip id: expected one of [ %s ] got 0x%x\n", buf, chip_id);
> + kfree(buf);

> + return ret;

As per "first" and "second" above.

> }

--
With Best Regards,
Andy Shevchenko