Commit 53e8785c authored by Nicholas Mc Guire's avatar Nicholas Mc Guire Committed by Jonathan Cameron

staging: iio: adc: ad7280a: check for devm_kasprint() failure

devm_kasprintf() may return NULL on failure of internal allocation thus
the assignments to  attr.name  are not safe if not checked. On error
ad7280_attr_init() returns a negative return so -ENOMEM should be
OK here (passed on as return value of the probe function). To make the
error case more readable a temporary  iio_attr  is introduced and the code
refactored.
Signed-off-by: default avatarNicholas Mc Guire <hofrat@osadl.org>
Fixes: 2051f25d ("iio: adc: New driver for AD7280A Lithium Ion Battery Monitoring System2")
Signed-off-by: default avatarJonathan Cameron <Jonathan.Cameron@huawei.com>
parent 88884a50
...@@ -561,6 +561,7 @@ static int ad7280_attr_init(struct ad7280_state *st) ...@@ -561,6 +561,7 @@ static int ad7280_attr_init(struct ad7280_state *st)
{ {
int dev, ch, cnt; int dev, ch, cnt;
unsigned int index; unsigned int index;
struct iio_dev_attr *iio_attr;
st->iio_attr = devm_kcalloc(&st->spi->dev, 2, sizeof(*st->iio_attr) * st->iio_attr = devm_kcalloc(&st->spi->dev, 2, sizeof(*st->iio_attr) *
(st->slave_num + 1) * AD7280A_CELLS_PER_DEV, (st->slave_num + 1) * AD7280A_CELLS_PER_DEV,
...@@ -571,37 +572,35 @@ static int ad7280_attr_init(struct ad7280_state *st) ...@@ -571,37 +572,35 @@ static int ad7280_attr_init(struct ad7280_state *st)
for (dev = 0, cnt = 0; dev <= st->slave_num; dev++) for (dev = 0, cnt = 0; dev <= st->slave_num; dev++)
for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_CELL_VOLTAGE_6; for (ch = AD7280A_CELL_VOLTAGE_1; ch <= AD7280A_CELL_VOLTAGE_6;
ch++, cnt++) { ch++, cnt++) {
iio_attr = &st->iio_attr[cnt];
index = dev * AD7280A_CELLS_PER_DEV + ch; index = dev * AD7280A_CELLS_PER_DEV + ch;
st->iio_attr[cnt].address = iio_attr->address = ad7280a_devaddr(dev) << 8 | ch;
ad7280a_devaddr(dev) << 8 | ch; iio_attr->dev_attr.attr.mode = 0644;
st->iio_attr[cnt].dev_attr.attr.mode = iio_attr->dev_attr.show = ad7280_show_balance_sw;
0644; iio_attr->dev_attr.store = ad7280_store_balance_sw;
st->iio_attr[cnt].dev_attr.show = iio_attr->dev_attr.attr.name =
ad7280_show_balance_sw;
st->iio_attr[cnt].dev_attr.store =
ad7280_store_balance_sw;
st->iio_attr[cnt].dev_attr.attr.name =
devm_kasprintf(&st->spi->dev, GFP_KERNEL, devm_kasprintf(&st->spi->dev, GFP_KERNEL,
"in%d-in%d_balance_switch_en", "in%d-in%d_balance_switch_en",
index, index + 1); index, index + 1);
ad7280_attributes[cnt] = if (!iio_attr->dev_attr.attr.name)
&st->iio_attr[cnt].dev_attr.attr; return -ENOMEM;
ad7280_attributes[cnt] = &iio_attr->dev_attr.attr;
cnt++; cnt++;
st->iio_attr[cnt].address = iio_attr = &st->iio_attr[cnt];
ad7280a_devaddr(dev) << 8 | iio_attr->address = ad7280a_devaddr(dev) << 8 |
(AD7280A_CB1_TIMER + ch); (AD7280A_CB1_TIMER + ch);
st->iio_attr[cnt].dev_attr.attr.mode = iio_attr->dev_attr.attr.mode = 0644;
0644; iio_attr->dev_attr.show = ad7280_show_balance_timer;
st->iio_attr[cnt].dev_attr.show = iio_attr->dev_attr.store = ad7280_store_balance_timer;
ad7280_show_balance_timer; iio_attr->dev_attr.attr.name =
st->iio_attr[cnt].dev_attr.store =
ad7280_store_balance_timer;
st->iio_attr[cnt].dev_attr.attr.name =
devm_kasprintf(&st->spi->dev, GFP_KERNEL, devm_kasprintf(&st->spi->dev, GFP_KERNEL,
"in%d-in%d_balance_timer", "in%d-in%d_balance_timer",
index, index + 1); index, index + 1);
ad7280_attributes[cnt] = if (!iio_attr->dev_attr.attr.name)
&st->iio_attr[cnt].dev_attr.attr; return -ENOMEM;
ad7280_attributes[cnt] = &iio_attr->dev_attr.attr;
} }
ad7280_attributes[cnt] = NULL; ad7280_attributes[cnt] = NULL;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment