[{"id":18159,"web_url":"https://patchwork.libcamera.org/comment/18159/","msgid":"<ccf6d70b-1f95-c156-daa3-4d739de5ccb5@ideasonboard.com>","date":"2021-07-13T08:28:31","subject":"Re: [libcamera-devel] [PATCH v2 11/30] cam: options: Avoid copies\n\tof OptionvValue and KeyValueParser::Options","submitter":{"id":4,"url":"https://patchwork.libcamera.org/api/people/4/","name":"Kieran Bingham","email":"kieran.bingham@ideasonboard.com"},"content":"Hi Laurent,\n\nOn 12/07/2021 22:56, Laurent Pinchart wrote:\n> The OptionValue toKeyValues() and toArray() functions return a copy of\n> the values. This is unnecessary, and can cause use-after-free issues\n> when taking references to the return values. Return references instead\n> to optimize the implementation and avoid issues.\n> \n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n\nReviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\n\n> ---\n> Changes since v1:\n> \n> - assert() on undefined behaviour\n> ---\n>  src/cam/options.cpp | 24 ++++++++++++------------\n>  src/cam/options.h   |  4 ++--\n>  2 files changed, 14 insertions(+), 14 deletions(-)\n> \n> diff --git a/src/cam/options.cpp b/src/cam/options.cpp\n> index fda6d9764ac5..aa3035fdf364 100644\n> --- a/src/cam/options.cpp\n> +++ b/src/cam/options.cpp\n> @@ -631,27 +631,27 @@ std::string OptionValue::toString() const\n>  \n>  /**\n>   * \\brief Retrieve the value as a key-value list\n> - * \\return The option value as a KeyValueParser::Options, or an empty list if\n> - * the value type isn't ValueType::ValueKeyValue\n> + *\n> + * The behaviour is undefined if the value type isn't ValueType::ValueKeyValue.\n> + *\n> + * \\return The option value as a KeyValueParser::Options\n>   */\n> -KeyValueParser::Options OptionValue::toKeyValues() const\n> +const KeyValueParser::Options &OptionValue::toKeyValues() const\n>  {\n> -\tif (type_ != ValueKeyValue)\n> -\t\treturn KeyValueParser::Options();\n> -\n> +\tassert(type_ == ValueKeyValue);\n>  \treturn keyValues_;\n>  }\n>  \n>  /**\n>   * \\brief Retrieve the value as an array\n> - * \\return The option value as a std::vector of OptionValue, or an empty vector\n> - * if the value type isn't ValueType::ValueArray\n> + *\n> + * The behaviour is undefined if the value type isn't ValueType::ValueArray.\n> + *\n> + * \\return The option value as a std::vector of OptionValue\n>   */\n> -std::vector<OptionValue> OptionValue::toArray() const\n> +const std::vector<OptionValue> &OptionValue::toArray() const\n>  {\n> -\tif (type_ != ValueArray)\n> -\t\treturn std::vector<OptionValue>{};\n> -\n> +\tassert(type_ == ValueArray);\n>  \treturn array_;\n>  }\n>  \n> diff --git a/src/cam/options.h b/src/cam/options.h\n> index 210e502a24e1..09cbc8339dd0 100644\n> --- a/src/cam/options.h\n> +++ b/src/cam/options.h\n> @@ -141,8 +141,8 @@ public:\n>  \n>  \tint toInteger() const;\n>  \tstd::string toString() const;\n> -\tKeyValueParser::Options toKeyValues() const;\n> -\tstd::vector<OptionValue> toArray() const;\n> +\tconst KeyValueParser::Options &toKeyValues() const;\n> +\tconst std::vector<OptionValue> &toArray() const;\n>  \n>  \tconst OptionsParser::Options &children() const;\n>  \n>","headers":{"Return-Path":"<libcamera-devel-bounces@lists.libcamera.org>","X-Original-To":"parsemail@patchwork.libcamera.org","Delivered-To":"parsemail@patchwork.libcamera.org","Received":["from lancelot.ideasonboard.com (lancelot.ideasonboard.com\n\t[92.243.16.209])\n\tby patchwork.libcamera.org (Postfix) with ESMTPS id DFDD5C3225\n\tfor <parsemail@patchwork.libcamera.org>;\n\tTue, 13 Jul 2021 08:28:37 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 41ED768528;\n\tTue, 13 Jul 2021 10:28:37 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 4F3A968516\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 13 Jul 2021 10:28:34 +0200 (CEST)","from [192.168.0.20]\n\t(cpc89244-aztw30-2-0-cust3082.18-1.cable.virginm.net [86.31.172.11])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id CACCCCC;\n\tTue, 13 Jul 2021 10:28:33 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"c0SJnK+t\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1626164913;\n\tbh=LxpJJB2RJ9qG6kcW37QN2XJnprw9wbhkxWcEbiuaglA=;\n\th=Subject:To:References:From:Date:In-Reply-To:From;\n\tb=c0SJnK+tIiht6Psf93KEHDzHtKw4fDCsOojFJwxBQVE/BJq5L8swHcuKWooWBDx3+\n\tU16hHWJMefPYlyyATMuOagoZPvG/nrs1EWOaV4AxVDG1fuOdv7sjTRgIxh/GiqzbV7\n\tboptBtjnuL4umDZRCSRKSwKc+vvLPU9h0NgH3aGo=","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>,\n\tlibcamera-devel@lists.libcamera.org","References":"<20210712215645.30478-1-laurent.pinchart@ideasonboard.com>\n\t<20210712215645.30478-12-laurent.pinchart@ideasonboard.com>","From":"Kieran Bingham <kieran.bingham@ideasonboard.com>","Message-ID":"<ccf6d70b-1f95-c156-daa3-4d739de5ccb5@ideasonboard.com>","Date":"Tue, 13 Jul 2021 09:28:31 +0100","User-Agent":"Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101\n\tThunderbird/78.11.0","MIME-Version":"1.0","In-Reply-To":"<20210712215645.30478-12-laurent.pinchart@ideasonboard.com>","Content-Type":"text/plain; charset=utf-8","Content-Language":"en-GB","Content-Transfer-Encoding":"7bit","Subject":"Re: [libcamera-devel] [PATCH v2 11/30] cam: options: Avoid copies\n\tof OptionvValue and KeyValueParser::Options","X-BeenThere":"libcamera-devel@lists.libcamera.org","X-Mailman-Version":"2.1.29","Precedence":"list","List-Id":"<libcamera-devel.lists.libcamera.org>","List-Unsubscribe":"<https://lists.libcamera.org/options/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=unsubscribe>","List-Archive":"<https://lists.libcamera.org/pipermail/libcamera-devel/>","List-Post":"<mailto:libcamera-devel@lists.libcamera.org>","List-Help":"<mailto:libcamera-devel-request@lists.libcamera.org?subject=help>","List-Subscribe":"<https://lists.libcamera.org/listinfo/libcamera-devel>,\n\t<mailto:libcamera-devel-request@lists.libcamera.org?subject=subscribe>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]