From patchwork Wed Sep 9 18:33:40 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Kieran Bingham X-Patchwork-Id: 53324 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-wi0-f200.google.com (mail-wi0-f200.google.com [209.85.212.200]) by patches.linaro.org (Postfix) with ESMTPS id 6A72122B05 for ; Wed, 9 Sep 2015 18:34:31 +0000 (UTC) Received: by wicuu12 with SMTP id uu12sf9918241wic.2 for ; Wed, 09 Sep 2015 11:34:30 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:delivered-to:from:to:cc:subject :date:message-id:in-reply-to:references:sender:precedence:list-id :x-original-sender:x-original-authentication-results:mailing-list :list-post:list-help:list-archive:list-unsubscribe; bh=jt/3t5yBhFYwP5dyixMnSVSXtTDRVl6zXdULKtLRo70=; b=EK7AwMZ03yiHsNG098dogt9qnXRs4MKyAKmfASkOgEAJfodPG/0C4UcbEvy8qzFJqI AD0WKxshPNead5B4CXwJsuJ01lvkh4JTqtXDuKi+TD9x6Y7MJXG85hsfTUj46RiTUjjL uXXnyyAN5np3j1rwZZ4YhKruAZr19kaFVqxv7uz8DzQH63MHbZ4fOTazTYjxt3TrpEu9 OsVaGV5sZSdNzLR9f9rdDA+IfQdi9VrzvDzkNLpdksxJhPMoEQQ6HfZBbruPKMBzRPkQ HMXUJOa1fg07zu+LQiJg5Aova8PbhnpBRrj6se/WgHVA9I0FZh/OflAT7HUFDoOkAtGS oemw== X-Gm-Message-State: ALoCoQnNl/yg4ppxFZb85eM8GzaEvNccp7KKcmpzMRgnPu4FsvTbhUM4K15s+8nJ1PwsnertsdeG X-Received: by 10.112.198.33 with SMTP id iz1mr8235731lbc.8.1441823670595; Wed, 09 Sep 2015 11:34:30 -0700 (PDT) MIME-Version: 1.0 X-BeenThere: patchwork-forward@linaro.org Received: by 10.152.6.225 with SMTP id e1ls98030laa.108.gmail; Wed, 09 Sep 2015 11:34:30 -0700 (PDT) X-Received: by 10.152.20.134 with SMTP id n6mr30802918lae.108.1441823670436; Wed, 09 Sep 2015 11:34:30 -0700 (PDT) Received: from mail-la0-x22b.google.com (mail-la0-x22b.google.com. [2a00:1450:4010:c03::22b]) by mx.google.com with ESMTPS id a3si7483479laa.35.2015.09.09.11.34.30 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 09 Sep 2015 11:34:30 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::22b as permitted sender) client-ip=2a00:1450:4010:c03::22b; Received: by lamp12 with SMTP id p12so12540734lam.0 for ; Wed, 09 Sep 2015 11:34:30 -0700 (PDT) X-Received: by 10.152.9.200 with SMTP id c8mr28078675lab.76.1441823670318; Wed, 09 Sep 2015 11:34:30 -0700 (PDT) X-Forwarded-To: patchwork-forward@linaro.org X-Forwarded-For: patch@linaro.org patchwork-forward@linaro.org Delivered-To: patch@linaro.org Received: by 10.112.59.35 with SMTP id w3csp491960lbq; Wed, 9 Sep 2015 11:34:29 -0700 (PDT) X-Received: by 10.67.15.36 with SMTP id fl4mr63026217pad.152.1441823669288; Wed, 09 Sep 2015 11:34:29 -0700 (PDT) Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id ff4si12838802pab.164.2015.09.09.11.34.28; Wed, 09 Sep 2015 11:34:29 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-i2c-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1754782AbbIISe1 (ORCPT + 1 other); Wed, 9 Sep 2015 14:34:27 -0400 Received: from mail-wi0-f182.google.com ([209.85.212.182]:33281 "EHLO mail-wi0-f182.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1754755AbbIISe0 (ORCPT ); Wed, 9 Sep 2015 14:34:26 -0400 Received: by wiclk2 with SMTP id lk2so1635908wic.0; Wed, 09 Sep 2015 11:34:24 -0700 (PDT) X-Received: by 10.180.83.227 with SMTP id t3mr61044337wiy.55.1441823664881; Wed, 09 Sep 2015 11:34:24 -0700 (PDT) Received: from localhost.localdomain (cpc17-aztw24-2-0-cust759.aztw.cable.virginm.net. [92.237.134.248]) by smtp.gmail.com with ESMTPSA id u1sm5155141wiz.22.2015.09.09.11.34.23 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 09 Sep 2015 11:34:23 -0700 (PDT) From: Kieran Bingham To: Wolfram Sang , Samuel Ortiz , Lee Jones Cc: linux-i2c@vger.kernel.org, linux-kernel@vger.kernel.org, linus.walleij@linaro.org, grant.likely@linaro.org Subject: [PATCH v4 1/8] i2c: Add pointer dereference protection to i2c_match_id() Date: Wed, 9 Sep 2015 19:33:40 +0100 Message-Id: <1441823627-6227-2-git-send-email-kieranbingham@gmail.com> X-Mailer: git-send-email 2.1.4 In-Reply-To: <1441823627-6227-1-git-send-email-kieranbingham@gmail.com> References: <1441823627-6227-1-git-send-email-kieranbingham@gmail.com> Sender: linux-i2c-owner@vger.kernel.org Precedence: list List-ID: X-Mailing-List: linux-i2c@vger.kernel.org X-Original-Sender: kieranbingham@gmail.com X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 2a00:1450:4010:c03::22b as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org; dkim=neutral (body hash did not verify) header.i=@gmail.com; dmarc=fail (p=NONE dis=NONE) header.from=gmail.com Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 List-Post: , List-Help: , List-Archive: List-Unsubscribe: , From: Lee Jones Here we're providing dereference protection for i2c_match_id(), which saves us having to do it each time it's called. We're also stripping out the (now) needless checks in i2c_device_match(). This patch paves the way for other, similar code trimming. Acked-by: Grant Likely Signed-off-by: Lee Jones --- drivers/i2c/i2c-core.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/drivers/i2c/i2c-core.c b/drivers/i2c/i2c-core.c index c83e4d1..30d8a77 100644 --- a/drivers/i2c/i2c-core.c +++ b/drivers/i2c/i2c-core.c @@ -450,6 +450,9 @@ static inline int acpi_i2c_install_space_handler(struct i2c_adapter *adapter) static const struct i2c_device_id *i2c_match_id(const struct i2c_device_id *id, const struct i2c_client *client) { + if (!(id && client)) + return NULL; + while (id->name[0]) { if (strcmp(client->name, id->name) == 0) return id; @@ -463,8 +466,6 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv) struct i2c_client *client = i2c_verify_client(dev); struct i2c_driver *driver; - if (!client) - return 0; /* Attempt an OF style match */ if (of_driver_match_device(dev, drv)) @@ -475,9 +476,10 @@ static int i2c_device_match(struct device *dev, struct device_driver *drv) return 1; driver = to_i2c_driver(drv); - /* match on an id table if there is one */ - if (driver->id_table) - return i2c_match_id(driver->id_table, client) != NULL; + + /* Finally an I2C match */ + if (i2c_match_id(driver->id_table, client)) + return 1; return 0; }