From patchwork Wed Jul 7 02:19:22 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Laurent Pinchart X-Patchwork-Id: 12824 Return-Path: X-Original-To: parsemail@patchwork.libcamera.org Delivered-To: parsemail@patchwork.libcamera.org Received: from lancelot.ideasonboard.com (lancelot.ideasonboard.com [92.243.16.209]) by patchwork.libcamera.org (Postfix) with ESMTPS id B0FFBC3226 for ; Wed, 7 Jul 2021 02:20:44 +0000 (UTC) Received: from lancelot.ideasonboard.com (localhost [IPv6:::1]) by lancelot.ideasonboard.com (Postfix) with ESMTP id 9E15468529; Wed, 7 Jul 2021 04:20:41 +0200 (CEST) Authentication-Results: lancelot.ideasonboard.com; dkim=fail reason="signature verification failed" (1024-bit key; unprotected) header.d=ideasonboard.com header.i=@ideasonboard.com header.b="OOeUVSMq"; dkim-atps=neutral Received: from perceval.ideasonboard.com (perceval.ideasonboard.com [IPv6:2001:4b98:dc2:55:216:3eff:fef7:d647]) by lancelot.ideasonboard.com (Postfix) with ESMTPS id 879916850C for ; Wed, 7 Jul 2021 04:20:35 +0200 (CEST) Received: from pendragon.lan (62-78-145-57.bb.dnainternet.fi [62.78.145.57]) by perceval.ideasonboard.com (Postfix) with ESMTPSA id 09385466 for ; Wed, 7 Jul 2021 04:20:34 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com; s=mail; t=1625624435; bh=hmPHGzwAxUE8U1K60Zl1TwCoV+QrKuHJcrle3x6g+Hs=; h=From:To:Subject:Date:In-Reply-To:References:From; b=OOeUVSMqYCYcY6Ojgb+YKqqyS1OOyhUqcvB04ymwMx5snIEpVLsteLDEXTtjexrak 8DAH98u73CakGB+6nBhvDjFlav6saR8JivGaxD3yyryynZ/AdLRSl83hhSbDwbeqt3 iw72q9Knl+StbqTye8BqMDNQDkCjq+y1WNa1DTkM= From: Laurent Pinchart To: libcamera-devel@lists.libcamera.org Date: Wed, 7 Jul 2021 05:19:22 +0300 Message-Id: <20210707021941.20804-12-laurent.pinchart@ideasonboard.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210707021941.20804-1-laurent.pinchart@ideasonboard.com> References: <20210707021941.20804-1-laurent.pinchart@ideasonboard.com> MIME-Version: 1.0 Subject: [libcamera-devel] [PATCH 11/30] cam: options: Avoid copies of OptionvValue and KeyValueParser::Options X-BeenThere: libcamera-devel@lists.libcamera.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libcamera-devel-bounces@lists.libcamera.org Sender: "libcamera-devel" The OptionValue toKeyValues() and toArray() functions return a copy of the values. This is unnecessary, and can cause use-after-free issues when taking references to the return values. Return references instead to optimize the implementation and avoid issues. Signed-off-by: Laurent Pinchart --- src/cam/options.cpp | 22 ++++++++++------------ src/cam/options.h | 4 ++-- 2 files changed, 12 insertions(+), 14 deletions(-) diff --git a/src/cam/options.cpp b/src/cam/options.cpp index 319fab6143c5..9de3dd77fd15 100644 --- a/src/cam/options.cpp +++ b/src/cam/options.cpp @@ -632,27 +632,25 @@ std::string OptionValue::toString() const /** * \brief Retrieve the value as a key-value list - * \return The option value as a KeyValueParser::Options, or an empty list if - * the value type isn't ValueType::ValueKeyValue + * + * The behaviour is undefined if the value type isn't ValueType::ValueKeyValue. + * + * \return The option value as a KeyValueParser::Options */ -KeyValueParser::Options OptionValue::toKeyValues() const +const KeyValueParser::Options &OptionValue::toKeyValues() const { - if (type_ != ValueKeyValue) - return KeyValueParser::Options(); - return keyValues_; } /** * \brief Retrieve the value as an array - * \return The option value as a std::vector of OptionValue, or an empty vector - * if the value type isn't ValueType::ValueArray + * + * The behaviour is undefined if the value type isn't ValueType::ValueArray. + * + * \return The option value as a std::vector of OptionValue */ -std::vector OptionValue::toArray() const +const std::vector &OptionValue::toArray() const { - if (type_ != ValueArray) - return std::vector{}; - return array_; } diff --git a/src/cam/options.h b/src/cam/options.h index 210e502a24e1..09cbc8339dd0 100644 --- a/src/cam/options.h +++ b/src/cam/options.h @@ -141,8 +141,8 @@ public: int toInteger() const; std::string toString() const; - KeyValueParser::Options toKeyValues() const; - std::vector toArray() const; + const KeyValueParser::Options &toKeyValues() const; + const std::vector &toArray() const; const OptionsParser::Options &children() const;