Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(yaml): properly escape double quotes #3965

Merged
merged 4 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion companion/src/firmwares/modeldata.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
4 changes: 0 additions & 4 deletions radio/src/storage/yaml/yaml_parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions radio/src/storage/yaml/yaml_tree_walker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
if (*str >= 0x20 && *str <= 0x7E && *str != '"') {
if (!wf(opaque, str++, 1)) return false;
max_len--;
}
Expand Down
81 changes: 81 additions & 0 deletions radio/src/tests/model.cpp
Original file line number Diff line number Diff line change
@@ -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: \"Tst Name\"\n", // no embedded double quote

"header: \n"
" name: \"Tst \\x22 Name\"\n", // embedded and encoded double quote

// As written by Companion - only enclosed in double quotes when necessary
"header: \n"
" name: Tst Name\n", // no embedded double quote

"header: \n"
" name: Tst \" Name\n", // embedded double quote in string

"header: \n"
" name: \"\\\"Tst 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(), "Tst Name");
loadModelYamlStr(_model_config[1]);
EXPECT_STREQ(modelName(), "Tst \" Name");
loadModelYamlStr(_model_config[2]);
EXPECT_STREQ(modelName(), "Tst Name");
loadModelYamlStr(_model_config[3]);
EXPECT_STREQ(modelName(), "Tst \" Name");
loadModelYamlStr(_model_config[4]);
EXPECT_STREQ(modelName(), "\"Tst Name");
}