Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

iio: gyro: hid-sensor-gyro-3d: Fix memory leak in probe()

'channels' is allocated via kmemdup and it is never freed in the
subsequent error paths.

Use 'indio_dev->channels' directly instead, so that we avoid such
memory leak problem.

Signed-off-by: Fabio Estevam <fabio.estevam@freescale.com>
Reviewed-by: Srinivas Pandruvada <srinivas.pandruvada@linux.intel.com>
Signed-off-by: Jonathan Cameron <jic23@kernel.org>

authored by

Fabio Estevam and committed by
Jonathan Cameron
d8c9d23e b136faff

+6 -7
+6 -7
drivers/iio/gyro/hid-sensor-gyro-3d.c
··· 298 298 struct iio_dev *indio_dev; 299 299 struct gyro_3d_state *gyro_state; 300 300 struct hid_sensor_hub_device *hsdev = pdev->dev.platform_data; 301 - struct iio_chan_spec *channels; 302 301 303 302 indio_dev = devm_iio_device_alloc(&pdev->dev, sizeof(*gyro_state)); 304 303 if (!indio_dev) ··· 316 317 return ret; 317 318 } 318 319 319 - channels = kmemdup(gyro_3d_channels, sizeof(gyro_3d_channels), 320 - GFP_KERNEL); 321 - if (!channels) { 320 + indio_dev->channels = kmemdup(gyro_3d_channels, 321 + sizeof(gyro_3d_channels), GFP_KERNEL); 322 + if (!indio_dev->channels) { 322 323 dev_err(&pdev->dev, "failed to duplicate channels\n"); 323 324 return -ENOMEM; 324 325 } 325 326 326 - ret = gyro_3d_parse_report(pdev, hsdev, channels, 327 - HID_USAGE_SENSOR_GYRO_3D, gyro_state); 327 + ret = gyro_3d_parse_report(pdev, hsdev, 328 + (struct iio_chan_spec *)indio_dev->channels, 329 + HID_USAGE_SENSOR_GYRO_3D, gyro_state); 328 330 if (ret) { 329 331 dev_err(&pdev->dev, "failed to setup attributes\n"); 330 332 goto error_free_dev_mem; 331 333 } 332 334 333 - indio_dev->channels = channels; 334 335 indio_dev->num_channels = ARRAY_SIZE(gyro_3d_channels); 335 336 indio_dev->dev.parent = &pdev->dev; 336 337 indio_dev->info = &gyro_3d_info;