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

media: dvb-usb: Fix error handling in dvb_usb_i2c_init

In dvb_usb_i2c_init, if i2c_add_adapter fails, it only prints an error
message, and then continues to set DVB_USB_STATE_I2C. This affects the
logic of dvb_usb_i2c_exit, which leads to that, the deletion of i2c_adap
even if the i2c_add_adapter fails.

Fix this by returning at the failure of i2c_add_adapter and then move
dvb_usb_i2c_exit out of the error handling code of dvb_usb_i2c_init.

Fixes: 13a79f14ab28 ("media: dvb-usb: Fix memory leak at error in dvb_usb_device_init()")
Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
Signed-off-by: Sean Young <sean@mess.org>
Signed-off-by: Mauro Carvalho Chehab <mchehab+huawei@kernel.org>

authored by

Dongliang Mu and committed by
Mauro Carvalho Chehab
131ae388 797c061a

+8 -3
+7 -2
drivers/media/usb/dvb-usb/dvb-usb-i2c.c
··· 17 17 18 18 if (d->props.i2c_algo == NULL) { 19 19 err("no i2c algorithm specified"); 20 - return -EINVAL; 20 + ret = -EINVAL; 21 + goto err; 21 22 } 22 23 23 24 strscpy(d->i2c_adap.name, d->desc->name, sizeof(d->i2c_adap.name)); ··· 28 27 29 28 i2c_set_adapdata(&d->i2c_adap, d); 30 29 31 - if ((ret = i2c_add_adapter(&d->i2c_adap)) < 0) 30 + ret = i2c_add_adapter(&d->i2c_adap); 31 + if (ret < 0) { 32 32 err("could not add i2c adapter"); 33 + goto err; 34 + } 33 35 34 36 d->state |= DVB_USB_STATE_I2C; 35 37 38 + err: 36 39 return ret; 37 40 } 38 41
+1 -1
drivers/media/usb/dvb-usb/dvb-usb-init.c
··· 194 194 195 195 err_adapter_init: 196 196 dvb_usb_adapter_exit(d); 197 - err_i2c_init: 198 197 dvb_usb_i2c_exit(d); 198 + err_i2c_init: 199 199 if (d->priv && d->props.priv_destroy) 200 200 d->props.priv_destroy(d); 201 201 err_priv_init: