{"id":21287,"url":"https://patchwork.libcamera.org/api/patches/21287/?format=json","web_url":"https://patchwork.libcamera.org/patch/21287/","project":{"id":1,"url":"https://patchwork.libcamera.org/api/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":"<20240916222834.464460-1-stefan.klug@ideasonboard.com>","date":"2024-09-16T22:28:20","name":"libcamera: yaml-parser: Differentiate between empty and empty string","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"ab25d6e4bdd6d22649c639acf01123e675cdd84b","submitter":{"id":184,"url":"https://patchwork.libcamera.org/api/people/184/?format=json","name":"Stefan Klug","email":"stefan.klug@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/21287/mbox/","series":[{"id":4604,"url":"https://patchwork.libcamera.org/api/series/4604/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=4604","date":"2024-09-16T22:28:20","name":"libcamera: yaml-parser: Differentiate between empty and empty string","version":1,"mbox":"https://patchwork.libcamera.org/series/4604/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/21287/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/21287/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 9060BC324C\n\tfor <parsemail@patchwork.libcamera.org>;\n\tMon, 16 Sep 2024 22:28:55 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id 43612634FD;\n\tTue, 17 Sep 2024 00:28:54 +0200 (CEST)","from perceval.ideasonboard.com (perceval.ideasonboard.com\n\t[213.167.242.64])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTPS id 14700634F4\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tTue, 17 Sep 2024 00:28:52 +0200 (CEST)","from ideasonboard.com (unknown [31.12.6.210])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 9709E23F;\n\tTue, 17 Sep 2024 00:27:30 +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=\"FFZUiTzj\"; dkim-atps=neutral","DKIM-Signature":"v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1726525650;\n\tbh=3Ce91cmaduzVV9YHbJ4ak11uE2GaKncWTpCroJeL/Ks=;\n\th=From:To:Cc:Subject:Date:From;\n\tb=FFZUiTzjR8/sl9csnSkKbOAcaWN9e/jTG3bZmVq6XLvGZk9XThOX/HYaWZB/1nSlN\n\txablaFJkVBwUUwrGBKnqjkr45uw0A75K1vSZAEVeazMjWdbfcR70UteteAwLO1NwG+\n\t9/isYsOdJhaGLsTaGf0o65EA1PW58IRIzjxyH4hU=","From":"Stefan Klug <stefan.klug@ideasonboard.com>","To":"libcamera-devel@lists.libcamera.org","Cc":"Stefan Klug <stefan.klug@ideasonboard.com>","Subject":"[PATCH] libcamera: yaml-parser: Differentiate between empty and\n\tempty string","Date":"Tue, 17 Sep 2024 00:28:20 +0200","Message-ID":"<20240916222834.464460-1-stefan.klug@ideasonboard.com>","X-Mailer":"git-send-email 2.43.0","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","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":"When accessing a nonexistent key on a dict the YamlObject returns an\nempty element. This element can happily be casted to a string which is\nunexpected. For example the following statement:\nyamlDict[\"nonexistant\"].get<string>(\"default\") is expected to return\n\"default\" but actually returns \"\". Fix this by introducing a empty type\nto distinguish between an empty YamlObject and a YamlObject of type\nvalue containing an empty string. For completeness add an isEmpty()\nfunction to be able to test for that type.\n\nSigned-off-by: Stefan Klug <stefan.klug@ideasonboard.com>\n---\n include/libcamera/internal/yaml_parser.h |  5 +++++\n src/libcamera/yaml_parser.cpp            | 20 ++++++++++++++------\n test/yaml-parser.cpp                     |  6 ++++++\n 3 files changed, 25 insertions(+), 6 deletions(-)","diff":"diff --git a/include/libcamera/internal/yaml_parser.h b/include/libcamera/internal/yaml_parser.h\nindex 16708e488d88..33c3a96fed47 100644\n--- a/include/libcamera/internal/yaml_parser.h\n+++ b/include/libcamera/internal/yaml_parser.h\n@@ -159,6 +159,10 @@ public:\n \t{\n \t\treturn type_ == Type::Dictionary;\n \t}\n+\tbool isEmpty() const\n+\t{\n+\t\treturn type_ == Type::Empty;\n+\t}\n \n \tstd::size_t size() const;\n \n@@ -212,6 +216,7 @@ private:\n \t\tDictionary,\n \t\tList,\n \t\tValue,\n+\t\tEmpty,\n \t};\n \n \ttemplate<typename T>\ndiff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\nindex 8b6a403898be..eb72529136f7 100644\n--- a/src/libcamera/yaml_parser.cpp\n+++ b/src/libcamera/yaml_parser.cpp\n@@ -38,12 +38,12 @@ static const YamlObject empty;\n  * \\brief A class representing the tree structure of the YAML content\n  *\n  * The YamlObject class represents the tree structure of YAML content. A\n- * YamlObject can be a dictionary or list of YamlObjects or a value if a tree\n- * leaf.\n+ * YamlObject can be empty, a dictionary or list of YamlObjects or a value if a\n+ * tree leaf.\n  */\n \n YamlObject::YamlObject()\n-\t: type_(Type::Value)\n+\t: type_(Type::Empty)\n {\n }\n \n@@ -70,6 +70,13 @@ YamlObject::~YamlObject() = default;\n  * \\return True if the YamlObject is a dictionary, false otherwise\n  */\n \n+/**\n+ * \\fn YamlObject::isEmpty()\n+ * \\brief Return whether the YamlObject is a empty\n+ *\n+ * \\return True if the YamlObject is empty, false otherwise\n+ */\n+\n /**\n  * \\fn YamlObject::size()\n  * \\brief Retrieve the number of elements in a dictionary or list YamlObject\n@@ -443,7 +450,8 @@ template std::optional<std::vector<Size>> YamlObject::getList<Size>() const;\n  *\n  * This function retrieves an element of the YamlObject. Only YamlObject\n  * instances of List type associate elements with index, calling this function\n- * on other types of instances is invalid and results in undefined behaviour.\n+ * on other types of instances or with an invalid index results in an empty\n+ * object.\n  *\n  * \\return The YamlObject as an element of the list\n  */\n@@ -480,8 +488,8 @@ bool YamlObject::contains(const std::string &key) const\n  *\n  * This function retrieve a member of a YamlObject by name. Only YamlObject\n  * instances of Dictionary type associate elements with names, calling this\n- * function on other types of instances is invalid and results in undefined\n- * behaviour.\n+ * function on other types of instances or with a non existant key results in an\n+ * empty object.\n  *\n  * \\return The YamlObject corresponding to the \\a key member\n  */\ndiff --git a/test/yaml-parser.cpp b/test/yaml-parser.cpp\nindex 81c829834667..b8ae0b4c11f8 100644\n--- a/test/yaml-parser.cpp\n+++ b/test/yaml-parser.cpp\n@@ -514,6 +514,12 @@ protected:\n \t\t\ti++;\n \t\t}\n \n+\t\t/* Test access to nonexistent member */\n+\t\tif (dictObj[\"nonexistent\"].get<std::string>(\"default\") != \"default\") {\n+\t\t\tcerr << \"Accessing nonexistent dict fails to return default\" << std::endl;\n+\t\t\treturn TestFail;\n+\t\t}\n+\n \t\t/* Make sure utils::map_keys() works on the adapter. */\n \t\t(void)utils::map_keys(dictObj.asDict());\n \n","prefixes":[]}