Fix case with invalid switch, pot & slider idx name in config

This commit is contained in:
Raphael Coeffic
2021-11-10 11:07:00 +01:00
parent 218590b277
commit b5aca8f22e
3 changed files with 37 additions and 9 deletions
@@ -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[] = {
+18 -3
View File
@@ -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 = {
@@ -44,6 +44,7 @@ class YamlTreeWalker
uint8_t stack_level;
uint8_t virt_level;
uint8_t anon_union;
bool idx_invalid;
uint8_t* data;