From patchwork Wed Feb 1 02:11:29 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 9548855 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 93722604A0 for ; Wed, 1 Feb 2017 02:11:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 837FB27BA5 for ; Wed, 1 Feb 2017 02:11:49 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 786D12840E; Wed, 1 Feb 2017 02:11:49 +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 525492823D for ; Wed, 1 Feb 2017 02:11:48 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751236AbdBACLn (ORCPT ); Tue, 31 Jan 2017 21:11:43 -0500 Received: from mail-pg0-f65.google.com ([74.125.83.65]:34169 "EHLO mail-pg0-f65.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751146AbdBACLl (ORCPT ); Tue, 31 Jan 2017 21:11:41 -0500 Received: by mail-pg0-f65.google.com with SMTP id 3so14865048pgj.1; Tue, 31 Jan 2017 18:11:41 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=szWEVwrpRh13RCLgHKFIXDnddFWaRgYLYjZ3mI65S6k=; b=QHZsiGh7PK8pRGaS/BDbjqfV0/4ezohb+XizojmIs5jVtppSLM5z7kTxEVf07r/xPn C+TWDbpYmjsObrEBxo3/qzLLW+O4GqhXrYzwRWuQZHLkaKHoFi+yKZ16opEcw/I2SAVs pExuY+BARF/FkkyofnWzeuN8f7mujymU7JQkUhH3AOC5UWS1I+AeXqVu4HghjwsptnG+ OCxPIIZfxJVtCjGgxGTd9yJLnVpqe/wXsA9PyiIiI6f207X+MwP7sjrWQgmAr0iEhalF WQNjlSCnZEOLEbDbrl5vdEd/CweoxHc9qPlh/n8ebEMYbdTMxRK9IBy4ibeuPZJun5Jx Fqtg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=szWEVwrpRh13RCLgHKFIXDnddFWaRgYLYjZ3mI65S6k=; b=lSjDvRNmaH0aFUE1yAE9QbSLjlM8FWSJ7Bj8vMTjxx5ms83oE5ujNiT7+iXE28un2q R7Mev+xZ5gtsEd7jwjRIVDcCb7A7AnYADjzhGaBJntvBjXiDw3cbroguVXv5MCST6Seg Z4Al4bPh/Ovfh/N6A6sZ89PSJIRdy1Hw7w2cpykvYit8+e2U1ugNpfgThBfbk6codaBm /eQJLdPKjLBh1cErW3cix5m6rRyBfo7fAUggtH+zgrxXshifEeneyRvwvK5sYpTKd9bD MWMSf7iv61N13sV4/2y7czphSmvktt80GZve57Vl72gMveGpzRR1EHFH9/LU5vAfenFY q9lQ== X-Gm-Message-State: AIkVDXJP/HeJDffyqd0iAwnuq/Pw0lAX1poTVF9ghmbUGAtB1mgl5Tv7V7KzsXIbW2AKHA== X-Received: by 10.98.72.88 with SMTP id v85mr603720pfa.54.1485915095489; Tue, 31 Jan 2017 18:11:35 -0800 (PST) Received: from dtor-ws.mtv.corp.google.com ([172.22.152.34]) by smtp.gmail.com with ESMTPSA id b66sm44526875pgc.20.2017.01.31.18.11.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Tue, 31 Jan 2017 18:11:34 -0800 (PST) From: Dmitry Torokhov To: "Rafael J. Wysocki" Cc: linux-acpi@vger.kernel.org, linux-kernel@vger.kernel.org, Andy Shevchenko , Mika Westerberg , Hans de Goede , Wolfram Sang Subject: [PATCH v2 3/4] driver property: constify property arrays values Date: Tue, 31 Jan 2017 18:11:29 -0800 Message-Id: <20170201021130.20586-4-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.11.0.483.g087da7b7c-goog In-Reply-To: <20170201021130.20586-1-dmitry.torokhov@gmail.com> References: <20170201021130.20586-1-dmitry.torokhov@gmail.com> 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 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 --- drivers/base/property.c | 65 +++++++++++++++++++++++++++++++++--------------- include/linux/property.h | 12 ++++----- 2 files changed, 51 insertions(+), 26 deletions(-) diff --git a/drivers/base/property.c b/drivers/base/property.c index edc09854520b..fd91e0891665 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -682,44 +682,65 @@ 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]); + 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) { + error = -ENOMEM; + 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 +750,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;