{"id":18098,"url":"https://patchwork.libcamera.org/api/patches/18098/?format=json","web_url":"https://patchwork.libcamera.org/patch/18098/","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":"<20230108214357.12641-2-laurent.pinchart@ideasonboard.com>","date":"2023-01-08T21:43:56","name":"[libcamera-devel,v1,1/2] base: utils: Add and use strtod() helper","commit_ref":null,"pull_url":null,"state":"accepted","archived":false,"hash":"ed86ed92c14c764790b22f797bffae9e0ee022e1","submitter":{"id":2,"url":"https://patchwork.libcamera.org/api/people/2/?format=json","name":"Laurent Pinchart","email":"laurent.pinchart@ideasonboard.com"},"delegate":null,"mbox":"https://patchwork.libcamera.org/patch/18098/mbox/","series":[{"id":3702,"url":"https://patchwork.libcamera.org/api/series/3702/?format=json","web_url":"https://patchwork.libcamera.org/project/libcamera/list/?series=3702","date":"2023-01-08T21:43:55","name":"libcamera Fix compilation with uClibc","version":1,"mbox":"https://patchwork.libcamera.org/series/3702/mbox/"}],"comments":"https://patchwork.libcamera.org/api/patches/18098/comments/","check":"pending","checks":"https://patchwork.libcamera.org/api/patches/18098/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 1DECEC322E\n\tfor <parsemail@patchwork.libcamera.org>;\n\tSun,  8 Jan 2023 21:44:05 +0000 (UTC)","from lancelot.ideasonboard.com (localhost [IPv6:::1])\n\tby lancelot.ideasonboard.com (Postfix) with ESMTP id CAD3F625E2;\n\tSun,  8 Jan 2023 22:44:04 +0100 (CET)","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 C3191625D8\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  8 Jan 2023 22:44:02 +0100 (CET)","from pendragon.ideasonboard.com (213-243-189-158.bb.dnainternet.fi\n\t[213.243.189.158])\n\tby perceval.ideasonboard.com (Postfix) with ESMTPSA id 3BD106CF\n\tfor <libcamera-devel@lists.libcamera.org>;\n\tSun,  8 Jan 2023 22:44:02 +0100 (CET)"],"DKIM-Signature":["v=1; a=rsa-sha256; c=relaxed/simple; d=libcamera.org;\n\ts=mail; t=1673214244;\n\tbh=KNj4VmbsOtr15SGL6TtOBEOGJUOlMlXXBv6eM1MclHU=;\n\th=To:Date:In-Reply-To:References:Subject:List-Id:List-Unsubscribe:\n\tList-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:\n\tFrom;\n\tb=BbHjpRbBq0DGmlC6WaywU1SpB8383Rej7Y3t1Q190gKnjHMfMhhzvJU/gP1cGEndb\n\tnVMv215yveE85IpwbbszLXNCkxZOdNsC06all7nxh2Vfku9N3J1K8y5SnRfh0WrosD\n\tonaopjrCvCyMrIAOh3leHTS7TZrVGEc92Uq+dalS1cQmsywI/hx5MqZLL5ng6FENjS\n\tn2NztIrkJe4QFBpD0IUhaq7BuebQuqQizXz/B4BXy7/eqfvJE82uQNOM8J2GeLAkVA\n\tmgNFekoToFW1gyN1g4+e2e02lIFq9CgcDzlJGFQvzB8gXA4snmSnNRIuLVVUkYn9nO\n\t0Y95rILzoSrig==","v=1; a=rsa-sha256; c=relaxed/simple; d=ideasonboard.com;\n\ts=mail; t=1673214242;\n\tbh=KNj4VmbsOtr15SGL6TtOBEOGJUOlMlXXBv6eM1MclHU=;\n\th=From:To:Subject:Date:In-Reply-To:References:From;\n\tb=K326f5h8CJ5RqechnGOZmUX33WVv858A4xIK6mU3Oz7mvO3PVvtBO297w7K3cLlxi\n\t8c5c7j5+YjBBnnjCfbLwGEg4xtAt2KP2THNtBS0fdo5L+DY3Y1uAy09qTlpAbwN4+P\n\tg+gg8MNC/hKv4PBbwoKEo7HLdRfyQObfv1R7hyEg="],"Authentication-Results":"lancelot.ideasonboard.com; dkim=pass (1024-bit key; \n\tunprotected) header.d=ideasonboard.com\n\theader.i=@ideasonboard.com\n\theader.b=\"K326f5h8\"; dkim-atps=neutral","To":"libcamera-devel@lists.libcamera.org","Date":"Sun,  8 Jan 2023 23:43:56 +0200","Message-Id":"<20230108214357.12641-2-laurent.pinchart@ideasonboard.com>","X-Mailer":"git-send-email 2.38.2","In-Reply-To":"<20230108214357.12641-1-laurent.pinchart@ideasonboard.com>","References":"<20230108214357.12641-1-laurent.pinchart@ideasonboard.com>","MIME-Version":"1.0","Content-Transfer-Encoding":"8bit","Subject":"[libcamera-devel] [PATCH v1 1/2] base: utils: Add and use strtod()\n\thelper","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>","From":"Laurent Pinchart via libcamera-devel\n\t<libcamera-devel@lists.libcamera.org>","Reply-To":"Laurent Pinchart <laurent.pinchart@ideasonboard.com>","Errors-To":"libcamera-devel-bounces@lists.libcamera.org","Sender":"\"libcamera-devel\" <libcamera-devel-bounces@lists.libcamera.org>"},"content":"The strtod() function is locale-dependent, and thus ill-suited to parse\nnumbers coming from, for instance, YAML files. The YamlObject class uses\nstrtod_l() to fix that issue, but that function is not available with\nall libc implementations. Correctly handling this problem is becoming\nout of scope for the YamlObject class.\n\nAs a first step, add a strtod() helper function in the utils namespace\nthat copies the implementation from YamlObject, and use it in\nYamlObject. The core issue will then be fixed in utils::strtod().\n\nSigned-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>\n---\n include/libcamera/base/utils.h |  2 ++\n src/libcamera/base/utils.cpp   | 46 ++++++++++++++++++++++++++++++++++\n src/libcamera/yaml_parser.cpp  | 34 +------------------------\n 3 files changed, 49 insertions(+), 33 deletions(-)","diff":"diff --git a/include/libcamera/base/utils.h b/include/libcamera/base/utils.h\nindex eb7bcdf4c173..37d9af609ec7 100644\n--- a/include/libcamera/base/utils.h\n+++ b/include/libcamera/base/utils.h\n@@ -367,6 +367,8 @@ decltype(auto) abs_diff(const T &a, const T &b)\n \t\treturn a - b;\n }\n \n+double strtod(const char *__restrict nptr, char **__restrict endptr);\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\ndiff --git a/src/libcamera/base/utils.cpp b/src/libcamera/base/utils.cpp\nindex 6a307940448e..4a239427a4d9 100644\n--- a/src/libcamera/base/utils.cpp\n+++ b/src/libcamera/base/utils.cpp\n@@ -8,6 +8,7 @@\n #include <libcamera/base/utils.h>\n \n #include <iomanip>\n+#include <locale.h>\n #include <sstream>\n #include <stdlib.h>\n #include <string.h>\n@@ -463,6 +464,51 @@ std::string toAscii(const std::string &str)\n  * \\a b\n  */\n \n+namespace {\n+\n+/*\n+ * RAII wrapper around locale_t instances, to support global locale instances\n+ * without leaking memory.\n+ */\n+class Locale\n+{\n+public:\n+\tLocale(const char *locale)\n+\t{\n+\t\tlocale_ = newlocale(LC_ALL_MASK, locale, static_cast<locale_t>(0));\n+\t}\n+\n+\t~Locale()\n+\t{\n+\t\tfreelocale(locale_);\n+\t}\n+\n+\tlocale_t locale() { return locale_; }\n+\n+private:\n+\tlocale_t locale_;\n+};\n+\n+Locale cLocale(\"C\");\n+\n+} /* namespace */\n+\n+/**\n+ * \\brief Convert a string to a double independently of the current locale\n+ * \\param[in] nptr The string to convert\n+ * \\param[out] endptr Pointer to trailing portion of the string after conversion\n+ *\n+ * This function is a locale-independent version of the std::strtod() function.\n+ * It behaves as the standard function, but uses the \"C\" locale instead of the\n+ * current locale.\n+ *\n+ * \\return The converted value, if any, or 0.0 if the conversion failed.\n+ */\n+double strtod(const char *__restrict nptr, char **__restrict endptr)\n+{\n+\treturn strtod_l(nptr, endptr, cLocale.locale());\n+}\n+\n } /* namespace utils */\n \n #ifndef __DOXYGEN__\ndiff --git a/src/libcamera/yaml_parser.cpp b/src/libcamera/yaml_parser.cpp\nindex 2806c591f75d..153a6d53c3f9 100644\n--- a/src/libcamera/yaml_parser.cpp\n+++ b/src/libcamera/yaml_parser.cpp\n@@ -31,38 +31,6 @@ namespace {\n /* Empty static YamlObject as a safe result for invalid operations */\n static const YamlObject empty;\n \n-/*\n- * Construct a global RAII locale for use by all YAML parser instances to\n- * ensure consistency when parsing configuration files and types regardless of\n- * the system locale configuration.\n- *\n- * For more information see:\n- * - https://bugs.libcamera.org/show_bug.cgi?id=174\n- */\n-class Locale\n-{\n-public:\n-\tLocale(const char *locale)\n-\t{\n-\t\tlocale_ = newlocale(LC_ALL_MASK, locale, static_cast<locale_t>(0));\n-\t\tif (locale_ == static_cast<locale_t>(0))\n-\t\t\tLOG(YamlParser, Fatal)\n-\t\t\t\t<< \"Failed to construct a locale\";\n-\t}\n-\n-\t~Locale()\n-\t{\n-\t\tfreelocale(locale_);\n-\t}\n-\n-\tlocale_t locale() { return locale_; }\n-\n-private:\n-\tlocale_t locale_;\n-};\n-\n-Locale yamlLocale(\"C\");\n-\n } /* namespace */\n \n /**\n@@ -315,7 +283,7 @@ std::optional<double> YamlObject::get() const\n \tchar *end;\n \n \terrno = 0;\n-\tdouble value = strtod_l(value_.c_str(), &end, yamlLocale.locale());\n+\tdouble value = utils::strtod(value_.c_str(), &end);\n \n \tif ('\\0' != *end || errno == ERANGE)\n \t\treturn std::nullopt;\n","prefixes":["libcamera-devel","v1","1/2"]}