From patchwork Mon Nov 26 09:39:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nicholas Mc Guire X-Patchwork-Id: 10697909 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 136435A4 for ; Mon, 26 Nov 2018 09:44:40 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 052F928A4B for ; Mon, 26 Nov 2018 09:44:40 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id EDA932978F; Mon, 26 Nov 2018 09:44:39 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 8131828A4B for ; Mon, 26 Nov 2018 09:44:38 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726279AbeKZUiL (ORCPT ); Mon, 26 Nov 2018 15:38:11 -0500 Received: from www.osadl.org ([62.245.132.105]:51445 "EHLO www.osadl.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726184AbeKZUiL (ORCPT ); Mon, 26 Nov 2018 15:38:11 -0500 Received: from debian01.hofrr.at (178.115.242.59.static.drei.at [178.115.242.59]) by www.osadl.org (8.13.8/8.13.8/OSADL-2007092901) with ESMTP id wAQ9g1B1010454; Mon, 26 Nov 2018 10:42:01 +0100 From: Nicholas Mc Guire To: Lars-Peter Clausen Cc: Michael Hennerich , Jonathan Cameron , Hartmut Knaack , Peter Meerwald-Stadler , Greg Kroah-Hartman , linux-iio@vger.kernel.org, devel@driverdev.osuosl.org, linux-kernel@vger.kernel.org, Nicholas Mc Guire Subject: [PATCH] staging: iio: adc: ad7280a: check for devm_kasprint() failure Date: Mon, 26 Nov 2018 10:39:04 +0100 Message-Id: <1543225144-27468-1-git-send-email-hofrat@osadl.org> X-Mailer: git-send-email 2.1.4 Sender: linux-iio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-iio@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP devm_kasprintf() may return NULL on failure of internal allocation thus the assignments to attr.name are not safe if not checked. On error ad7280_attr_init() returns a negative return so -ENOMEM should be OK here (passed on as return value of the probe function). Signed-off-by: Nicholas Mc Guire Fixes: 2051f25d2a26 ("iio: adc: New driver for AD7280A Lithium Ion Battery Monitoring System2") --- Problem located with an experimental coccinelle script As using if(!st->iio_attr[cnt].dev_attr.attr.name) seamed quite unreadable in this case the (var == NULL) variant was used. Not sure if there are objections against this (checkpatch.pl issues a CHECK on this). Patch was compile tested with: x86_64_defconfig + STAGING=y SPI=y, IIO=y, AD7280=m Patch is against 4.20-rc4 (localversion-next is next-20181126) drivers/staging/iio/adc/ad7280a.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/drivers/staging/iio/adc/ad7280a.c b/drivers/staging/iio/adc/ad7280a.c index 0bb9ab1..5b87530 100644 --- a/drivers/staging/iio/adc/ad7280a.c +++ b/drivers/staging/iio/adc/ad7280a.c @@ -584,6 +584,9 @@ static int ad7280_attr_init(struct ad7280_state *st) devm_kasprintf(&st->spi->dev, GFP_KERNEL, "in%d-in%d_balance_switch_en", index, index + 1); + if (st->iio_attr[cnt].dev_attr.attr.name == NULL) + return -ENOMEM; + ad7280_attributes[cnt] = &st->iio_attr[cnt].dev_attr.attr; cnt++; @@ -600,6 +603,9 @@ static int ad7280_attr_init(struct ad7280_state *st) devm_kasprintf(&st->spi->dev, GFP_KERNEL, "in%d-in%d_balance_timer", index, index + 1); + if (st->iio_attr[cnt].dev_attr.attr.name == NULL) + return -ENOMEM; + ad7280_attributes[cnt] = &st->iio_attr[cnt].dev_attr.attr; }