{"id":12924,"url":"https://patchwork.libcamera.org/api/1.1/patches/12924/?format=json","web_url":"https://patchwork.libcamera.org/patch/12924/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/1.1/projects/1/?format=json","name":"libcamera","link_name":"libcamera","list_id":"libcamera_core","list_email":"libcamera-devel@lists.libcamera.org","web_url":"","scm_url":"","webscm_url":""},"msgid":"<20210712215645.30478-12-laurent.pinchart@ideasonboard.com>","date":"2021-07-12T21:56:26","name":"[libcamera-devel,v2,11/30] cam: options: Avoid copies of OptionvValue and KeyValueParser::Options","commit_ref":null,"pull_url":null,"state":"superseded","archived":false,"hash":"d34ff0d52ca02db78b926ff678e044b2c1b37bbe","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/1.1/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/12924/mbox/","series":[{"id":2229,"url":"https://patchwork.libcamera.org/api/1.1/series/2229/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=2229","date":"2021-07-12T21:56:15","name":"Multi-camera support in the cam application","version":2,"mbox":"https://patchwork.libcamera.org/series/2229/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/12924/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/12924/checks/","tags":{},"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 49241C3226\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 12 Jul 2021 21:57:47 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 81A0368534;\n\tMon, 12 Jul 2021 23:57:46 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 866A368521\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 12 Jul 2021 23:57:39 +0200 (CEST)","from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 261F63F1\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tMon, 12 Jul 2021 23:57:39 +0200 (CEST)"],"Authentication-Results":"lancelot.ideasonboard.com;\n\tdkim=fail reason=\"signature verification failed\" (1024-bit key;\n\tunprotected) header.d=ideasonboard.com header.i=@ideasonboard.com\n\theader.b=\"dVYJohgs\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1626127059;\n\tbh=2Nrao7hCiOuTxzzmFwGmv8Sdo5LoiSincf+DAC/x624=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=dVYJohgsQgmPiGtbNrATpBojOIOs6Qnum4zFlb2wjE5GWB9NGeW+zDuRyOjZz6elN\n\t3MbxT72qAs6sEC6FafkYH06sY11RpBj4CHVPdCL+DtIrwVlH6IZFIVJj6Rj8nCT60O\n\t3cQ7Em0oFbjhtYcIJxZPtun42OG1tGQfnSGIDQTY=","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Date":"Tue, 13 Jul 2021 00:56:26 +0300","Message-Id":"<20210712215645.30478-12-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.31.1","In-Reply-To":"<20210712215645.30478-1-laurent.pinchart@ideasonboard.com>","References":"<20210712215645.30478-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v2 11/30] cam: options: Avoid copies of\n\tOptionvValue 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>"},"content":"The OptionValue toKeyValues() and toArray() functions return a copy of\nthe values. This is unnecessary, and can cause use-after-free issues\nwhen taking references to the return values. Return references instead\nto optimize the implementation and avoid issues.\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\nChanges 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(-)","diff":"diff --git a/src/cam/options.cpp b/src/cam/options.cpp\nindex 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 \ndiff --git a/src/cam/options.h b/src/cam/options.h\nindex 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","prefixes":["libcamera-devel","v2","11/30"]}