mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-09-17 08:05:11 +00:00
Fix config parser aborting on an empty object
'custom:{}' (a DynamicConfigSerializer with nothing set) hits EXPECT_KEY
with a '}' and returns TOK_ERROR, so loadSerial stops there and silently
drops every property after it. Nothing follows 'custom' in NodePrefs
today, so it goes unnoticed until you add one.
Also include stdlib.h, which Arduino.h was providing on-device but not
in the native test build.
This commit is contained in:
@@ -1,4 +1,5 @@
|
||||
#include "ConfigSerializer.h"
|
||||
#include <stdlib.h> // atoi/atol/atof (Arduino.h pulls this in on-device, native builds do not)
|
||||
|
||||
bool ConfigSerializer::saveSerial(Stream& s) {
|
||||
Context context(&s, OP::WRITE);
|
||||
@@ -62,6 +63,7 @@ int ConfigSerializer::Context::readNext() {
|
||||
case EXPECT_COMMA_OR_KEY:
|
||||
if (c == ',') { rd_mode = EXPECT_KEY; return TOK_WHITESPACE; }
|
||||
case EXPECT_KEY:
|
||||
if (rd_len == 0 && c == '}') { rd_mode = EXPECT_COMMA_OR_KEY_OR_CLOSE; return TOK_END_OBJ; } // empty object, eg. 'custom:{}'
|
||||
if (rd_len > 0 && c == ':') { rd_buf[rd_len] = 0; rd_len = 0; rd_mode = EXPECT_VAL_OR_OBJ; return TOK_KEY; }
|
||||
if (rd_len == 0 && is_whitespace(c)) return TOK_WHITESPACE;
|
||||
if (rd_len < CONFIG_MAX_KEYLEN-1 && is_key_char(c)) { rd_buf[rd_len++] = c; return TOK_WHITESPACE; }
|
||||
|
||||
@@ -185,6 +185,47 @@ TEST(ConfigSerializer, LoadSerial_IgnoreUnknowns) {
|
||||
EXPECT_TRUE(match);
|
||||
}
|
||||
|
||||
class TestNested : public ConfigSerializer {
|
||||
class Inner : public ConfigSerializer {
|
||||
protected:
|
||||
void structure() override { } // no properties, so it writes as '{}'
|
||||
};
|
||||
Inner inner;
|
||||
protected:
|
||||
void structure() override {
|
||||
def("age", age);
|
||||
def("inner", inner);
|
||||
def("name", name, sizeof(name)); // comes *after* the empty sub-object
|
||||
}
|
||||
public:
|
||||
int32_t age;
|
||||
char name[16];
|
||||
};
|
||||
|
||||
TEST(ConfigSerializer, LoadSerial_EmptyObject) {
|
||||
MockInputStream s("{age:" TEST_INT_S ",inner:{},name:\"Scott\"}");
|
||||
TestNested data;
|
||||
data.name[0] = 0;
|
||||
|
||||
bool success = data.loadSerial(s);
|
||||
EXPECT_TRUE(success);
|
||||
|
||||
EXPECT_EQ(TEST_INT, data.age);
|
||||
bool match = strcmp("Scott", data.name) == 0;
|
||||
EXPECT_TRUE(match); // properties after an empty object must still load
|
||||
}
|
||||
|
||||
TEST(ConfigSerializer, LoadSerial_EmptyObjectWithWhitespace) {
|
||||
MockInputStream s("{age:" TEST_INT_S ",inner:{ },name:\"Scott\"}");
|
||||
TestNested data;
|
||||
data.name[0] = 0;
|
||||
|
||||
bool success = data.loadSerial(s);
|
||||
EXPECT_TRUE(success);
|
||||
bool match = strcmp("Scott", data.name) == 0;
|
||||
EXPECT_TRUE(match);
|
||||
}
|
||||
|
||||
TEST(DynamicConfigSerializer, GetSet_Basic) {
|
||||
DynamicConfigSerializer data;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user