From patchwork Sun Mar 6 17:50:26 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jakob Koschel X-Patchwork-Id: 548835 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id E1D29C43217 for ; Sun, 6 Mar 2022 17:51:54 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234127AbiCFRwp (ORCPT ); Sun, 6 Mar 2022 12:52:45 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41462 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234010AbiCFRwf (ORCPT ); Sun, 6 Mar 2022 12:52:35 -0500 Received: from mail-ed1-x52f.google.com (mail-ed1-x52f.google.com [IPv6:2a00:1450:4864:20::52f]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 6CDD7527D6; Sun, 6 Mar 2022 09:51:33 -0800 (PST) Received: by mail-ed1-x52f.google.com with SMTP id w4so14454949edc.7; Sun, 06 Mar 2022 09:51:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=7L/xs4fx6+L+Psy2Q24Oz42Aj8LhoH2R3t77ruSAieQ=; b=VD93FV3zAZ9mxWdMMOnPObLl522sLSooKrrpc/8aSIViodCPTPH1h8uMcDp/Js704J X5P1/in71tJ6NTm+pijd2XRPN/eytV7XscDN94Z5c0MgKFgdBHYkXIYMY/lmHu/iUvs6 b6Uc6glX0ewfqsqVp2BjswgX+7MBmagZaYi7tJ2e1AWUNPg4wcKMuRu7VYmDYG6h5Muv +fTIRWHLg9uQ43zDw/y2G2nkEHProJ6dlOYh8k87JATXogeoYAIShY0IuI5CwaFhZ5y7 D4GK1I7CMN+z6k9f13aOOZ+9cf9iZ4jisNYZh2sMOwMb3KCUOTPV3XAgMk/MuZH6zHap JU7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=7L/xs4fx6+L+Psy2Q24Oz42Aj8LhoH2R3t77ruSAieQ=; b=qWfHgNCaVtE9Rqgm0zttnDubzxxqmSSbjith8GDXy4XdIygIg/cX71h7r1xKjLnY6W F2dbt0cxagyZGvLL56AIHWcvLGGv6krppsJrVpIWO1g9OlNG4sd6oh5FyK+9YpD3Zfa+ bQYPRoD14fW9Ey4W9ko6AKvf2tC/WPdbBo9bqyiUjYO6EjSyaQ9hvOZPvgNQlxe0gmTg Rt0UV8j9on52X3dDhL9NtjvOs3oTpnQy5usSqRTfzlt2CoOWbJRHkl9oILEzRGX6u5he +WwA3ZJ8KAoc8Who6zvZpXzRrBL2DmqAEaUDCg9N1m8bqDZayxNxEzI4ZcBcLd0sHAEE stTA== X-Gm-Message-State: AOAM530aikbGA77UPqQJ9YvhmIB6KXme8JHezhIGwD1lw6BBceBD40OP /As+ybeOKpwyBGcAn/dcWeA= X-Google-Smtp-Source: ABdhPJzik5qGEaWXA/mY5pY2E48KOsS17nIJHMdyugphkWCPmVeey2cfjVdgaH3vxXegNIVnq8fSCw== X-Received: by 2002:aa7:c946:0:b0:416:226f:f8a6 with SMTP id h6-20020aa7c946000000b00416226ff8a6mr6569335edt.360.1646589091947; Sun, 06 Mar 2022 09:51:31 -0800 (PST) Received: from localhost.localdomain (i130160.upc-i.chello.nl. [62.195.130.160]) by smtp.googlemail.com with ESMTPSA id 20-20020a17090601d400b006caff964e30sm4052606ejj.19.2022.03.06.09.51.30 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 06 Mar 2022 09:51:31 -0800 (PST) From: Jakob Koschel To: Greg Kroah-Hartman Cc: Jakob Koschel , linux-kernel@vger.kernel.org, Linus Torvalds , Felipe Balbi , Joel Stanley , Andrew Jeffery , Nicolas Ferre , Alexandre Belloni , Claudiu Beznea , Cristian Birsan , Al Cooper , Li Yang , Vladimir Zapolskiy , Daniel Mack , Haojian Zhuang , Robert Jarzmik , Krzysztof Kozlowski , Alim Akhtar , Thierry Reding , Jonathan Hunter , Michal Simek , linux-usb@vger.kernel.org (open list:USB GADGET/PERIPHERAL SUBSYSTEM), Mike Rapoport , "Brian Johannesmeyer" , Cristiano Giuffrida , "Bos, H.J." Subject: [PATCH 18/26] usb: gadget: legacy: remove using list iterator after loop body as a ptr Date: Sun, 6 Mar 2022 18:50:26 +0100 Message-Id: <20220306175034.3084609-19-jakobkoschel@gmail.com> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20220306175034.3084609-1-jakobkoschel@gmail.com> References: <20220306175034.3084609-1-jakobkoschel@gmail.com> MIME-Version: 1.0 Precedence: bulk List-ID: X-Mailing-List: linux-usb@vger.kernel.org If the list does not contain the expected element, the value of list_for_each_entry() iterator will not point to a valid structure. To avoid type confusion in such case, the list iterator scope will be limited to list_for_each_entry() loop. In preparation to limiting scope of a list iterator to the list traversal loop, use a dedicated pointer to point to the found element [1]. Determining if an element was found is then simply checking if the pointer is != NULL instead of using the potentially bogus pointer. Link: https://lore.kernel.org/all/YhdfEIwI4EdtHdym@kroah.com/ Signed-off-by: Jakob Koschel --- drivers/usb/gadget/legacy/hid.c | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/drivers/usb/gadget/legacy/hid.c b/drivers/usb/gadget/legacy/hid.c index 3912cc805f3a..b7f1ff2ce951 100644 --- a/drivers/usb/gadget/legacy/hid.c +++ b/drivers/usb/gadget/legacy/hid.c @@ -134,7 +134,7 @@ static int hid_bind(struct usb_composite_dev *cdev) { struct usb_gadget *gadget = cdev->gadget; struct list_head *tmp; - struct hidg_func_node *n, *m; + struct hidg_func_node *n = NULL, *m, *tmp_n; struct f_hid_opts *hid_opts; int status, funcs = 0; @@ -144,18 +144,19 @@ static int hid_bind(struct usb_composite_dev *cdev) if (!funcs) return -ENODEV; - list_for_each_entry(n, &hidg_func_list, node) { - n->fi = usb_get_function_instance("hid"); - if (IS_ERR(n->fi)) { - status = PTR_ERR(n->fi); + list_for_each_entry(tmp_n, &hidg_func_list, node) { + tmp_n->fi = usb_get_function_instance("hid"); + if (IS_ERR(tmp_n->fi)) { + status = PTR_ERR(tmp_n->fi); + n = tmp_n; goto put; } - hid_opts = container_of(n->fi, struct f_hid_opts, func_inst); - hid_opts->subclass = n->func->subclass; - hid_opts->protocol = n->func->protocol; - hid_opts->report_length = n->func->report_length; - hid_opts->report_desc_length = n->func->report_desc_length; - hid_opts->report_desc = n->func->report_desc; + hid_opts = container_of(tmp_n->fi, struct f_hid_opts, func_inst); + hid_opts->subclass = tmp_n->func->subclass; + hid_opts->protocol = tmp_n->func->protocol; + hid_opts->report_length = tmp_n->func->report_length; + hid_opts->report_desc_length = tmp_n->func->report_desc_length; + hid_opts->report_desc = tmp_n->func->report_desc; }