From patchwork Thu Oct 15 13:08:48 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Linus Walleij X-Patchwork-Id: 55006 Return-Path: X-Original-To: linaro@patches.linaro.org Delivered-To: linaro@patches.linaro.org Received: from mail-lf0-f69.google.com (mail-lf0-f69.google.com [209.85.215.69]) by patches.linaro.org (Postfix) with ESMTPS id 7ADCD2301F for ; Thu, 15 Oct 2015 13:11:12 +0000 (UTC) Received: by lfbe131 with SMTP id e131sf2135839lfb.3 for ; Thu, 15 Oct 2015 06:11:11 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:delivered-to:from:to:subject:date:message-id :in-reply-to:references:precedence:list-id:list-unsubscribe :list-archive:list-post:list-help:list-subscribe:cc:mime-version :content-type:content-transfer-encoding:sender:errors-to :x-original-sender:x-original-authentication-results:mailing-list; bh=RT/OOzfDjKQZosuuzNC9BnI9yz+fkKb419bIJhBTPSI=; b=HNEN+Wg6hHLSRbqgtsPNMVfVcHwxvwnpBcOQZT+Dnj1HmvWR1Pog7aa7USvCwJ5zzT ZJWJiO2YYYlc0U9N1hZFpbhB+p4a3wJtxJZi6QUGj4ypETT0mIOYqw6RRpm5WBqC2vK6 pU3c8dlp9jLV884YJJ8SMLOyM3XJmTckfbht3nnV+b6Wxx8Q1spVmWwkoKzLJaM7b6z1 3TkAv41+bgIyTI4xz/GJvdNYiR+FoMnXqg7zZaz1n9ppAhb644oh9g906nxp6PpksizD R65TfFNtIrXNKHyDIU7pbUXfBBdyA/lKQFbbgM5/B7K3XKIDBzKqM4eiWIjlVlNMOlzs 9uAQ== X-Gm-Message-State: ALoCoQlgLVes6CP7mDVAJQUrUoG+RyMlBJWfxqhuAL3IuL01xCiDtO9Ufmv6qbEADTVaIwjZc401 X-Received: by 10.194.118.65 with SMTP id kk1mr2137062wjb.5.1444914671465; Thu, 15 Oct 2015 06:11:11 -0700 (PDT) X-BeenThere: patchwork-forward@linaro.org Received: by 10.25.89.207 with SMTP id n198ls155944lfb.54.gmail; Thu, 15 Oct 2015 06:11:11 -0700 (PDT) X-Received: by 10.112.13.34 with SMTP id e2mr3524333lbc.120.1444914671305; Thu, 15 Oct 2015 06:11:11 -0700 (PDT) Received: from mail-lf0-f43.google.com (mail-lf0-f43.google.com. [209.85.215.43]) by mx.google.com with ESMTPS id t197si9102831lfe.92.2015.10.15.06.11.09 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Oct 2015 06:11:09 -0700 (PDT) Received-SPF: pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.43 as permitted sender) client-ip=209.85.215.43; Received: by lfaz124 with SMTP id z124so27281059lfa.1 for ; Thu, 15 Oct 2015 06:11:09 -0700 (PDT) X-Received: by 10.25.38.9 with SMTP id m9mr2879576lfm.112.1444914668761; Thu, 15 Oct 2015 06:11:08 -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 w3csp625074lbq; Thu, 15 Oct 2015 06:11:07 -0700 (PDT) X-Received: by 10.68.168.2 with SMTP id zs2mr10137139pbb.154.1444914666916; Thu, 15 Oct 2015 06:11:06 -0700 (PDT) Received: from bombadil.infradead.org (bombadil.infradead.org. [2001:1868:205::9]) by mx.google.com with ESMTPS id os5si21520794pab.233.2015.10.15.06.11.06 for (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Oct 2015 06:11:06 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-mtd-bounces+patch=linaro.org@lists.infradead.org designates 2001:1868:205::9 as permitted sender) client-ip=2001:1868:205::9; Received: from localhost ([127.0.0.1] helo=bombadil.infradead.org) by bombadil.infradead.org with esmtp (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZmiJ1-0007Th-4Z; Thu, 15 Oct 2015 13:10:51 +0000 Received: from mail-lb0-f172.google.com ([209.85.217.172]) by bombadil.infradead.org with esmtps (Exim 4.80.1 #2 (Red Hat Linux)) id 1ZmiHz-0005NH-OQ for linux-mtd@lists.infradead.org; Thu, 15 Oct 2015 13:09:49 +0000 Received: by lbbwb3 with SMTP id wb3so6355345lbb.1 for ; Thu, 15 Oct 2015 06:09:25 -0700 (PDT) X-Received: by 10.112.140.197 with SMTP id ri5mr4500165lbb.65.1444914565837; Thu, 15 Oct 2015 06:09:25 -0700 (PDT) Received: from localhost.localdomain ([85.235.10.227]) by smtp.gmail.com with ESMTPSA id rf8sm2036081lbb.20.2015.10.15.06.09.24 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 15 Oct 2015 06:09:24 -0700 (PDT) From: Linus Walleij To: David Woodhouse , Brian Norris , linux-mtd@lists.infradead.org Subject: [PATCH 05/10] mtd: afs: simplify partition parsing Date: Thu, 15 Oct 2015 15:08:48 +0200 Message-Id: <1444914533-27782-6-git-send-email-linus.walleij@linaro.org> X-Mailer: git-send-email 2.4.3 In-Reply-To: <1444914533-27782-1-git-send-email-linus.walleij@linaro.org> References: <1444914533-27782-1-git-send-email-linus.walleij@linaro.org> X-CRM114-Version: 20100106-BlameMichelson ( TRE 0.8.0 (BSD) ) MR-646709E3 X-CRM114-CacheID: sfid-20151015_060948_024220_149DA43D X-CRM114-Status: GOOD ( 21.77 ) X-Spam-Score: -2.6 (--) X-Spam-Report: SpamAssassin version 3.4.0 on bombadil.infradead.org summary: Content analysis details: (-2.6 points) pts rule name description ---- ---------------------- -------------------------------------------------- -0.7 RCVD_IN_DNSWL_LOW RBL: Sender listed at http://www.dnswl.org/, low trust [209.85.217.172 listed in list.dnswl.org] -0.0 RCVD_IN_MSPIKE_H2 RBL: Average reputation (+2) [209.85.217.172 listed in wl.mailspike.net] -0.0 SPF_PASS SPF: sender matches SPF record -1.9 BAYES_00 BODY: Bayes spam probability is 0 to 1% [score: 0.0000] X-BeenThere: linux-mtd@lists.infradead.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: , List-Help: , List-Subscribe: , Cc: Linus Walleij , Liviu Dudau , Ryan Harkin , linux-arm-kernel@lists.infradead.org MIME-Version: 1.0 Sender: "linux-mtd" Errors-To: linux-mtd-bounces+patch=linaro.org@lists.infradead.org X-Removed-Original-Auth: Dkim didn't pass. X-Original-Sender: linus.walleij@linaro.org X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of patch+caf_=patchwork-forward=linaro.org@linaro.org designates 209.85.215.43 as permitted sender) smtp.mailfrom=patch+caf_=patchwork-forward=linaro.org@linaro.org Mailing-list: list patchwork-forward@linaro.org; contact patchwork-forward+owners@linaro.org X-Google-Group-Id: 836684582541 This simplifies the AFS partition parsing to make the code more straight-forward and readable. Before this patch the code tried to calculate the memory required to hold the partition info by adding up the sizes of the strings of the names and adding that to a single memory allocation, indexing the name pointers in front of the struct mtd_partition allocations so all allocated data was in one chunk. This is overzealous. Instead use kstrdup and bail out, kfree():ing the memory used for MTD partitions and names alike on the errorpath. In the process rename the index variable from idx to i. Cc: Ryan Harkin Cc: Liviu Dudau Signed-off-by: Linus Walleij --- drivers/mtd/afs.c | 67 ++++++++++++++++++++++++++----------------------------- 1 file changed, 32 insertions(+), 35 deletions(-) diff --git a/drivers/mtd/afs.c b/drivers/mtd/afs.c index a1eea50ce180..9e6089615f16 100644 --- a/drivers/mtd/afs.c +++ b/drivers/mtd/afs.c @@ -166,9 +166,9 @@ static int parse_afs_partitions(struct mtd_info *mtd, struct mtd_part_parser_data *data) { struct mtd_partition *parts; - u_int mask, off, idx, sz; + u_int mask, off, sz; int ret = 0; - char *str; + int i; /* * This is the address mask; we use this to mask off out of @@ -181,78 +181,75 @@ static int parse_afs_partitions(struct mtd_info *mtd, * partition information. We include in this the size of * the strings. */ - for (idx = off = sz = 0; off < mtd->size; off += mtd->erasesize) { - struct image_info_v1 iis; + for (i = off = sz = 0; off < mtd->size; off += mtd->erasesize) { u_int iis_ptr, img_ptr; ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); if (ret < 0) - break; + return ret; if (ret) { - ret = afs_read_iis_v1(mtd, &iis, iis_ptr); - if (ret < 0) - break; - if (ret == 0) - continue; - sz += sizeof(struct mtd_partition); - sz += strlen(iis.name) + 1; - idx += 1; + i += 1; } } - if (!sz) - return ret; + if (!i) + return 0; parts = kzalloc(sz, GFP_KERNEL); if (!parts) return -ENOMEM; - str = (char *)(parts + idx); - /* * Identify the partitions */ - for (idx = off = 0; off < mtd->size; off += mtd->erasesize) { + for (i = off = 0; off < mtd->size; off += mtd->erasesize) { struct image_info_v1 iis; u_int iis_ptr, img_ptr; /* Read the footer. */ ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask); if (ret < 0) - break; + goto out_free_parts; if (ret == 0) continue; /* Read the image info block */ ret = afs_read_iis_v1(mtd, &iis, iis_ptr); if (ret < 0) - break; + goto out_free_parts; if (ret == 0) continue; - strcpy(str, iis.name); + parts[i].name = kstrdup(iis.name, GFP_KERNEL); + if (!parts[i].name) { + ret = -ENOMEM; + goto out_free_parts; + } - parts[idx].name = str; - parts[idx].size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1); - parts[idx].offset = img_ptr; - parts[idx].mask_flags = 0; + parts[i].size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1); + parts[i].offset = img_ptr; + parts[i].mask_flags = 0; printk(" mtd%d: at 0x%08x, %5lluKiB, %8u, %s\n", - idx, img_ptr, parts[idx].size / 1024, - iis.imageNumber, str); - - idx += 1; - str = str + strlen(iis.name) + 1; - } + i, img_ptr, parts[i].size / 1024, + iis.imageNumber, parts[i].name); - if (!idx) { - kfree(parts); - parts = NULL; + i += 1; } *pparts = parts; - return idx ? idx : ret; + return i; + +out_free_parts: + while (i >= 0) { + if (parts[i].name) + kfree(parts[i].name); + i--; + } + kfree(parts); + *pparts = NULL; + return ret; } static struct mtd_part_parser afs_parser = {