Commit d83fb184 authored by Thomas Meyer's avatar Thomas Meyer Committed by Greg Kroah-Hartman
Browse files

staging: iio: Use kcalloc instead of kzalloc to allocate array

The advantage of kcalloc is, that will prevent integer overflows which could
result from the multiplication of number of elements and size and it is also
a bit nicer to read.

The semantic patch that makes this change is available
in https://lkml.org/lkml/2011/11/25/107



Signed-off-by: default avatarThomas Meyer <thomas@m3y3r.de>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 20132043
Loading
Loading
Loading
Loading
+1 −2
Original line number Original line Diff line number Diff line
@@ -93,8 +93,7 @@ static int lis3l02dq_read_all(struct iio_dev *indio_dev, u8 *rx_array)
	struct spi_message msg;
	struct spi_message msg;
	int ret, i, j = 0;
	int ret, i, j = 0;


	xfers = kzalloc((buffer->scan_count) * 2
	xfers = kcalloc((buffer->scan_count) * 2, sizeof(*xfers), GFP_KERNEL);
			* sizeof(*xfers), GFP_KERNEL);
	if (!xfers)
	if (!xfers)
		return -ENOMEM;
		return -ENOMEM;


+3 −3
Original line number Original line Diff line number Diff line
@@ -488,8 +488,8 @@ static int ad7280_channel_init(struct ad7280_state *st)
{
{
	int dev, ch, cnt;
	int dev, ch, cnt;


	st->channels = kzalloc(sizeof(*st->channels) *
	st->channels = kcalloc((st->slave_num + 1) * 12 + 2,
				((st->slave_num + 1) * 12 + 2), GFP_KERNEL);
			       sizeof(*st->channels), GFP_KERNEL);
	if (st->channels == NULL)
	if (st->channels == NULL)
		return -ENOMEM;
		return -ENOMEM;


@@ -683,7 +683,7 @@ static irqreturn_t ad7280_event_handler(int irq, void *private)
	unsigned *channels;
	unsigned *channels;
	int i, ret;
	int i, ret;


	channels = kzalloc(sizeof(*channels) * st->scan_cnt, GFP_KERNEL);
	channels = kcalloc(st->scan_cnt, sizeof(*channels), GFP_KERNEL);
	if (channels == NULL)
	if (channels == NULL)
		return IRQ_HANDLED;
		return IRQ_HANDLED;


+2 −1
Original line number Original line Diff line number Diff line
@@ -518,7 +518,8 @@ static __init int iio_dummy_init(void)
		return -EINVAL;
		return -EINVAL;
	}
	}
	/* Fake a bus */
	/* Fake a bus */
	iio_dummy_devs = kzalloc(sizeof(*iio_dummy_devs)*instances, GFP_KERNEL);
	iio_dummy_devs = kcalloc(instances, sizeof(*iio_dummy_devs),
				 GFP_KERNEL);
	/* Here we have no actual device so call probe */
	/* Here we have no actual device so call probe */
	for (i = 0; i < instances; i++) {
	for (i = 0; i < instances; i++) {
		ret = iio_dummy_probe(i);
		ret = iio_dummy_probe(i);
+6 −8
Original line number Original line Diff line number Diff line
@@ -315,9 +315,8 @@ int iio_buffer_register(struct iio_dev *indio_dev,
			attrcount += ret;
			attrcount += ret;
		}
		}
		if (indio_dev->masklength && buffer->scan_mask == NULL) {
		if (indio_dev->masklength && buffer->scan_mask == NULL) {
			buffer->scan_mask
			buffer->scan_mask = kcalloc(BITS_TO_LONGS(indio_dev->masklength),
				= kzalloc(sizeof(*buffer->scan_mask)*
						    sizeof(*buffer->scan_mask),
					  BITS_TO_LONGS(indio_dev->masklength),
						    GFP_KERNEL);
						    GFP_KERNEL);
			if (buffer->scan_mask == NULL) {
			if (buffer->scan_mask == NULL) {
				ret = -ENOMEM;
				ret = -ENOMEM;
@@ -328,9 +327,8 @@ int iio_buffer_register(struct iio_dev *indio_dev,


	buffer->scan_el_group.name = iio_scan_elements_group_name;
	buffer->scan_el_group.name = iio_scan_elements_group_name;


	buffer->scan_el_group.attrs
	buffer->scan_el_group.attrs = kcalloc(attrcount + 1,
		= kzalloc(sizeof(buffer->scan_el_group.attrs[0])*
					      sizeof(buffer->scan_el_group.attrs[0]),
			  (attrcount + 1),
					      GFP_KERNEL);
					      GFP_KERNEL);
	if (buffer->scan_el_group.attrs == NULL) {
	if (buffer->scan_el_group.attrs == NULL) {
		ret = -ENOMEM;
		ret = -ENOMEM;
+6 −8
Original line number Original line Diff line number Diff line
@@ -669,9 +669,8 @@ static int iio_device_register_sysfs(struct iio_dev *indio_dev)
	if (indio_dev->name)
	if (indio_dev->name)
		attrcount++;
		attrcount++;


	indio_dev->chan_attr_group.attrs
	indio_dev->chan_attr_group.attrs = kcalloc(attrcount + 1,
		= kzalloc(sizeof(indio_dev->chan_attr_group.attrs[0])*
						   sizeof(indio_dev->chan_attr_group.attrs[0]),
			  (attrcount + 1),
						   GFP_KERNEL);
						   GFP_KERNEL);
	if (indio_dev->chan_attr_group.attrs == NULL) {
	if (indio_dev->chan_attr_group.attrs == NULL) {
		ret = -ENOMEM;
		ret = -ENOMEM;
@@ -965,9 +964,8 @@ static int iio_device_register_eventset(struct iio_dev *indio_dev)
	}
	}


	indio_dev->event_interface->group.name = iio_event_group_name;
	indio_dev->event_interface->group.name = iio_event_group_name;
	indio_dev->event_interface->group.attrs =
	indio_dev->event_interface->group.attrs = kcalloc(attrcount + 1,
		kzalloc(sizeof(indio_dev->event_interface->group.attrs[0])
							  sizeof(indio_dev->event_interface->group.attrs[0]),
			*(attrcount + 1),
							  GFP_KERNEL);
							  GFP_KERNEL);
	if (indio_dev->event_interface->group.attrs == NULL) {
	if (indio_dev->event_interface->group.attrs == NULL) {
		ret = -ENOMEM;
		ret = -ENOMEM;
Loading