From patchwork Thu Feb 2 16:39:18 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 9552481 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 7514160236 for ; Thu, 2 Feb 2017 16:39:25 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 62A642848B for ; Thu, 2 Feb 2017 16:39:25 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 54ABA28491; Thu, 2 Feb 2017 16:39:25 +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=-6.3 required=2.0 tests=BAYES_00, DKIM_ADSP_CUSTOM_MED, DKIM_SIGNED, FREEMAIL_FROM, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM, T_DKIM_INVALID 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 447422848B for ; Thu, 2 Feb 2017 16:39:24 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751316AbdBBQjW (ORCPT ); Thu, 2 Feb 2017 11:39:22 -0500 Received: from mail-pf0-f194.google.com ([209.85.192.194]:35285 "EHLO mail-pf0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750879AbdBBQjW (ORCPT ); Thu, 2 Feb 2017 11:39:22 -0500 Received: by mail-pf0-f194.google.com with SMTP id f144so1823128pfa.2; Thu, 02 Feb 2017 08:39:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=nnbm9rMr1o9s2NUoSv1LacWnrhB6X3JvwVF9wQMwXpM=; b=e4KRxMrDEBh+qA/NZuE+yQ8hOEnkZR0GNgD8ergkgVZiuKcHbscUAu7F2FKvnqHaED IED+3ZrlLoGiRBWVoEBpiWiZh8UQRIyyBjwGtpiOpeRlepxEWtQC87hs3DWRCu3Wbvsp ZTU6uRozjBACfO+ywegJU7K4tneSd0CsQPSw6rCzPA1DKnyZNL5oqk8KJF/gMo48SuF6 Ufq1y9UTdyjArhqgnMA7n2SEj7cGvMxVn2ruUt4TlTl+b2Tyt4IGAq+ex9UI6KKyeXUM XghgFPTajRdXjazVrZqH2axc49CTgKg2g/dEjX1Qe1RwXw7bBhEjrXZgCwTri9Pt51ND B9Pw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to:user-agent; bh=nnbm9rMr1o9s2NUoSv1LacWnrhB6X3JvwVF9wQMwXpM=; b=WGZ2nfUNpTOIRuLOAB8BaxOACzBsN+r/VtdYTIPMNVMXJyN/o/vec9e6K+OW2Oy4+G H32oY/KFVrsyZIf7WqDdMGFppzq6tZHUGHxo5OhD3xcyBFCQCIAGXh2MbHCQ6++R1awr rb4Cu64p51H2zSmoQbEw4NKdEBeI5jll0t6u6dosJAQ9psb9Q5CfW+qKdRdZPyBtpvO+ uNciOd22AidXegtR8RxzQO+LuZv0dcmYP/A2to2Fbi+VVNfmUy1wZYoRrWRel40BVYAA CFisSmxCrI2kdprtNClfEPIdA8u7BH9pGb+WA6nStJbpBMmZhHFTYfb8k8gFSgGtJ/8J s8eQ== X-Gm-Message-State: AIkVDXJe5dZA0pQ+8YMQ6Z5np8k9/ZiHIp4zDg5jxMhlPBZ68DoFTfzl6fERaPnkLlXcOg== X-Received: by 10.98.41.5 with SMTP id p5mr11851436pfp.183.1486053561222; Thu, 02 Feb 2017 08:39:21 -0800 (PST) Received: from dtor-ws ([2620:0:1000:1311:189:d04a:34b8:7bb2]) by smtp.gmail.com with ESMTPSA id l188sm59845959pfl.28.2017.02.02.08.39.20 (version=TLS1_2 cipher=AES128-SHA bits=128/128); Thu, 02 Feb 2017 08:39:20 -0800 (PST) Date: Thu, 2 Feb 2017 08:39:18 -0800 From: Dmitry Torokhov To: "Rafael J. Wysocki" Cc: Joe Perches , "Rafael J. Wysocki" , linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , Mika Westerberg , Hans de Goede , Wolfram Sang Subject: [PATCH v4 3/4] device property: constify property arrays values Message-ID: <20170202163918.GA21924@dtor-ws> References: <20170201173125.40354-1-dmitry.torokhov@gmail.com> <20170201173125.40354-4-dmitry.torokhov@gmail.com> <1485991803.22276.14.camel@perches.com> <1605853.brEpCT4tOG@aspire.rjw.lan> MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: <1605853.brEpCT4tOG@aspire.rjw.lan> User-Agent: Mutt/1.5.21 (2010-09-15) Sender: linux-acpi-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-acpi@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Dmitry Torokhov Data that is fed into property arrays should not be modified, so let's mark relevant pointers as const. This will allow us making source arrays as const/__initconst. Also fix memory leaks on errors in property_entry_copy(). Signed-off-by: Dmitry Torokhov --- Not sending the rest of the series as to not clutter mailing lists too much... drivers/base/property.c | 66 +++++++++++++++++++++++++++++++--------------- include/linux/property.h | 12 ++++---- 2 files changed, 51 insertions(+), 27 deletions(-) -- To unsubscribe from this list: send the line "unsubscribe linux-acpi" in the body of a message to majordomo@vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html diff --git a/drivers/base/property.c b/drivers/base/property.c index edc09854520b..09fb9757e086 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -682,44 +682,64 @@ int fwnode_property_match_string(struct fwnode_handle *fwnode, } EXPORT_SYMBOL_GPL(fwnode_property_match_string); +static int property_copy_string_array(struct property_entry *dst, + const struct property_entry *src) +{ + char **d; + size_t nval = src->length / sizeof(*d); + size_t i; + + d = kcalloc(nval, sizeof(*d), GFP_KERNEL); + if (!d) + return -ENOMEM; + + for (i = 0; i < nval; i++) { + d[i] = kstrdup(src->pointer.str[i], GFP_KERNEL); + if (!d[i] && src->pointer.str[i]) { + while (--i >= 0) + kfree(d[i]); + kfree(d); + return -ENOMEM; + } + } + + dst->pointer.str = (void *)d; + return 0; +} + static int property_entry_copy(struct property_entry *dst, const struct property_entry *src) { - const char **d, **s; - size_t i, nval; + int error; dst->name = kstrdup(src->name, GFP_KERNEL); if (!dst->name) return -ENOMEM; if (src->is_array) { - if (!src->length) - return -ENODATA; + if (!src->length) { + error = -ENODATA; + goto out_free_name; + } if (src->is_string) { - nval = src->length / sizeof(const char *); - dst->pointer.str = kcalloc(nval, sizeof(const char *), - GFP_KERNEL); - if (!dst->pointer.str) - return -ENOMEM; - - d = dst->pointer.str; - s = src->pointer.str; - for (i = 0; i < nval; i++) { - d[i] = kstrdup(s[i], GFP_KERNEL); - if (!d[i] && s[i]) - return -ENOMEM; - } + error = property_copy_string_array(dst, src); + if (error) + goto out_free_name; } else { dst->pointer.raw_data = kmemdup(src->pointer.raw_data, src->length, GFP_KERNEL); - if (!dst->pointer.raw_data) - return -ENOMEM; + if (!dst->pointer.raw_data) { + error = -ENOMEM; + goto out_free_name; + } } } else if (src->is_string) { dst->value.str = kstrdup(src->value.str, GFP_KERNEL); - if (!dst->value.str && src->value.str) - return -ENOMEM; + if (!dst->value.str && src->value.str) { + error = -ENOMEM; + goto out_free_name; + } } else { dst->value.raw_data = src->value.raw_data; } @@ -729,6 +749,10 @@ static int property_entry_copy(struct property_entry *dst, dst->is_string = src->is_string; return 0; + +out_free_name: + kfree(dst->name); + return error; } /** diff --git a/include/linux/property.h b/include/linux/property.h index 5746e9927016..64e3a9c6d95f 100644 --- a/include/linux/property.h +++ b/include/linux/property.h @@ -160,12 +160,12 @@ struct property_entry { bool is_string; union { union { - void *raw_data; - u8 *u8_data; - u16 *u16_data; - u32 *u32_data; - u64 *u64_data; - const char **str; + const void *raw_data; + const u8 *u8_data; + const u16 *u16_data; + const u32 *u32_data; + const u64 *u64_data; + const char * const *str; } pointer; union { unsigned long long raw_data;