Skip to content

Commit 428359c

Browse files
rmsilvamchehab
authored andcommitted
media: staging: greybus: light: fix memory leak in v4l2 register
We are allocating memory for the v4l2 flash configuration structure and leak it in the normal path. Just use the stack for this as we do not use it outside of this function. Also use IS_ERR() instead of IS_ERR_OR_NULL() to check return value from v4l2_flash_init() for it never returns NULL. Fixes: 2870b52 ("greybus: lights: add lights implementation") Reported-by: Sakari Ailus <[email protected]> Signed-off-by: Rui Miguel Silva <[email protected]> Reviewed-by: Viresh Kumar <[email protected]> Signed-off-by: Sakari Ailus <[email protected]> Acked-by: Hans Verkuil <[email protected]> Signed-off-by: Mauro Carvalho Chehab <[email protected]>
1 parent b7aaf82 commit 428359c

File tree

1 file changed

+9
-20
lines changed

1 file changed

+9
-20
lines changed

drivers/staging/greybus/light.c

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -534,25 +534,20 @@ static int gb_lights_light_v4l2_register(struct gb_light *light)
534534
{
535535
struct gb_connection *connection = get_conn_from_light(light);
536536
struct device *dev = &connection->bundle->dev;
537-
struct v4l2_flash_config *sd_cfg;
537+
struct v4l2_flash_config sd_cfg = { {0} };
538538
struct led_classdev_flash *fled;
539539
struct led_classdev *iled = NULL;
540540
struct gb_channel *channel_torch, *channel_ind, *channel_flash;
541-
int ret = 0;
542-
543-
sd_cfg = kcalloc(1, sizeof(*sd_cfg), GFP_KERNEL);
544-
if (!sd_cfg)
545-
return -ENOMEM;
546541

547542
channel_torch = get_channel_from_mode(light, GB_CHANNEL_MODE_TORCH);
548543
if (channel_torch)
549544
__gb_lights_channel_v4l2_config(&channel_torch->intensity_uA,
550-
&sd_cfg->torch_intensity);
545+
&sd_cfg.torch_intensity);
551546

552547
channel_ind = get_channel_from_mode(light, GB_CHANNEL_MODE_INDICATOR);
553548
if (channel_ind) {
554549
__gb_lights_channel_v4l2_config(&channel_ind->intensity_uA,
555-
&sd_cfg->indicator_intensity);
550+
&sd_cfg.indicator_intensity);
556551
iled = &channel_ind->fled.led_cdev;
557552
}
558553

@@ -561,27 +556,21 @@ static int gb_lights_light_v4l2_register(struct gb_light *light)
561556

562557
fled = &channel_flash->fled;
563558

564-
snprintf(sd_cfg->dev_name, sizeof(sd_cfg->dev_name), "%s", light->name);
559+
snprintf(sd_cfg.dev_name, sizeof(sd_cfg.dev_name), "%s", light->name);
565560

566561
/* Set the possible values to faults, in our case all faults */
567-
sd_cfg->flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
562+
sd_cfg.flash_faults = LED_FAULT_OVER_VOLTAGE | LED_FAULT_TIMEOUT |
568563
LED_FAULT_OVER_TEMPERATURE | LED_FAULT_SHORT_CIRCUIT |
569564
LED_FAULT_OVER_CURRENT | LED_FAULT_INDICATOR |
570565
LED_FAULT_UNDER_VOLTAGE | LED_FAULT_INPUT_VOLTAGE |
571566
LED_FAULT_LED_OVER_TEMPERATURE;
572567

573568
light->v4l2_flash = v4l2_flash_init(dev, NULL, fled, iled,
574-
&v4l2_flash_ops, sd_cfg);
575-
if (IS_ERR_OR_NULL(light->v4l2_flash)) {
576-
ret = PTR_ERR(light->v4l2_flash);
577-
goto out_free;
578-
}
569+
&v4l2_flash_ops, &sd_cfg);
570+
if (IS_ERR(light->v4l2_flash))
571+
return PTR_ERR(light->v4l2_flash);
579572

580-
return ret;
581-
582-
out_free:
583-
kfree(sd_cfg);
584-
return ret;
573+
return 0;
585574
}
586575

587576
static void gb_lights_light_v4l2_unregister(struct gb_light *light)

0 commit comments

Comments
 (0)