Skip to content

Commit 65ee80f

Browse files
fix(radio): Fix handling of blank lines in YAML files (#3762)
* Fix handling of blank lines in YAML files. * Added some basic EOL / blank line unit tests --------- Co-authored-by: raphaelcoeffic <raphael.coeffic@frafos.com>
1 parent fec64a9 commit 65ee80f

File tree

2 files changed

+76
-8
lines changed

2 files changed

+76
-8
lines changed

radio/src/storage/yaml/yaml_parser.cpp

+6-8
Original file line numberDiff line numberDiff line change
@@ -302,15 +302,13 @@ YamlParser::parse(const char* buffer, unsigned int size)
302302
break;
303303

304304
case ps_CRLF:
305-
if (*c == '\n') {
306-
// Skip blank lines
307-
while (c < end && (*c == '\r' || *c == '\n'))
308-
c += 1;
309-
// reset state at EOL
305+
// Skip blank lines
306+
while (c < end && (*c == '\r' || *c == '\n'))
307+
c += 1;
308+
// reset state at EOL (unless we have run out of buffer, in case EOL continues in next buffer)
309+
if (c < end)
310310
reset();
311-
continue;
312-
}
313-
break;
311+
continue;
314312
}
315313

316314
c++;

radio/src/tests/yaml.cpp

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
* Copyright (C) EdgeTX
3+
*
4+
* Based on code named
5+
* opentx - https://github.com/opentx/opentx
6+
* th9x - http://code.google.com/p/th9x
7+
* er9x - http://code.google.com/p/er9x
8+
* gruvin9x - http://code.google.com/p/gruvin9x
9+
*
10+
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
11+
*
12+
* This program is free software; you can redistribute it and/or modify
13+
* it under the terms of the GNU General Public License version 2 as
14+
* published by the Free Software Foundation.
15+
*
16+
* This program is distributed in the hope that it will be useful,
17+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
18+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19+
* GNU General Public License for more details.
20+
*/
21+
22+
#include "gtests.h"
23+
24+
#include <storage/yaml/yaml_node.h>
25+
#include <storage/yaml/yaml_parser.h>
26+
#include <storage/yaml/yaml_tree_walker.h>
27+
28+
struct TestStruct {
29+
uint8_t foo;
30+
uint8_t bar;
31+
32+
TestStruct() : foo(0), bar(0) {}
33+
};
34+
35+
static const struct YamlNode struct_TestStruct[] = {
36+
YAML_UNSIGNED( "foo", 8 ),
37+
YAML_UNSIGNED( "bar", 8 ),
38+
YAML_END
39+
};
40+
41+
static const struct YamlNode struct_test[] = {
42+
YAML_STRUCT("testStruct", sizeof(TestStruct) * 8, struct_TestStruct, NULL),
43+
YAML_END
44+
};
45+
46+
static const struct YamlNode _root_node = YAML_ROOT( struct_test );
47+
48+
TEST(Yaml, SkipBlankLines)
49+
{
50+
TestStruct t;
51+
52+
YamlTreeWalker tree;
53+
tree.reset(&_root_node, (uint8_t*)&t);
54+
55+
const char chunk_1[] = "testStruct:\n foo: 12\n";
56+
const char chunk_2[] = "\n bar: 34\n\n fo";
57+
const char chunk_3[] = "o: 45";
58+
59+
YamlParser yp;
60+
yp.init(YamlTreeWalker::get_parser_calls(), &tree);
61+
EXPECT_EQ(YamlParser::CONTINUE_PARSING, yp.parse(chunk_1, sizeof(chunk_1) - 1));
62+
EXPECT_EQ(12, t.foo);
63+
64+
EXPECT_EQ(YamlParser::CONTINUE_PARSING, yp.parse(chunk_2, sizeof(chunk_2) - 1));
65+
EXPECT_EQ(34, t.bar);
66+
67+
yp.set_eof();
68+
EXPECT_EQ(YamlParser::CONTINUE_PARSING, yp.parse(chunk_3, sizeof(chunk_3) - 1));
69+
EXPECT_EQ(45, t.foo);
70+
}

0 commit comments

Comments
 (0)