[{"id":18260,"web_url":"https://patchwork.libcamera.org/comment/18260/","msgid":"<20210722125448.bktot4jzbkxkngkc@uno.localdomain>","date":"2021-07-22T12:54:48","subject":"Re: [libcamera-devel] [PATCH v3 13/33] cam: options: Avoid copies\n\tof OptionvValue and KeyValueParser::Options","submitter":{"id":3,"url":"https://patchwork.libcamera.org/api/people/3/","name":"Jacopo Mondi","email":"jacopo@jmondi.org"},"content":"Hi Laurent,\n\nOn Fri, Jul 16, 2021 at 12:14:39AM +0300, 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\nIt might be good to briefly mention that error types are now rising an\nassertion.\n\nReviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n\nThanks\n   j\n\n>\n> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\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 481ac189f115..6e0d802cb986 100644\n> --- a/src/cam/options.cpp\n> +++ b/src/cam/options.cpp\n> @@ -638,27 +638,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 83c409ae4d28..0047b4f220ed 100644\n> --- a/src/cam/options.h\n> +++ b/src/cam/options.h\n> @@ -142,8 +142,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> --\n> Regards,\n>\n> Laurent Pinchart\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 3303EC0109\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 22 Jul 2021 12:54:03 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 91FA968543;\n\tThu, 22 Jul 2021 14:54:02 +0200 (CEST)","from relay6-d.mail.gandi.net (relay6-d.mail.gandi.net\n\t[217.70.183.198])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 2D2476027A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 22 Jul 2021 14:54:01 +0200 (CEST)","(Authenticated sender: jacopo@jmondi.org)\n\tby relay6-d.mail.gandi.net (Postfix) with ESMTPSA id A4E87C0014;\n\tThu, 22 Jul 2021 12:54:00 +0000 (UTC)"],"Date":"Thu, 22 Jul 2021 14:54:48 +0200","From":"Jacopo Mondi <jacopo@jmondi.org>","To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Message-ID":"<20210722125448.bktot4jzbkxkngkc@uno.localdomain>","References":"<20210715211459.19373-1-laurent.pinchart@ideasonboard.com>\n\t<20210715211459.19373-14-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210715211459.19373-14-laurent.pinchart@ideasonboard.com>","Subject":"Re: [libcamera-devel] [PATCH v3 13/33] 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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}},{"id":18264,"web_url":"https://patchwork.libcamera.org/comment/18264/","msgid":"<YPlxMsrcHkpVwBlS@pendragon.ideasonboard.com>","date":"2021-07-22T13:22:58","subject":"Re: [libcamera-devel] [PATCH v3 13/33] cam: options: Avoid copies\n\tof OptionvValue and KeyValueParser::Options","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"content":"Hi Jacopo,\n\nOn Thu, Jul 22, 2021 at 02:54:48PM +0200, Jacopo Mondi wrote:\n> On Fri, Jul 16, 2021 at 12:14:39AM +0300, 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> It might be good to briefly mention that error types are now rising an\n> assertion.\n\nGood point. I'll mention it in the commit message.\n\n> Reviewed-by: Jacopo Mondi <jacopo@jmondi.org>\n> \n> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n> > Reviewed-by: Kieran Bingham <kieran.bingham@ideasonboard.com>\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 481ac189f115..6e0d802cb986 100644\n> > --- a/src/cam/options.cpp\n> > +++ b/src/cam/options.cpp\n> > @@ -638,27 +638,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 83c409ae4d28..0047b4f220ed 100644\n> > --- a/src/cam/options.h\n> > +++ b/src/cam/options.h\n> > @@ -142,8 +142,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> >","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 8F25BC0109\n\tfor <parsemail@patchwork.libcamera.org>;\n\tThu, 22 Jul 2021 13:23:01 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id DD5F668543;\n\tThu, 22 Jul 2021 15:23:00 +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 99CB56027A\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tThu, 22 Jul 2021 15:22:59 +0200 (CEST)","from pendragon.ideasonboard.com (62-78-145-57.bb.dnainternet.fi\n\t[62.78.145.57])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 11C35465;\n\tThu, 22 Jul 2021 15:22:59 +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=\"DBHXhFce\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1626960179;\n\tbh=mpnFKjhDZRtOjGLEa1J0OzeIIQKB20FHjaaLskIlGyk=;\n\th=Date:From:To:Cc:Subject:References:In-Reply-To:From;\n\tb=DBHXhFcegXVA1GeVyRbiES3BCEcWTSRIu4T9XvvBcTeQMCHeD5YO18u47dO3+nzDn\n\t7X6mr223ce/SGKMsx4a6F770a31yHf8JqJyxtciSrtHuyo57pW85tpUXFfTS//fJD0\n\tjroJ60lMVyHZq1VaoeIBtbtWmW1vCxv11Yi5K5is=","Date":"Thu, 22 Jul 2021 16:22:58 +0300","From":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","To":"Jacopo Mondi <jacopo@jmondi.org>","Message-ID":"<YPlxMsrcHkpVwBlS@pendragon.ideasonboard.com>","References":"<20210715211459.19373-1-laurent.pinchart@ideasonboard.com>\n\t<20210715211459.19373-14-laurent.pinchart@ideasonboard.com>\n\t<20210722125448.bktot4jzbkxkngkc@uno.localdomain>","MIME-Version":"1.0","Content-Type":"text/plain; charset=utf-8","Content-Disposition":"inline","In-Reply-To":"<20210722125448.bktot4jzbkxkngkc@uno.localdomain>","Subject":"Re: [libcamera-devel] [PATCH v3 13/33] 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>","Cc":"libcamera-devel@lists.libcamera.org","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"}}]