From 81a5a4f093f747e3bb58ce87cd6fce4db41a9fe9 Mon Sep 17 00:00:00 2001 From: raphaelcoeffic Date: Thu, 24 Aug 2023 08:46:39 +0200 Subject: [PATCH 1/4] fix(yaml): properly escape double quotes --- radio/src/storage/yaml/yaml_tree_walker.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/radio/src/storage/yaml/yaml_tree_walker.cpp b/radio/src/storage/yaml/yaml_tree_walker.cpp index d13c7422b5d..c3a79d1e3d5 100644 --- a/radio/src/storage/yaml/yaml_tree_walker.cpp +++ b/radio/src/storage/yaml/yaml_tree_walker.cpp @@ -119,7 +119,7 @@ static bool yaml_output_string(const char* str, uint32_t max_len, return false; while(max_len > 0 && *str) { - if (*str >= 0x20 && *str <= 0x7E) { + if (*str >= 0x20 && *str <= 0x7E && *str != '\"') { if (!wf(opaque, str++, 1)) return false; max_len--; } From 249150450bd702df438fca5a4b483002dc2ef6bd Mon Sep 17 00:00:00 2001 From: philmoz Date: Thu, 2 Nov 2023 11:19:46 +1100 Subject: [PATCH 2/4] Fix firmware to be able to read model name from Companion with embedded quote. Allow '"' in model name in Companion. --- companion/src/firmwares/modeldata.h | 2 +- radio/src/storage/yaml/yaml_parser.cpp | 4 ---- radio/src/storage/yaml/yaml_tree_walker.cpp | 4 ++-- 3 files changed, 3 insertions(+), 7 deletions(-) diff --git a/companion/src/firmwares/modeldata.h b/companion/src/firmwares/modeldata.h index 632fd4660ed..ec65605f511 100644 --- a/companion/src/firmwares/modeldata.h +++ b/companion/src/firmwares/modeldata.h @@ -49,7 +49,7 @@ constexpr char AIM_MODELDATA_FUNCSWITCHSTART[] {"modeldata.funcswitchstart"}; constexpr int LABEL_LENGTH=16; #define CHAR_FOR_NAMES " ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789_-." -#define CHAR_FOR_NAMES_REGEX "[ A-Za-z0-9_.-,]*" +#define CHAR_FOR_NAMES_REGEX "[ A-Za-z0-9_.-,\"]*" class RSSIAlarmData { public: diff --git a/radio/src/storage/yaml/yaml_parser.cpp b/radio/src/storage/yaml/yaml_parser.cpp index 733315e17e0..2c35b0defc1 100644 --- a/radio/src/storage/yaml/yaml_parser.cpp +++ b/radio/src/storage/yaml/yaml_parser.cpp @@ -285,10 +285,6 @@ YamlParser::parse(const char* buffer, unsigned int size) state = ps_CRLF; continue; } - if (*c == '\"') { - state = ps_ValQuo; - break; - } if (*c == '\\') { state = ps_ValEsc; break; diff --git a/radio/src/storage/yaml/yaml_tree_walker.cpp b/radio/src/storage/yaml/yaml_tree_walker.cpp index c3a79d1e3d5..f425a2c1884 100644 --- a/radio/src/storage/yaml/yaml_tree_walker.cpp +++ b/radio/src/storage/yaml/yaml_tree_walker.cpp @@ -117,9 +117,9 @@ static bool yaml_output_string(const char* str, uint32_t max_len, { if (!wf(opaque, "\"", 1)) return false; - + while(max_len > 0 && *str) { - if (*str >= 0x20 && *str <= 0x7E && *str != '\"') { + if (*str >= 0x20 && *str <= 0x7E && *str != '"') { if (!wf(opaque, str++, 1)) return false; max_len--; } From 8e503bf752394a65004e7beef989f4810a4e13be Mon Sep 17 00:00:00 2001 From: philmoz Date: Tue, 7 Nov 2023 08:34:12 +1100 Subject: [PATCH 3/4] Add tests for model name parsing from YAML. --- radio/src/tests/model.cpp | 81 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 81 insertions(+) create mode 100644 radio/src/tests/model.cpp diff --git a/radio/src/tests/model.cpp b/radio/src/tests/model.cpp new file mode 100644 index 00000000000..6634e219d7d --- /dev/null +++ b/radio/src/tests/model.cpp @@ -0,0 +1,81 @@ +/* + * Copyright (C) EdgeTX + * + * Based on code named + * opentx - https://github.com/opentx/opentx + * th9x - http://code.google.com/p/th9x + * er9x - http://code.google.com/p/er9x + * gruvin9x - http://code.google.com/p/gruvin9x + * + * License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License version 2 as + * published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + */ + +#include "gtests.h" + +#include "storage/yaml/yaml_tree_walker.h" +#include "storage/yaml/yaml_parser.h" +#include "storage/yaml/yaml_datastructs.h" +#include "storage/yaml/yaml_bits.h" + +static const char* _model_config[] = + { + // As written by radio firmware - always enclosed in double quotes + "header: \n" + " name: \"Test Name\"\n", // no embedded double quote + + "header: \n" + " name: \"Test \\x22 Name\"\n", // embedded and encoded double quote + + // As written by Companion - only enclosed in double quotes when necessary + "header: \n" + " name: Test Name\n", // no embedded double quote + + "header: \n" + " name: Test \" Name\n", // embedded double quote in string + + "header: \n" + " name: \"\\\"Test Name\"\n", // embedded double quote at start of string + }; + +static void loadModelYamlStr(const char* str) +{ + YamlTreeWalker tree; + tree.reset(get_modeldata_nodes(), (uint8_t*)&g_model); + + YamlParser yp; + yp.init(YamlTreeWalker::get_parser_calls(), &tree); + + size_t len = strlen(str); + yp.parse(str, len); +} + +static char* modelName() +{ + static char name[LEN_MODEL_NAME + 1]; + strncpy(name, g_model.header.name, LEN_MODEL_NAME); + name[LEN_MODEL_NAME] = 0; + return name; +} + +TEST(Model, testModelNameParse) +{ + loadModelYamlStr(_model_config[0]); + EXPECT_STREQ(modelName(), "Test Name"); + loadModelYamlStr(_model_config[1]); + EXPECT_STREQ(modelName(), "Test \" Name"); + loadModelYamlStr(_model_config[2]); + EXPECT_STREQ(modelName(), "Test Name"); + loadModelYamlStr(_model_config[3]); + EXPECT_STREQ(modelName(), "Test \" Name"); + loadModelYamlStr(_model_config[4]); + EXPECT_STREQ(modelName(), "\"Test Name"); +} From 9b5339c17eeeebf6a72fb8542b6ee564068d5dcc Mon Sep 17 00:00:00 2001 From: philmoz Date: Tue, 7 Nov 2023 08:51:49 +1100 Subject: [PATCH 4/4] Shorten test string for B&W radios. --- radio/src/tests/model.cpp | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/radio/src/tests/model.cpp b/radio/src/tests/model.cpp index 6634e219d7d..d952b0711e8 100644 --- a/radio/src/tests/model.cpp +++ b/radio/src/tests/model.cpp @@ -30,20 +30,20 @@ static const char* _model_config[] = { // As written by radio firmware - always enclosed in double quotes "header: \n" - " name: \"Test Name\"\n", // no embedded double quote + " name: \"Tst Name\"\n", // no embedded double quote "header: \n" - " name: \"Test \\x22 Name\"\n", // embedded and encoded double quote + " name: \"Tst \\x22 Name\"\n", // embedded and encoded double quote // As written by Companion - only enclosed in double quotes when necessary "header: \n" - " name: Test Name\n", // no embedded double quote + " name: Tst Name\n", // no embedded double quote "header: \n" - " name: Test \" Name\n", // embedded double quote in string + " name: Tst \" Name\n", // embedded double quote in string "header: \n" - " name: \"\\\"Test Name\"\n", // embedded double quote at start of string + " name: \"\\\"Tst Name\"\n", // embedded double quote at start of string }; static void loadModelYamlStr(const char* str) @@ -69,13 +69,13 @@ static char* modelName() TEST(Model, testModelNameParse) { loadModelYamlStr(_model_config[0]); - EXPECT_STREQ(modelName(), "Test Name"); + EXPECT_STREQ(modelName(), "Tst Name"); loadModelYamlStr(_model_config[1]); - EXPECT_STREQ(modelName(), "Test \" Name"); + EXPECT_STREQ(modelName(), "Tst \" Name"); loadModelYamlStr(_model_config[2]); - EXPECT_STREQ(modelName(), "Test Name"); + EXPECT_STREQ(modelName(), "Tst Name"); loadModelYamlStr(_model_config[3]); - EXPECT_STREQ(modelName(), "Test \" Name"); + EXPECT_STREQ(modelName(), "Tst \" Name"); loadModelYamlStr(_model_config[4]); - EXPECT_STREQ(modelName(), "\"Test Name"); + EXPECT_STREQ(modelName(), "\"Tst Name"); }