From b5aca8f22e08eceb8ed592861067143848177060 Mon Sep 17 00:00:00 2001 From: Raphael Coeffic Date: Mon, 11 Oct 2021 08:19:43 +0200 Subject: [PATCH] Fix case with invalid switch, pot & slider idx name in config --- .../storage/yaml/yaml_datastructs_funcs.cpp | 24 ++++++++++++++----- radio/src/storage/yaml/yaml_tree_walker.cpp | 21 +++++++++++++--- radio/src/storage/yaml/yaml_tree_walker.h | 1 + 3 files changed, 37 insertions(+), 9 deletions(-) diff --git a/radio/src/storage/yaml/yaml_datastructs_funcs.cpp b/radio/src/storage/yaml/yaml_datastructs_funcs.cpp index f95f952a6..0b81420e0 100644 --- a/radio/src/storage/yaml/yaml_datastructs_funcs.cpp +++ b/radio/src/storage/yaml/yaml_datastructs_funcs.cpp @@ -205,13 +205,17 @@ static uint8_t select_sensor_cfg(uint8_t* data, uint32_t bitoffs) static uint32_t sw_read(const char* val, uint8_t val_len) { - return yaml_parse_enum(enum_MixSources, val, val_len) - MIXSRC_FIRST_SWITCH; + uint32_t sw = yaml_parse_enum(enum_MixSources, val, val_len); + if (sw >= MIXSRC_FIRST_SWITCH) + return sw - MIXSRC_FIRST_SWITCH; + + return -1; } static bool sw_write(uint32_t idx, yaml_writer_func wf, void* opaque) { const char* str = yaml_output_enum(idx + MIXSRC_FIRST_SWITCH, enum_MixSources); - return wf(opaque, str, strlen(str)); + return str ? wf(opaque, str, strlen(str)) : true; } static const struct YamlIdStr enum_SwitchConfig[] = { @@ -230,13 +234,17 @@ static const struct YamlNode struct_switchConfig[] = { static uint32_t pot_read(const char* val, uint8_t val_len) { - return yaml_parse_enum(enum_MixSources, val, val_len) - MIXSRC_FIRST_POT; + uint32_t pot = yaml_parse_enum(enum_MixSources, val, val_len); + if (pot >= MIXSRC_FIRST_POT) + return pot - MIXSRC_FIRST_POT; + + return -1; } static bool pot_write(uint32_t idx, yaml_writer_func wf, void* opaque) { const char* str = yaml_output_enum(idx + MIXSRC_FIRST_POT, enum_MixSources); - return wf(opaque, str, strlen(str)); + return str ? wf(opaque, str, strlen(str)) : true; } static const struct YamlIdStr enum_PotConfig[] = { @@ -255,13 +263,17 @@ static const struct YamlNode struct_potConfig[] = { static uint32_t slider_read(const char* val, uint8_t val_len) { - return yaml_parse_enum(enum_MixSources, val, val_len) - MIXSRC_FIRST_SLIDER; + uint32_t sl = yaml_parse_enum(enum_MixSources, val, val_len); + if (sl >= MIXSRC_FIRST_SLIDER) + return sl - MIXSRC_FIRST_SLIDER; + + return -1; } static bool slider_write(uint32_t idx, yaml_writer_func wf, void* opaque) { const char* str = yaml_output_enum(idx + MIXSRC_FIRST_SLIDER, enum_MixSources); - return wf(opaque, str, strlen(str)); + return str ? wf(opaque, str, strlen(str)) : true; } static const struct YamlIdStr enum_SliderConfig[] = { diff --git a/radio/src/storage/yaml/yaml_tree_walker.cpp b/radio/src/storage/yaml/yaml_tree_walker.cpp index 4a2e0db28..9b4c3cf90 100644 --- a/radio/src/storage/yaml/yaml_tree_walker.cpp +++ b/radio/src/storage/yaml/yaml_tree_walker.cpp @@ -191,7 +191,8 @@ static bool yaml_output_attr(uint8_t* ptr, uint32_t bit_ofs, const YamlNode* nod YamlTreeWalker::YamlTreeWalker() : stack_level(NODE_STACK_DEPTH), virt_level(0), - anon_union(0) + anon_union(0), + idx_invalid(false) { memset(stack,0,sizeof(stack)); } @@ -201,6 +202,7 @@ void YamlTreeWalker::reset(const YamlNode* node, uint8_t* data) this->data = data; stack_level = NODE_STACK_DEPTH; virt_level = 0; + idx_invalid = false; push(); setNode(node); @@ -214,6 +216,7 @@ bool YamlTreeWalker::push() stack_level--; memset(&(stack[stack_level]), 0, sizeof(State)); + idx_invalid = false; return true; } @@ -225,6 +228,8 @@ bool YamlTreeWalker::pop() memset(&(stack[stack_level]), 0, sizeof(State)); stack_level++; + idx_invalid = false; + return true; } @@ -321,6 +326,11 @@ bool YamlTreeWalker::toNextElmt() return false; } + if (idx_invalid) { + idx_invalid = false; + stack[stack_level].elmts = 0; + } + if (getElmts() >= node->u._array.u._a.elmts - 1) return false; @@ -405,7 +415,7 @@ void YamlTreeWalker::toNextAttr() void YamlTreeWalker::setAttrValue(char* buf, uint8_t len) { - if (!buf || !len) + if (!buf || !len || idx_invalid) return; const YamlNode* attr = getAttr(); @@ -417,7 +427,12 @@ void YamlTreeWalker::setAttrValue(char* buf, uint8_t len) else i = yaml_str2uint(buf, len); + //TODO: detect -1 and set idx_invalid = true + while ((i > getElmts()) && toNextElmt()); + + if (i > getElmts()) + idx_invalid = true; } else { yaml_set_attr(data, getBitOffset(), attr, buf, len); @@ -622,7 +637,7 @@ static bool find_node(void* ctx, char* buf, uint8_t len) static void set_attr(void* ctx, char* buf, uint8_t len) { - return ((YamlTreeWalker*)ctx)->setAttrValue(buf,len); + ((YamlTreeWalker*)ctx)->setAttrValue(buf,len); } const YamlParserCalls YamlTreeWalkerCalls = { diff --git a/radio/src/storage/yaml/yaml_tree_walker.h b/radio/src/storage/yaml/yaml_tree_walker.h index ce97b2c61..8747a0124 100644 --- a/radio/src/storage/yaml/yaml_tree_walker.h +++ b/radio/src/storage/yaml/yaml_tree_walker.h @@ -44,6 +44,7 @@ class YamlTreeWalker uint8_t stack_level; uint8_t virt_level; uint8_t anon_union; + bool idx_invalid; uint8_t* data;