media: uvcvideo: Refactor __uvc_ctrl_add_mapping
authorRicardo Ribalda <ribalda@chromium.org>
Tue, 3 Jan 2023 14:36:25 +0000 (15:36 +0100)
committerLaurent Pinchart <laurent.pinchart@ideasonboard.com>
Sun, 15 Jan 2023 21:45:12 +0000 (23:45 +0200)
Simplify the exit code with a common error tag freeing all the memory.

Suggested-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Ricardo Ribalda <ribalda@chromium.org>
Reviewed-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
drivers/media/usb/uvc/uvc_ctrl.c

index 65d41946ef1a1fa67ad8c0d56b1f39c168f96b31..ffa0e26542643940168eeeb9e9241bdcf3f12117 100644 (file)
@@ -2286,32 +2286,30 @@ static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
        unsigned int i;
 
        /*
-        * Most mappings come from static kernel data and need to be duplicated.
+        * Most mappings come from static kernel data, and need to be duplicated.
         * Mappings that come from userspace will be unnecessarily duplicated,
         * this could be optimized.
         */
        map = kmemdup(mapping, sizeof(*mapping), GFP_KERNEL);
-       if (map == NULL)
+       if (!map)
                return -ENOMEM;
 
+       map->name = NULL;
+       map->menu_info = NULL;
+
        /* For UVCIOC_CTRL_MAP custom control */
        if (mapping->name) {
                map->name = kstrdup(mapping->name, GFP_KERNEL);
-               if (!map->name) {
-                       kfree(map);
-                       return -ENOMEM;
-               }
+               if (!map->name)
+                       goto err_nomem;
        }
 
        INIT_LIST_HEAD(&map->ev_subs);
 
        size = sizeof(*mapping->menu_info) * mapping->menu_count;
        map->menu_info = kmemdup(mapping->menu_info, size, GFP_KERNEL);
-       if (map->menu_info == NULL) {
-               kfree(map->name);
-               kfree(map);
-               return -ENOMEM;
-       }
+       if (!map->menu_info)
+               goto err_nomem;
 
        if (map->get == NULL)
                map->get = uvc_get_le_value;
@@ -2332,6 +2330,12 @@ static int __uvc_ctrl_add_mapping(struct uvc_video_chain *chain,
                ctrl->info.selector);
 
        return 0;
+
+err_nomem:
+       kfree(map->menu_info);
+       kfree(map->name);
+       kfree(map);
+       return -ENOMEM;
 }
 
 int uvc_ctrl_add_mapping(struct uvc_video_chain *chain,