From patchwork Wed Feb 1 17:31:24 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dmitry Torokhov X-Patchwork-Id: 9550341 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 42AE860425 for ; Wed, 1 Feb 2017 17:33:37 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 343082808C for ; Wed, 1 Feb 2017 17:33:37 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 28B6D28451; Wed, 1 Feb 2017 17:33:37 +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=unavailable 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 C0DD02808C for ; Wed, 1 Feb 2017 17:33:36 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1753593AbdBARbd (ORCPT ); Wed, 1 Feb 2017 12:31:33 -0500 Received: from mail-pf0-f193.google.com ([209.85.192.193]:35022 "EHLO mail-pf0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753577AbdBARba (ORCPT ); Wed, 1 Feb 2017 12:31:30 -0500 Received: by mail-pf0-f193.google.com with SMTP id f144so32269006pfa.2; Wed, 01 Feb 2017 09:31:30 -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=N0734LDeEX6cNkPHEZmasXj29iwOScgKkgUDjY3cR/w=; b=J+11nvm9vT6HHOYQvXvrkhrBS1IpF1t3rsSSpB5VbgKCt6BF04daHCbUBIMI9dfkuE MV205EM2D5Y4xuMtJnqRrtNL+I7mYfMh+xluJJQkDDDASHYpqgbTjKty+ZJ8OvUQaCBp H+S1l295ZrGXF+DwqpGu5x13W3RtDCql5Lly0m6RZE3sLB1Ur457YcDVtrd98t6kIioO JnPbw9stqtzfg8+7pr48vvYdOgrhBSCzgcbAlIEu8oOF/SFQWEovTSng6SMbaYiqroz7 iSXkzlBqUPuLXBpJ5sD6ylI/Az3cao851D3sNgf58u1p7Bg9NMHFC8EVLjAx0++TEa4w xJ0w== 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=N0734LDeEX6cNkPHEZmasXj29iwOScgKkgUDjY3cR/w=; b=jHaTZFLKvQodIkGtlCdRvl9b2YpwrJGAw7IkXJIAInMBspgZtqSsXrb+twkiHobchD X49U+szmD+1IOmvwuKlavHKLDC93wfamHHVgUFp17A0LMhPmR36MBQTX0EcqvX+hRomn y8rlHv3T3AtmpyS2qauJCREycsBlJ2rxZ+ttxWSire7SiYu/OALFcft5+5AV+Z8IkEKd jQvuIZfPZyPan8Extmsz1K/NoWqyEWKOB4lfn169Rjd51CmMzAMmyFDuwHVm7kqLRJtX oz1ydP1sYJVU1J3Fz+Fsy6Ah6fu7bLIcewylAAt1zNoILtEWXfXLGgccaaNK5k/A7enF 2Kfw== X-Gm-Message-State: AIkVDXLSETfE0jJK7bvNVHbl/dP8RRia2QRJuvsPCddnplED6yPVeBgd/4Z0hFFxiFiY/A== X-Received: by 10.99.96.9 with SMTP id u9mr5117740pgb.151.1485970290101; Wed, 01 Feb 2017 09:31:30 -0800 (PST) Received: from dtor-ws.mtv.corp.google.com ([172.22.152.34]) by smtp.gmail.com with ESMTPSA id u14sm51170803pfg.18.2017.02.01.09.31.29 (version=TLS1_2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Wed, 01 Feb 2017 09:31:29 -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 v3 3/4] driver property: constify property arrays values Date: Wed, 1 Feb 2017 09:31:24 -0800 Message-Id: <20170201173125.40354-4-dmitry.torokhov@gmail.com> X-Mailer: git-send-email 2.11.0.483.g087da7b7c-goog In-Reply-To: <20170201173125.40354-1-dmitry.torokhov@gmail.com> References: <20170201173125.40354-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 Reviewed-by: Mika Westerberg --- drivers/base/property.c | 66 +++++++++++++++++++++++++++++++++--------------- include/linux/property.h | 12 ++++----- 2 files changed, 52 insertions(+), 26 deletions(-) diff --git a/drivers/base/property.c b/drivers/base/property.c index edc09854520b..8cfbc94cafb1 100644 --- a/drivers/base/property.c +++ b/drivers/base/property.c @@ -682,44 +682,66 @@ 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) { + 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 +751,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;