From patchwork Fri Sep 30 12:14:13 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Johannes Thumshirn X-Patchwork-Id: 676998 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by ozlabs.org (Postfix) with ESMTP id 3slr3X59hLz9s3T for ; Fri, 30 Sep 2016 22:14:48 +1000 (AEST) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S933178AbcI3MOV (ORCPT ); Fri, 30 Sep 2016 08:14:21 -0400 Received: from mx2.suse.de ([195.135.220.15]:50536 "EHLO mx2.suse.de" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S933294AbcI3MOT (ORCPT ); Fri, 30 Sep 2016 08:14:19 -0400 X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (charybdis-ext.suse.de [195.135.220.254]) by mx2.suse.de (Postfix) with ESMTP id 50E94ABC3; Fri, 30 Sep 2016 12:14:16 +0000 (UTC) From: Johannes Thumshirn To: Linus Walleij Cc: linux-gpio@vger.kernel.org, linux-kernel@vger.kernel.org, Johannes Thumshirn Subject: [PATCH 3/3] pinctrl: sunxi: Don't leak memory in case krealloc() failes Date: Fri, 30 Sep 2016 14:14:13 +0200 Message-Id: <1475237653-15164-1-git-send-email-jthumshirn@suse.de> X-Mailer: git-send-email 1.8.5.6 Sender: linux-gpio-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-gpio@vger.kernel.org If krealloc() fails there's no way of freeing the memory previously allocated for pctl->functions as it is overwritten by krealloc()'s return value. Fix this by assigning the return value to a temporary variable first so we can do error handling (which we didn't do at all before). Signed-off-by: Johannes Thumshirn --- drivers/pinctrl/sunxi/pinctrl-sunxi.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/pinctrl/sunxi/pinctrl-sunxi.c b/drivers/pinctrl/sunxi/pinctrl-sunxi.c index 54455af..57dd09b 100644 --- a/drivers/pinctrl/sunxi/pinctrl-sunxi.c +++ b/drivers/pinctrl/sunxi/pinctrl-sunxi.c @@ -787,6 +787,7 @@ static int sunxi_pinctrl_add_function(struct sunxi_pinctrl *pctl, static int sunxi_pinctrl_build_state(struct platform_device *pdev) { struct sunxi_pinctrl *pctl = platform_get_drvdata(pdev); + struct sunxi_pinctrl_function *functions; int i; pctl->ngroups = pctl->desc->npins; @@ -833,9 +834,14 @@ static int sunxi_pinctrl_build_state(struct platform_device *pdev) } } - pctl->functions = krealloc(pctl->functions, - pctl->nfunctions * sizeof(*pctl->functions), - GFP_KERNEL); + functions = krealloc(pctl->functions, + pctl->nfunctions * sizeof(*pctl->functions), + GFP_KERNEL); + if (!functions) { + kfree(pctl->functions); + return -ENOMEM; + } + pctl->functions = functions; for (i = 0; i < pctl->desc->npins; i++) { const struct sunxi_desc_pin *pin = pctl->desc->pins + i;